0
  • I have a ViewPager, a FragmentAdapter that extends FragmentPagerAdapter, and a ArrayList of NewsFragment which extends Fragment. The problem is that, when I edit the FragmentList dynamically, My tabLayout and FragmentList contains the correct data, but the viewPager is not showing correct Fragment.
  • For example, first, I go into fragment3, then I replace fragment3 with fragment# in a editing activity. After that I go back to mainActivity and bad thing happens. when I go into the third Fragment which is supposed to display fragment#, it's actually displaying the fragment3 that I instantiated the first time entering it.
  • I assumed the problem probably due to the way that activity manage fragments, maybe fragments are stored in a collection controlled by fragment manager or whatever. What's upsetting. I tried to clear the fragmentManager by popping items one by one, and finally the problem is still. Here is my code that relative to the problem
public class MainActivity extends AppCompatActivity {
    String[] categoriesList = {"娱乐", "军事", "教育", "文化", "健康", "财经", "体育", "汽车", "科技", "社会"};
    ArrayList<ChannelBean> channelBeans;
    ArrayList<ChannelBean> curChannels;
    ArrayList<NewsFragment> newsFragments;
    TabLayout tagBar;
    ViewPager viewPager;
    NewsFragment curFragment = null;
    Button channelManageBtn;

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

        tagBar = (TabLayout) findViewById(R.id.news_tab_layout);
        viewPager = findViewById(R.id.news_view_pager);
        channelManageBtn = findViewById(R.id.moreChannelBtn);
        newsFragments = new ArrayList<>();
        channelBeans = new ArrayList<>();
        curChannels = new ArrayList<>();
        for (String channel : categoriesList) {
            ChannelBean bean = new ChannelBean(channel, true);
            channelBeans.add(bean);
            curChannels.add(bean);
            newsFragments.add(new NewsFragment(channel));
        }


        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), newsFragments));

        tagBar.setupWithViewPager(viewPager);

        channelManageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChannelActivity.startChannelActivity(MainActivity.this, channelBeans);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE) {
            String jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);

            Gson gson = new Gson();
            channelBeans = gson.fromJson(jsonStr, new TypeToken<ArrayList<ChannelBean>>(){}.getType());
            newsFragments.clear();
            curChannels.clear();
            for (ChannelBean channel : channelBeans) {
                if (channel.isSelect()) {
                    newsFragments.add(new NewsFragment(channel.getName()));
                    curChannels.add(channel);
                }
            }

            viewPager.getAdapter().notifyDataSetChanged();

            tagBar.setupWithViewPager(viewPager);


        }


    }

    class FragmentAdapter extends FragmentPagerAdapter {

        ArrayList<NewsFragment> mNewsFragment;

        public FragmentAdapter(FragmentManager fm, ArrayList<NewsFragment> newsFragments) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            mNewsFragment = newsFragments;
        }
        @NonNull
        @Override
        public Fragment getItem(int position) {
            return mNewsFragment.get(position);
        }

        @Override
        public int getCount() {
            return mNewsFragment.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mNewsFragment.get(position).getCategories();
        }

        @Override
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            curFragment = (NewsFragment) object;
            super.setPrimaryItem(container, position, object);
        }
    }
}
  • 1
    You might find your answer here: https://stackoverflow.com/questions/18088076/update-fragment-from-viewpager – Dan Harms Sep 07 '21 at 17:48

0 Answers0