Replace ListFragment with snippet inside tabbed viewpager

I am trying to configure the following navigation in my application:

Actual state: ViewPager + Tabs for scrolling between lists:

  • ListFragment A
  • ListFragment B

Desired condition: ViewPager + Tabs:

  • ListFragment A onListItemSelected replace ListFragment A with DetailFragment A
  • ListFragment B onListItemSelected replace ListFragment B with DetailFragment B

The goal is to display parts of the part inside the tab navigation. I cannot replace the fragment list with detailFragment (objectList does not have a custom layout, and therefore there is no ID And I do not know how to do this). In addition, starting a new action hides the tab bar.

Can anyone help me out?

+4
source share
1 answer

I would make a fragment of a wrapper that knows what to show - a list or details. This would be completely separate from the ViewPager - the pager would only know that it contains wrapping fragments, and they themselves will manage their content.

Implementation will be carried out in the following areas:

 public class WrapperFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyListFragment list = new MyListFragment(); list.setListAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> l, View v, int position, long id) { // Create details fragment based on clicked item position Fragment details = new Fragment(); getChildFragmentManager() .beginTransaction() .replace(R.id.container, details) .addToBackStack(null) .commit(); } }); getChildFragmentManager() .beginTransaction() .add(R.id.container, list) .commit(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // There has to be a view with id `container` inside `wrapper.xml` return inflater.inflate(R.layout.wrapper, container, false); } public class MyListFragment extends ListFragment { private OnItemClickListener listener; public void setOnItemClickListener(OnItemClickListener l) { this.listener = l; } @Override public void onListItemClick(ListView l, View v, int position, long id) { if(listener != null) { listener.onItemClick(l, v, position, id); } } } } 

wrapper.xml . The idea is that the WrapperFragment should provide a layout that contains the view with id container - because we use the view with this id to place the child fragment - MyListFragment or DetailsFragment .

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout> 

Another way. This should work, but you have to try this (the layout has an id itself, and does not have a child with an id container ):

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"/> 
+14
source

All Articles