本文介绍了安卓viewpager改变适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个ViewPager横向滚动表的应用程序,有时我需要更改适配器加载不同的表。我试着做到以下几点:
I'm developing an app with a ViewPager for lateral scrolling tables, sometimes i need to change the adapter to load a different set of tables.I've tried to do the following:
mViewPager.setAdapter(new pagerAdapterPushed(getSupportFragmentManager()));
mViewPager.getAdapter().notifyDataSetChanged();
indicator.setViewPager(mViewPager);
indicator.invalidate();
mViewPager.invalidate();
但它总是刷新previous数据。是不是不可能性改变ViewPager的适配器?
but it always reload the previous data. Isn't it possibile to change the adapter of a ViewPager?
推荐答案
酷似我的应用程序在做什么...
Exactly like my APP doing...
有关变化ViewPagerAdapter,我所做的是:
For change ViewPagerAdapter, what I did is :
- 清除所有碎片电流适配器内部
- 设置当前适配器为null
-
指定为ViewPager新adpater
- Clear all Fragments inside current adapter
- Set current adapter to null
Assign new adpater for ViewPager
/* Clear all Fragments inside current adapter */
public class MyPagerAdapter extends FragmentPagerAdapter
{
private ArrayList<Fragment> fragments=new ArrayList<Fragment>();
//...some stuff
public void clearAll() //Clear all page
{
for(int i=0; i<fragments.size(); i++)
fragMan.beginTransaction().remove(fragments.get(i)).commit();
fragments.clear();
}
}
/* Set current adapter to null */
Constants.VP.removeAllViews();
Constants.VP.setAdapter(null);
/* Assign new adpater for ViewPager */
Constants.PAGER=new MyewPagerAdapter(getSupportFragmentManager());
Constants.VP.setAdapter(Constants.PAGER);
希望它帮助〜
这篇关于安卓viewpager改变适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!