Skip to content Skip to sidebar Skip to footer

How To Get Current Position In Horizontal Recyclerview?

I have a recyclerview and an imageview, I want to change imageview image on scroll but I don't know how to get the current position and by it get my image from the list then load i

Solution 1:

SnapHelpermSnapHelper=newPagerSnapHelper();
 mSnapHelper.attachToRecyclerView(recyclerView);
 LayoutManagerrecylerViewLayoutManager=newLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false) ;;
 recyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {
        @OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                   //Dragging 
                } elseif (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, newCallback() {
                        @OverridepublicvoidonSuccess() {

                        }

                        @OverridepublicvoidonError() {

                        }
                    });
                }
            }
        }

        @OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

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

        }
    });

Here

SnapHelpermSnapHelper=newPagerSnapHelper();

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

Solution 2:

Find the Position of Current Item from Recycleview

recyclerView.addOnScrollListener ( newRecyclerView.OnScrollListener() {  

              @OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);

                    if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                        //Dragging
                    } elseif (newState == RecyclerView.SCROLL_STATE_IDLE) {

                        intposition= linearLayoutManager.findFirstVisibleItemPosition();
                        Log.e("position", String.valueOf(position));

                    }

                }
            });

Post a Comment for "How To Get Current Position In Horizontal Recyclerview?"