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
Boot Broadcastreceiver Does Not Work on Xiaomi Devices
Listview: Textview with Linkmovementmethod Makes List Item Unclickable
Pcm -> Aac (Encoder) -> Pcm(Decoder) in Real-Time with Correct Optimization
Is Fcm (Firebase Cloud Messaging) Token for One Device or for One Account
Air 3 Native Extensions for Android - Can I/How to Include 3Rd Party Libraries
Fragmenttransaction Animation to Slide in Over Top
How to Make a Listview Transparent in Android
App Crashes on Startup Due to Java.Lang.Illegalargumentexception: Column '_Id' Does Not Exist
Black Screen Before Splash Screen Appear in Android
How to Adjust Text Kerning in Android Textview
Getting Error "Gradle Dsl Method Not Found: 'Compile()'" When Syncing Build.Gradle
How to Apply Primary Key on the Text Fields in Android Database
Arrayindexoutofboundsexception with Custom Android Adapter for Multiple Views in Listview
Automatic Versioning of Android Build Using Git Describe with Gradle
Onpagefinished() Never Called (Webview)!
Layout Problem with Button Margin
Fragment Must Be a Public Static Class to Be Properly Recreated from Instance State