RecyclerView gets the position where the item is displayed

rvview .setOnScrollListener( new RecyclerView.OnScrollListener () {
     @Override
 public void onScrollStateChanged ( RecyclerView recyclerView , int newState) {
         super .onScrollStateChanged( recyclerView , newState) ;
 RecyclerView.LayoutManager layoutManager = recyclerView //
 layout(judgment ) currently Is it LinearLayoutManager
         // Only LinearLayoutManager has the method to find the position of the first and last visible view
 if ( layoutManager instanceof LinearLayoutManager                            ) {
             LinearLayoutManager linearManager = ( LinearLayoutManager ) layoutManager ;
 // Get the position of the last visible view
 int lastItemPosition = linearManager .findLastVisibleItemPosition() ;
 // Get the position of the first visible view
 int firstItemPosition = linearManager .findFirstVisibleItemPosition ( lastItemPosition ( )
 > 10 )
                 { tvCount.setVisibility ( View.VISIBLE ) ; tvCount.setText (
 lastItemPosition _                                                
                            + "\n\n" + list.size());
                shouCount = true;
            } else {
                shouCount = false;
                tvCount.setVisibility(View.GONE);

            }
            switch (newState) {
                case RecyclerView.SCROLL_STATE_IDLE:
                    if (shouCount) {
                        tvCount.setText("一\n " + "↑ \ntop " ) ;
                     }
                     break ;
             }
             LogUtils . LogE ( "setOnScrollListener" , lastItemPosition + " " + firstItemPosition ) ;
         }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

    }
}) ;

Guess you like

Origin blog.csdn.net/qq_34475640/article/details/76084910