How to get current position in horizontal recyclerview?
Find the Position of Current Item from Recycleview
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) {
int position = linearLayoutManager.findFirstVisibleItemPosition();
Log.e("position", String.valueOf(position));
}
}
});
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