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) {
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"/>
source share