1

I'm a Chinese and my English is poor. I met a problem when I use the ViewPager with the ListView (ListView added in ViewPager) and PagerAdapter mode is POSITION_NONE, I saved the inflated ListView with List in PagerAdapter but the strange phenomenon is when I call the pagerAdapter.notifyDataSetChanged the ListView scroll to the first position.Here is the code:

private class MyAdapter2 extends PagerAdapter {


    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(listviews[position], 0);

        return listviews[position];
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
Linh
  • 43,513
  • 18
  • 206
  • 227
SunshineHu
  • 13
  • 6
  • hi sunshine, when ever you refresh the parent layout child layout also will refresh that is the reason your listview also refreshing. (As per my view). – KURUMADDALI ANURAG Dec 29 '15 at 05:05
  • but i save every page in pageAdapter when the view is inflated,such as the first listview and will not inflate the xml except the first time in instantiateItem – SunshineHu Dec 29 '15 at 05:46

1 Answers1

0

You can use this logic... before call dataSetChanged store this listview state in Parcelable varaiable and after dataSetChanged time for restore the listview from a value which you have strored in a parcelable variable like this ..

   Parcelable scrollPos=listview.onSaveInstanceState();
   // call your pagerAdapter.notifyDataSetChanged 

    if(scrollPos != null)
    {
        listview.onRestoreInstanceState(scrollPos);    
    } 

there is another way i found it from here

    // save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

// notify dataset changed or re-assign adapter here

// restore the position of listview
mList.setSelectionFromTop(index, top);
Community
  • 1
  • 1
Tanim reja
  • 1,991
  • 1
  • 15
  • 22