I am creating a demo application with a new android compatibility library . I want to show two lists of fragments using ViewPager. The first shows the root categories, and the second shows the subcategories. After someone clicks on the root category, the viewpager will move to the subcategory, but this will not work, because the elements of the root category are always displayed. I spend a few minutes debugging, and I realized that ViewPager creates the second fragment after the first. (I think the pager cache is the next view or fragment.)
Is it possible to load the second fragment after onListItemClick?
Here is the code:
public class ViewPagerDemoActivity extends FragmentActivity implements OnBookSelectedListener { private static int mSelectedCategoryId; private MyAdapter mMyAdapter; private ViewPager mPager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupPager(); } private void setupPager() { mPager = (ViewPager) findViewById(R.id.pager); mMyAdapter = new MyAdapter(getSupportFragmentManager()); mPager.setAdapter(mMyAdapter); } @Override public void onBookSelected(int categoryId) { mSelectedCategoryId = categoryId; if (mSelectedCategoryId == 0) { mPager.setCurrentItem(1); } else { mPager.setCurrentItem(0); } } public static class MyAdapter extends FragmentPagerAdapter { private static final int NUMBER_OF_FRAGMENTS = 2; public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return BookListFragment.newInstance(mSelectedCategoryId); } @Override public int getCount() { return NUMBER_OF_FRAGMENTS; } }
}
public class BookListFragment extends ListFragment { private static final String ARGUMENT_KEY = "categoryId"; private OnBookSelectedListener mOnBookSelectedListener; private int mCategoryId; public static BookListFragment newInstance(int categoryId) { BookListFragment bookListFragment = new BookListFragment(); Bundle argument = new Bundle(); argument.putInt(ARGUMENT_KEY, categoryId); bookListFragment.setArguments(argument); return bookListFragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCategoryId = getArguments() != null ? getArguments().getInt(ARGUMENT_KEY) : 0; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Books.getBooks(mCategoryId))); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.pager_list, container, false); View tv = v.findViewById(R.id.text); ((TextView) tv).setText("Category :" + mCategoryId); return v; } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); if (mCategoryId == 0) { mOnBookSelectedListener.onBookSelected(1); } else { mOnBookSelectedListener.onBookSelected(0); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mOnBookSelectedListener = (OnBookSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + "Must Implement OnBookSelectedListener."); } } public interface OnBookSelectedListener { public void onBookSelected(int categoryId); }
}
public class Books { private static final String[] fruits = new String[] { "Apple", "Banana", "Orange" }; private static final String[] category = new String[] { "Lorem", "Dolor", "Ipsum" }; public static String[] getBooks(int categoryId) { return categoryId == 0 ? category : fruits; }
}
source share