问题
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