How to update ViewPager content? [duplicate]

让人想犯罪 __ 提交于 2019-11-27 14:59:59

问题


This question already has an answer here:

  • ViewPager PagerAdapter not updating the View 39 answers

i have this PageAdapter with days of the week, for each pager I have the same Layout. My problem is how update the right listView when change the day. When I change the day the Pager create a new pager and delete other, when this happened my listview point to new pager but not the current. For Example, is wedneyday and I save listview reference, when i change for Tuesday the monday pager is create and Thursday is deleted, and my reference point to last page create(monday) but I'm on tuesday.

My pageAdapter:

ListView lvwConfig;

@Override
public Object instantiateItem(View pager, int position) {
    LayoutInflater inflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.pager_layout, null);
    lvwConfig = (ListView) v.findViewById(R.id.lvwConfig);
    ((ViewPager) pager).addView(v, 0);

    return v;
}

I want to the ListView always point to listview of the current item.


回答1:


Read this post: PagerAdapter

You can implement this function in PagerAdapter:

public int getItemPosition(Object object){
     return POSITION_NONE;
}

after, you can used this function:

yourPagerAdapterObject.notifyDataSetChanged();

This method is bad if you have too many views as to display. All the views are always recalculated. But in your case, for some views, it's okay.




回答2:


Overriding getItemPosition in PagerAdapter is easier, but bit more inefficient. This way, when you call notifyDataSetChanged(), the view pager will remove all views and reload them all. As so the reload effect is obtained. There was another option that suggested by alvarolb, is to setTag() method in instantiateItem() when instantiating a new view. Then instead of using notifyDataSetChanged(), you can use findViewWithTag() to find the view you want to update.



来源:https://stackoverflow.com/questions/10611018/how-to-update-viewpager-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!