Rahul
Rahul

Reputation: 395

Snap Recyclerview item to Center when loaded

I am using an infinite scroll for recyclerview with X items which repeats. Also i want am using PagerSnapHelper() to snap the scroll like viewpager.

So initially when the list is loaded the display is like - enter image description here

As soon as i start scrolling on either side left or right the PageSnapHelper() come in work an snap it like viewpager with center alignment like this - enter image description here

I want to show recyclerview as like 2nd image when initially loaded. Please tell what to do next to acheive that.

here is the code snipet -

linearLayoutManager = new LinearLayoutManager(mContext,   LinearLayoutManager.HORIZONTAL, false);
this.recyclerView.setLayoutManager(linearLayoutManager);
SnapHelper snapHelper = new PagerSnapHelper();
adapterCarousel = new AdapterCarousel(mContext, glideRequestManager);
adapterCarousel.setViewType(RECYCLER_VIEW_TYPE_NORMAL);
adapterCarousel.setCarousel(carousel);
this.recyclerView.setAdapter(adapterCarousel);
snapHelper.attachToRecyclerView(recyclerView);
linearLayoutManager.scrollToPosition(Integer.MAX_VALUE / 2);

Upvotes: 2

Views: 3988

Answers (3)

idunnololz
idunnololz

Reputation: 8363

I solved the issue by using LinearLayoutManager#scrollToPositionWithOffset(pos, offset). I just offset the scroll position by half of the padding between items. This worked for me as my items was the same width as the screen. In your case you need to do some math.

Let's say that your item's width is W, your RecyclerView had width rvW and the padding between items was padding then the amount you want to offset by is given by (rvW - W)/2 - padding/2 or (rvW - W - padding)/2.

Final code:

// post to give the recyclerview time to layout so it's width isn't 0
mRecyclerView.post(new Runnable() {
    void run() {
        int padding = getResources().getDimensionPixelSize(R.dimen.some_padding);
        int rvWidth = mRecyclerView.getWidth();
        int itemWidth = getResources().getDimensionPixelSize(R.dimen.itemWidth);
        int offset = (rvWidth - itemWidth - padding) / 2;
        mLinearLayoutManager.scrollToPositionWithOffset(pos, offset);
    }
});

Some optional improvements to the code:

  • Instead of posting to the RecyclerView, you can use View#measure() and View#getMeasuredWidth() or even the display's width if you are certain that the RecyclerView will always fit the screen's width.

  • If your RecyclerView's children are variable sized you can get the View via mLinearLayoutManager.findViewByPosition(mLinearLayoutManager.findFirstVisibleItemPosition()) and get it's width that way.

Upvotes: 3

Mr_gao
Mr_gao

Reputation: 1

   linearLayoutManager.scrollToPosition((Integer.MAX_VALUE / 2)-1);
   linearLayoutManager.smoothScrollToPosition(Integer.MAX_VALUE / 2);

If there is a better way, please tell me

Upvotes: 0

Katharina
Katharina

Reputation: 1642

for me using recyclerView.smoothScrollToPosition(x); instead of linearLayoutManager.scrollToPosition(x); did the trick

Upvotes: 0

Related Questions