I have a layout, in which RecyclerView and a view is used. View is overlapping the RecyclerView at centre. I want to get the position of item at position of view when scroll or when recyclerview scroll stops.
This is how you can get the item position.
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int itemPoition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
}
});
Related
I am practicing to build a chat app. When I insert an item to the recyclerview, I need it to scroll to the bottom. My recyclerview code for now looks like this. I tried adding an onScroll listener and that approach was wrong. Thanks!
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
((SimpleItemAnimator) binding.chatRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
linearLayoutManager.setReverseLayout(true);
binding.chatRecyclerView.setLayoutManager(linearLayoutManager);
binding.chatRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
Log.i(TAG, "onScrolled: " + firstVisiblePosition);
if (dy < 0) {
binding.chatDate.setVisibility(View.VISIBLE);
binding.chatDate.setText(sfdMainDate.format(new Date(chatadapter.getItem(firstVisiblePosition).getTimestamp())));
} else if (dy > 0) {
binding.chatDate.setVisibility(View.GONE);
}
}
});```
Add the data observer to your adapter and override onItemRangeInserted.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
binding.chatRecyclerView.smoothScrollToPosition(0);
}
});
How can I get object from recyclerview when it leaves screen while scrolling? For example I have note with id 11 how can I get this note id whenever it leaves screen?
One way you could do it:
Boolean upScrolling;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
LinearLayoutManager mgr = (LinearLayoutManager) recyclerView.getLayoutManager();
int topPosition = mgr.findFirstVisibleItemPosition();
/// your code
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy < 0) {
upScrolling = true;
} else if (dy > 0) {
upScrolling = false;
}
}
});
After scrolling up, in the listener you get the 1st visible item. At the position your code you write the code to check if the 1st visible item is the one below the item you want. If this is the case then you get the time it went off the screen.
Edited to check if it scrolls up or down.
I am using RecyclerView with CarouselLayoutManager library and i want to detect the most elevated item.
I have the method below to know if the user scrolled the recyclerview and change the current most elevated recyclerview item.
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
int overallXScroll = 0;
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
overallXScroll = overallXScroll + dx;
//if the scroll amount will change the recyclerView item order :
if(overallXScroll > 100){
Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show();
overallXScroll = 0;
}
Log.i("check","overallXScroll->" + overallXScroll);
}
});
This method works very slow and with a delay , what can i do ?
I have implemented addonscrollListener for my recyclerview since I want to
hide my FAB when recyclerview is scrolled as follows :
mRvNearby.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.e("DY",""+dy);
if(dy<0){
mFloatingActionMenu.hideMenuButton(true);
}else{
mFloatingActionMenu.showMenuButton(true);
}
}
});
But unfortunately, when I use this listener, the "dy" only changes once, and when I scrolled continuously, the value
hasn't changed.
I was expecting that when scrolled down, The value will be less than 0.
and when scrolled up, the value is greater than 0.
Use greater than sign instead of less than. When you scroll down the value is greater than 0. So your code will be :
mRvNearby.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.e("DY",""+dy);
if(dy>0){
mFloatingActionMenu.hideMenuButton(true);
}else{
mFloatingActionMenu.showMenuButton(true);
}
}
});
You can use FloatingActionButton.Behavior for showing/hiding FAB while Recyclerview scrolls.
Check out this Tutorial
Want to get scroll coordinates of a RecycleView like this:
int scrollX = recycleView.getScrollX();
But it always returns 0, even though view is already scrolled. Why it does not help? How is it possible get the shifted distance of the content?
Try with this
int scrollX = recycleView.computeHorizontalScrollOffset();
You can use OnScrollListener to achieve this:
private int xScroll = 0;
...
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
xScroll += dx;
}
});