活动更改时,阻止ViewPager重新加载

时间:2019-05-05 16:17:03

标签: android android-fragments android-recyclerview fragment

在我的应用中,我有一个包含ListView的片段。列表中的项目是可单击的,因此将打开一个新活动,并继续处理列表中的必要信息。当我从该活动返回到我的Fragment时,它被重新加载,但是我不想再次获取整个数据,而只是恢复它。 我注意到还有Fragment更改时如何防止重新加载的其他问题,但是我没有找到一个可行的解决方案来更改另一个活动并在不强制重新加载的情况下返回。在这种情况下也有办法防止它吗?那么,它是在MainActivity还是特定的Fragment中实现的?

我的ViewPager的MainActivity:

public class MainActivity extends AppCompatActivity {
   
    private ViewPager viewPager;
    public BottomNavigationView bottomNavigationView;
    QuestionsFragment questionsFragment;
    GroupsFragment groupsFragment;
    ProfileFragment profileFragment;
    MenuItem prevMenuItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Initializing viewPager
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        //Initializing the bottomNavigationView
        bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.navigation_questions:
                                viewPager.setCurrentItem(0);
                                break;
                            case R.id.navigation_groups:
                                viewPager.setCurrentItem(1);
                                break;
                            case R.id.navigation_profile:
                                viewPager.setCurrentItem(2);
                                break;
                        }
                        return false;
                    }
                });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                if (prevMenuItem != null) {
                    prevMenuItem.setChecked(false);
                }
                else
                {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                Log.d("page", "onPageSelected: "+position);
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
                prevMenuItem = bottomNavigationView.getMenu().getItem(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        setupViewPager(viewPager);
    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        questionsFragment = new QuestionsFragment();
        groupsFragment = new GroupsFragment();
        profileFragment = new ProfileFragment();
        adapter.addFragment(questionsFragment);
        adapter.addFragment(groupsFragment);
        adapter.addFragment(profileFragment);
        viewPager.setAdapter(adapter);
        int limit = adapter.getCount();
        viewPager.setOffscreenPageLimit(limit);
    }
}

我想恢复的片段:

public class QuestionsFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter recyclerViewAdapter;
    private RecyclerView.LayoutManager recyclerViewlayoutManager;
    private List<QuestionModel> questionList;
    public QuestionsFragment() {
       
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_questions, container, false);
        
    //fill questionList
    recyclerViewAdapter = new RecyclerViewQuestionAdapter(questionList, getActivity());
    recyclerView.setLayoutManager(recyclerViewlayoutManager);
    recyclerView.setAdapter(recyclerViewAdapter);
        
    return view;
    }
}

1 个答案:

答案 0 :(得分:1)

嗨,您的ViewPagerAdapter必须比FragmentStatePageAdapter扩展FragmentPagerAdapter

FragmentStatePageAdapter在选项卡更改和来回切换时保留片段状态。 返回到现有项目后,将重新创建Fragment实例,并恢复状态。该适配器适用于计数未知的列表或项目变化很大的列表。

setOffscreenPageLimit()设为3或2,至少使用3个片段。

  

使用FragmentPagerAdapter时,必须确保清除任何   onDestroyView()中对当前视图或上下文的引用