Is there a way to prevent the listview from scrolling to its top position when its adapter's data is changed?

前端 未结 4 1063
無奈伤痛
無奈伤痛 2020-12-09 03:43

I\'m having a bit of trouble preserving the scroll position of a list view when changing it\'s adapter\'s data.

What I\'m currently doing is to create a custom Array

4条回答
  •  囚心锁ツ
    2020-12-09 04:09

    In your Expandable/List Adapter, put this method

    public void refresh(List dataList) {
        mDataList.clear();
        mDataList.addAll(events);
        notifyDataSetChanged();
    }
    

    And from your activity, where you want to update the list, put this code

    if (mDataListView.getAdapter() == null) {
        MyDataAdapter myDataAdapter = new MyDataAdapter(mContext, dataList);
        mDataListView.setAdapter(myDataAdapter);
    } else {
        ((MyDataAdapter)mDataListView.getAdapter()).refresh(dataList);
    }
    

    In case of Expandable List View, you will use mDataListView.getExpandableListAdapter() instead of mDataListView.getAdapter()

提交回复
热议问题