Android How to Get Current Positon on Recyclerview That User Scrolled to Item

Android how can I get current positon on recyclerview that user scrolled to item

You are trying to get the info on the wrong object. It is not the RecyclerView nor the Adapter responsibility but the RecyclerView's LayoutManager.

Instead of the generic ViewTreeObserver.OnScrollChangedListener() I would recommend to add instead the RecyclerView.OnScrollListener and use the onScrollStateChanged(RecyclerView recyclerView, int newState) callback which gives you the newState, you should use SCROLL_STATE_IDLE to fetch its position. Meaning:

yourRecyclerView.getLayoutManager().findFirstVisibleItemPosition();

As Rik van Velzen pointed out, you probably need to cast your
LayoutManager to a LinearLayoutManager or GridLayoutManager
(you have to cast to the correct type you are using) to access these
findVisibleXXXX() methods.

On said callback method. Hope I made this clear enough for your, you can find documentation on the classes here:

RecyclerView.OnScrollListener

yigit's (Google) response on visible positions

Android - Get current position from recyclerview with listener when position changed

this my solution :

        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final int offset = topRv.computeHorizontalScrollOffset();
if (offset % myCellWidth == 0) {
final int position = offset / myCellWidth ;
}
}
});

this solution gives me the current position constantly

How to get current position in horizontal recyclerview?

 SnapHelper mSnapHelper = new PagerSnapHelper();
mSnapHelper.attachToRecyclerView(recyclerView);
LayoutManager recylerViewLayoutManager = new LayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false) ;;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
//Dragging
} else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
review_position = recylerViewLayoutManager.findFirstVisibleItemPosition();
/*
Here load the Image to image view with picaso
*/
Picasso.with(itemView.getContext())
.load(url)
.into(yourImageView, new Callback() {
@Override
public void onSuccess() {

}

@Override
public void onError() {

}
});
}
}
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

int firstVisibleItem = recylerViewLayoutManager.findFirstVisibleItemPosition();
/* Log.e ("VisibleItem", String.valueOf(firstVisibleItem));*/

}
});

Here

SnapHelper mSnapHelper = new PagerSnapHelper();

This makes the horizontal recycler view to show and scroll entire one item at time so you cannot get stuck in middle like half visible and another half invisible

How to focus/scroll to a specific item in recyclerView by fetching item position

Add public function into your adapter.

public class EventsAdapter extends RecyclerView.Adapter<EventsAdapter.ViewHolder> {
...
public int getItemPosition(String eventId) {
for (int i = 0; i < eventsDataModels.size(); i++) {
if (eventsDataModels.get(i).getEventID().equals(eventId)) {
return i;
}
}
return -1;
}

Activity:

private void scrollToPosition() {
String eventId = "someId";
int position = adapter.getItemPosition(eventId);
if (position >= 0) {
recycler.scrollToPosition(position);
}
}

How to find the item that shows up on top of the RecyclerView while scrolling

findFirstCompletelyVisibleItemPosition() will return the adapter position of the first fully visible view.

You can do it like this:

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()).findFirstCompletelyVisibleItemPosition();

// Get the item from the list
mList.get(itemPoition)

}

RecyclerView scroll to position when a new item is added

If want to add the data to the bottom of the list you need to use setStackFromEnd() in RecyclerView Layout Manager.

But first, you need to fix your Adapter. You must not pass your RecylerView to your Adapter. So the following code is wrong:

...
// This is wrong!!
adapter = new RecyclerViewAdapter(data, recyclerView);

recyclerView.setAdapter(adapter);

You need to change your Adapter constructor to only receive the data as its parameter. Something like this:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private List<Data> mData;

public RecyclerViewAdapter(List<Data> data) {
this.mData = data;
}

...
}

Then you can set the data to always added at the last bottom with the following code:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);

adapter = new RecyclerViewAdapter(data);
recyclerView.setAdapter(adapter);

To add the new data, you better to make a new method in the adapter. Something like this:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private List<Data> mData;
...

public void addItem(Data datum) {
mData.add(datum);
notifyItemInserted(mData.size());
}
}

Whenever you have adding a new data, you need to scroll to the bottom with scrollToPosition method. Something like this:

adapter.addItem(newData);
recyclerView.scrollToPosition(adapter.getItemCount() - 1);

Remember that you need to override getItemCount() in your Adapter. It should be something like this:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private List<Data> mData;
public RecyclerViewAdapter(List<Data> data) {
this.mData = data;
}

// Return the total count of items
@Override
public int getItemCount() {
return mData.size();
}

...
}

Please be aware that I'm using a Data pojo as a sample here. You need to change it according to your data type.



Related Topics



Leave a reply



Submit