How to get the Scrollposition in the Recyclerview/Layoutmanager?

后端 未结 7 1054
旧时难觅i
旧时难觅i 2020-12-23 20:12

How to get the scrollposition in Recyclerview or the Layoutmanager?

I can measure the scrollposition by adding an OnScrollListener

相关标签:
7条回答
  • 2020-12-23 21:16

    To get ScroolPosition try this:

    1. You will need this:

          mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
      
    2. And then get the position of the mLayoutManager (like this):

     recyclerView
                .addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        int pos = mLayoutManager.getPosition(v);
                        RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(pos);
             }
    });

    With this "pos" you get the position, startin with the 0.

       int pos = mLayoutManager.getPosition(v);
    
    0 讨论(0)
提交回复
热议问题