How to Know Whether a Recyclerview/Linearlayoutmanager Is Scrolled to Top or Bottom

Detect the top of RecyclerView?

If you have a LinearLayoutManager set on your RecyclerView you can use it to find the first visible item:

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(layoutManager);

if (layoutManager.firstCompletelyVisibleItemPosition() == 0) {
//this is the top of the RecyclerView
//Do Something
}

How to detect when user have scrolled to the top most item in a RecyclerView

I have solved my question by myself. I used the code below instead

private boolean isUserScrolling = false;
private boolean isListGoingUp = true;

Then in the RecyclerView OnScrollListener

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//detect is the topmost item visible and is user scrolling? if true then only execute
if(newState == RecyclerView.SCROLL_STATE_DRAGGING){
isUserScrolling = true;
if(isListGoingUp){
//my recycler view is actually inverted so I have to write this condition instead
if(linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1 == list.size()){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(isListGoingUp) {
if (linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1 == list.size()) {
Toast.makeText(getContext(),"exeute something", Toast.LENGTH_SHORT).show();
}
}
}
},50);
//waiting for 50ms because when scrolling down from top, the variable isListGoingUp is still true until the onScrolled method is executed
}
}
}
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(isUserScrolling){
if(dy > 0){
//means user finger is moving up but the list is going down
isListGoingUp = false;
}
else{
//means user finger is moving down but the list is going up
isListGoingUp = true;
}
}
}

Scroll to top in RecyclerView with LinearLayoutManager

Continuing from above comments, ideally, replacing

mRecyclerView.smoothScrollToPosition(0);

in the onClick of the floating action button with

mLayoutManager.scrollToPositionWithOffset(0, 0);

should work. You can also remove the SnackBar code, because you don't need it anyways. So, all in all your above method should look like

public void setFloatingActionButton(final View view) {
float actionButton = (android.support.design.widget.FloatingActionButton) getActivity()
.findViewById(R.id.float);
actionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
.getLayoutManager();
layoutManager.scrollToPositionWithOffset(0, 0);
}
});
}

And if you say that the above doesnt work, then test if the onClick() is even being called or not. Try adding a log message in it and see if its printed.

How programmatically know that recyclerview is scrolled position is bottom of recyclerview to execute following function

first / last visible child depends on the LayoutManager.
If you are using LinearLayoutManager or GridLayoutManager, you can use

int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();

For example:

GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();

For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may be prefered to layout more items than visible for caching.

Android recyclerview scroll to top

Did you try taskRecyclerView.getLayoutManager().scrollToPosition(0) or taskRecyclerView.scrollToPosition(your_position)?

RecyclerView does not implement any scrolling logic in taskRecyclerView.scrollToPosition(your_position) and taskRecyclerView.getLayoutManager().scrollToPosition(0) is more specific and it goes to specific index depending implemented layout, in your case is linear.

Check if RecyclerView is scrollable

There you go:

public boolean isRecyclerScrollable() {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (layoutManager == null || adapter == null) return false;

return layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
}


Related Topics



Leave a reply



Submit