是否可以检查RecyclerView
的第一项或第二项在用户的屏幕上是否可见?
例如,当用户向下滚动时:
if (first item not visible to user) {
// do something
}
else if ( first item is visible){
// do something
}
我目前要做的是在回收站中添加一个侦听器,以便当用户向下滚动时,它可以执行某些操作并向上滚动。
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");
}
}
});
但是我需要检查第一项是否可见。
答案 0 :(得分:4)
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
if (firstVisiblePosition == 0) { do your thing )
答案 1 :(得分:2)
您可以在RecyclerView.LayoutManager
中找到一些帮助方法,例如,如果您使用LinearLayoutManager
,请检查以下方法:
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.
查看完整的文档here。
在您的代码中:
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 :(得分:0)
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`
}
}
});
答案 3 :(得分:0)
尝试一下。
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();
}
}
}
}
}
});
}