android viewpager更新数据,Android-ViewPager+Fragment数据更新问题(示例代码)

由于FragmentPagerAdapter内部存在缓存。因此调用notifyDataSetChanged()并不可以去更新Fragment的内容。

能够有两种解决的方法:

(1)重写Adapter的getItemPosition():

public int getItemPosition(Object object) {

return POSITION_NONE;

}当调用notifyDataSetChanged()的时候。ViewPager会remove掉全部的view,然后又一次去载入。可行,可是效率低。

(2)在view上调用SetTag。然后用ViewPager.findViewWithTag()来找到要更新的view,然后做更新。

由于FragmentPagerAdapter内部缓存Fragment的时候,已经是依照tag的方式缓存的,因此。在更新的时候,我们仅仅要依据tag,拿到fragment,然后去更新fragment就能够了。

看下FragmentPagerAdapter的instantiateItem()方法:

public Object instantiateItem(ViewGroup container, int position)

{

if (this.mCurTransaction == null) {

this.mCurTransaction = this.mFragmentManager.beginTransaction();

}

long itemId = getItemId(position);

String name = makeFragmentName(container.getId(), itemId);//这里就是在生成fragment的tag

Fragment fragment = this.mFragmentManager.findFragmentByTag(name);//这里是依据tag查找

if (fragment != null)

{

this.mCurTransaction.attach(fragment);//找到直接attch

} else {

fragment = getItem(position);//找不到的时候。才会调用getItem

this.mCurTransaction.add(container.getId(), fragment, makeFragmentName(container.getId(), itemId));

}

if (fragment != this.mCurrentPrimaryItem) {

fragment.setMenuVisibility(false);

fragment.setUserVisibleHint(false);

}

return fragment;

}依据原代码我们能够知道系统给每个Fragment都打上了一个标签,通过标签来寻找对应的fragment,所以当我们第二次进入fragment的时候。fragment的oncreate,oncreateView方法都不会被调用的。由于FragmentPageAdapter中的getitem()方法根本不会被调用,由于系统会依据标签找到对应的fragment。假设已经存在,就不会被调用,fragment有一个缓存机制在这里。

如今的问题是必需要做更新,那么能够这么弄:

public class FragmentViewPagerAdapter extends FragmentPagerAdapter {

private FragmentManager mFragmentManager;

private List mDatas;

private List tagList = new ArrayList();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A ViewPager is a layout manager that allows the user to swipe left and right to move between pages of data. In the case of a Fragment-based ViewPager, each page is represented by a Fragment. To use a ViewPager with Fragments, you need to do the following: 1. Create a layout file that contains a ViewPager element. 2. Create a Fragment class that will represent a single page in the ViewPager. This class should inflate a layout that contains any UI elements you want to display on the page. 3. Create an adapter class that extends FragmentPagerAdapter or FragmentStatePagerAdapter. This adapter will be responsible for creating and managing the Fragments that are displayed in the ViewPager. 4. Set the adapter on the ViewPager. Here's an example of how to create a Fragment-based ViewPager: 1. Create a layout file that contains a ViewPager element. For example: ``` <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. Create a Fragment class that will represent a single page in the ViewPager. For example: ``` public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my, container, false); // TODO: Set up UI elements in the layout return view; } } ``` 3. Create an adapter class that extends FragmentPagerAdapter or FragmentStatePagerAdapter. For example: ``` public class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // TODO: Return a new instance of MyFragment for the given position return null; } @Override public int getCount() { // TODO: Return the total number of pages return 0; } } ``` 4. Set the adapter on the ViewPager in your Activity or Fragment. For example: ``` ViewPager viewPager = findViewById(R.id.view_pager); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); ``` That's it! You should now have a functioning Fragment-based ViewPager. Of course, you'll need to fill in the TODOs in the code snippets above to actually display content on the pages.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值