Android FragmentStatePagerAdapter

后端 未结 5 1333
太阳男子
太阳男子 2021-02-01 18:02

I\'m working with a FragmentStatePagerAdapter using this example.

The MyAdapter class is implemented as follows:

public static class MyAdapter extends Fr         


        
5条回答
  •  余生分开走
    2021-02-01 18:22

    public class MyPagerAdapter extends FragmentStatePagerAdapter {
        final Context m_context;
        final WeakReference[] m_fragments;
    
        public DetailPagerAdapter(FragmentManager fm) {
            super(fm);
            m_context = ...
                    m_fragments = new WeakReference[enter size here];
        }
    
        @Override
        public Fragment getItem(int position) {
            final Fragment fragment = instantiate your fragment;
            m_fragments[position] = new WeakReference(fragment);
            return fragment;
        }
    
        @Override
        public int getCount() {
            return ...
        }
    
        public Fragment getFragment(final int position) {
            return m_fragments[position] == null ? null :
                m_fragments[position].get();
        }
    }
    

提交回复
热议问题