Android getting exact scroll position in ListView

前端 未结 8 1232
误落风尘
误落风尘 2020-12-01 02:56

I\'d like to get the exact, pixel position of the ListView scroll. And no, I am not referring to the first visible position.

Is there a way to achieve this?

8条回答
  •  天命终不由人
    2020-12-01 03:18

    First Declare your int variable for hold the position.

    int position = 0;
    

    then add scrollListener to your ListView,

    listView.setOnScrollListener(new OnScrollListener() {
    
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                 position = firstVisibleItem;
    
            }
        });
    

    Then after getting new data or any changes in your data that time you need to set the listview current position

    listView.setSelection(position);
    

    I have used after setup my adapter , works fine for me..

提交回复
热议问题