ListView within fragment cant save scroll position

后端 未结 4 1096
北海茫月
北海茫月 2020-12-15 01:52

How can I save the ListView\'s scroll position when the ListView is situated within a ListFragment?

4条回答
  •  北海茫月
    2020-12-15 02:14

    I think your solution is fine for touch mode but for me it wasn't enough. I needed to get the selector on the same selected item, not the first visible:

    @Override
    public void onStop() {
        super.onStop();
        ListView listView = this.getListView();
        lastPosition = listView.getSelectedItemPosition();
        int lastPositionInGroup = lastPosition - listView.getFirstVisiblePosition();
        lastTop = listView.getChildAt( lastPositionInGroup ).getTop();
    }
    
    @Override
    public void onLoadFinished(Loader loader, Cursor data) {
        /* cursor adapter stuff here */
    
        if (lastPosition != AdapterView.INVALID_POSITION) {
            listView.setSelectionFromTop(
                    lastPosition,
                    lastTop != AdapterView.INVALID_POSITION ? lastTop : 0
            );
        }
    }
    

提交回复
热议问题