albinmathew / design-support-library-demo

A simple demo of new Design Support Library for Android
Apache License 2.0
1 stars 1 forks source link

One question) #4

Open Radzhab opened 9 years ago

Radzhab commented 9 years ago
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new CheeseListFragment(), "Tab 1");
        adapter.addFragment(new CheeseListFragment(), "Tab 2");
        adapter.addFragment(new CheeseListFragment(), "Tab 1");
        adapter.addFragment(new CheeseListFragment(), "Tab 2");
        viewPager.setAdapter(adapter);

When i debug project its twice enter in onCreateView. Why twice? But i initialize 4 instance of CheeseListFragment?

CheeseListFragment.java
@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        RecyclerView rv = (RecyclerView) inflater.inflate(
                R.layout.fragment_cheese_list, container, false);
       // setupRecyclerView(rv);
        return rv;
    }
albinmathew commented 9 years ago

@Radzhab You should read more about ViewPager and its working. ViewPager itself handles above case itself. For instance, if you are currently at item n, the FragmentPagerAdapter calls the onCreateView() of fragments at n-1, n and n+1. You can manually override this with setOffscreenPageLimit() Read more at http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit%28int%29