Java – fragmentpageradapter displays all fragment contents on the first page

In memory, I have a list of items, each with a title and a 0.. n chunks list (storing some information). I know I am creating this list correctly in memory because I have output the whole structure to the command line

I have an activity that uses viewpager to display this data. Each page of viewpager is an item, which is filled with fragment, which contains the information of each chunk of the item

In my current coding method, the blocks of the first two items are displayed on the first page under the first item. When the page scrolls to the second page, it is empty. When the page scrolls to the third page (I now have only three item lists for testing), it will also display a blank page, but when the page returns to the second page, it will display the block of the first item. Then, Every time you navigate to page 1 and then page 2, the block on page 3 is newly added to the existing list. If there is navigation from page 2 to page 3 and back, the block in Item 1 will be newly added to the list. This can be done by disgusting the growing list. I don't know why it doesn't behave properly

This is my main activity (including fragmentpageradapter):

public class MyActivity extends FragmentActivity {
    MyPagerAdapter mPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Controller.setup();

        mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mPagerAdapter);
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new MyFragment();

            Bundle args = new Bundle();
            args.putInt(MyFragment.POSITION, position);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public int getCount(){
            return Controller.getAmount();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Controller.getItem(position).getTitle();
        }
    }
}

Controller is very simple and can return information as you expect. Controller. Setup() only creates the items list

This is my item fragment:

public class MyFragment extends Fragment {
    public final static String POSITION = "position";
    private int pos;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        pos = getArguments().getInt(POSITION);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_item, container, false);

        Item item = Controller.getItem(pos);
        Iterator<Chunk> chunks = item.getChunks().iterator();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        while(chunks.hasNext()){
            Chunk chunk = chunks.next();
            ChunkFragment entry = new ChunkFragment();

            Bundle args = new Bundle();         
            args.putString(ChunkFragment.INFO_ARG, chunk.getInfo());
            entry.setArguments(args);

            transaction.add(R.id.container, entry);
        }
        transaction.commit();

        return rootView;
    }
}

Finally, my chunk clip:

public class ChunkFragment extends Fragment {
    public static final String INFO_ARG = "Info";
    private String mInfo;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        mInfo = args.getString(INFO_ARG);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //setup view
        ViewGroup rootView;

        rootView = (ViewGroup) inflater.inflate(R.layout.fragment_chunk, container, false);

        TextView infoView = (TextView) rootView.findViewById(R.id.info_text);
        infoView.setText(mInfo);

        return rootView;
    }
}

I know this is a lot of code, but I don't know what the problem is. I simplify it to a minimum, including only the operations that may be the root cause of the problem. I haven't included my XML layout files yet, but they have been tested and work normally

resolvent:

I'm not sure about this, but I believe your problem may be related to the difficulty of instantiating fragments in viewpager fragments

You can consider canceling this as one of the possibilities by following the guidelines for nesting fragments: this actually boils down to using getchildfragmentmanager () in the fragment where you want to add fragments

In addition, you may need to consider extending the fragmentstatepageradapter, which better maintains the fragment state between their destruction and re creation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>