How to check if first item of RecyclerView is visible on the screen

时光毁灭记忆、已成空白 提交于 2020-05-13 11:53:12

问题


Is it possible to check if the first or second item of the RecyclerView is visible on the screen of the user?

For example when the user scrolls down:

  if (first item not visible to user)  { 
      // do something
  }
  else if ( first item is visible){
      // do something
  }

What I currently do is I add a listener to my recycler so that when the user scrolls down, it will do something and scroll up.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (dy > 0) {
            mAccountLayout.setVisibility(View.GONE);
            mDateLayout.setVisibility(View.GONE);
            Log.d("SCROLLINGDOWN","SCROLL");
        } else {
            mAccountLayout.setVisibility(View.VISIBLE);
            mDateLayout.setVisibility(View.VISIBLE);
            Log.d("SCROLLINGUP","SCROLL");
        }
    }
});

But what I need is to check if the first item is visible or not.


回答1:


You can find some helper methods in RecyclerView.LayoutManager, for example, if you use a LinearLayoutManager, check these methods:

int findFirstCompletelyVisibleItemPosition() // Returns the adapter position of the first fully visible view.
int findFirstVisibleItemPosition() // Returns the adapter position of the first visible view.
int findLastCompletelyVisibleItemPosition() // Returns the adapter position of the last fully visible view.
int findLastVisibleItemPosition() // Returns the adapter position of the last visible view.

See the full docs here.

In your code:

recyclerView.setAdapter(adapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (layoutManager.findFirstVisibleItemPosition() > 0) {
            mAccountLayout.setVisibility(View.GONE);
            mDateLayout.setVisibility(View.GONE);
            Log.d("SCROLLINGDOWN","SCROLL");
        } else {
            mAccountLayout.setVisibility(View.VISIBLE);
            mDateLayout.setVisibility(View.VISIBLE);
            Log.d("SCROLLINGUP","SCROLL");
        }
    }
});



回答2:


int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
if (firstVisiblePosition  == 0) { do your thing )



回答3:


   declare globally LinearLayoutManager layoutManager;

and in oncreate use

     layoutManager = new LinearLayoutManager(this);

use this layoutmanager in recyclerview
   recyclerview.setLayoutManager(layoutManager);

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

                //Tocheck if  recycler is on top
                if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
//Its at top ..
                    //ie first item is visible
                } else {
             //not visible`enter code here`
                }
            }
        });



回答4:


Try this things..

    private LinearLayoutManager linearLayoutManager;// this two line define as global level.
protected int pastVisibleItems, visibleItemCount, totalItemCount;
protected void addScrollListener() {
    rvData.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0) //check for scroll down
            {
                visibleItemCount = linearLayoutManager.getChildCount();
                totalItemCount = linearLayoutManager.getItemCount();
                pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition();

                if (!isPullingMoreResults) {
                    if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                        if (mHasMoreResultsToPull && !isPullingMoreResults) {
                            isPullingMoreResults = true;
                            pageNumber++;
                            getMessage();
                        }
                    }
                }
            }
        }
    });
}


来源:https://stackoverflow.com/questions/50983472/how-to-check-if-first-item-of-recyclerview-is-visible-on-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!