Skip to content Skip to sidebar Skip to footer

Snaphelper Item Position

I'm using vertical RecyclerView to list my items and SnapHelper to snap center item. The idea is to randomize selection, so user swipe screen or shake the device and it is scrolli

Solution 1:

I used this on a project that had a RecyclerView with SnapHelper, not sure if it is what you want.

mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = newLinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    mAdapter = newDemoSlidesAdapter(getApplicationContext());
    mRecyclerView.setAdapter(mAdapter);

    finalSnapHelpersnapHelper=newLinearSnapHelper();
    snapHelper.attachToRecyclerView(mRecyclerView);

    mRecyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {

        @OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(newState == RecyclerView.SCROLL_STATE_IDLE) {
                ViewcenterView= snapHelper.findSnapView(mLayoutManager);
                intpos= mLayoutManager.getPosition(centerView);
                Log.e("Snapped Item Position:",""+pos);
            }
        }
    });

Solution 2:

I try to use this code with a PagerSnapHelper to mimic the pager behaviour and it was useful but i found some corner cases to solve, if you move fast from the last page to the first one and keep swapping until see the boundarie then the IDLE state doesnt happen and you lose your index. to solve that I move out the position from the IF and add a extra condition for this corner case.

overridefunonScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
             super.onScrollStateChanged(recyclerView, newState)
             val centerView = snapHelper.findSnapView(mLayoutManager)
             val pos = mLayoutManager.getPosition(centerView!!)
             if (newState == RecyclerView.SCROLL_STATE_IDLE || (pos == 0 && newState == RecyclerView.SCROLL_STATE_DRAGGING)) {
                 Log.d("BINDING", "positionView SCROLL_STATE_IDLE: $pos")
             }
         }

Code is in kotlin hope it helps

Solution 3:

privatefunrecyclerViewScrollListener() = object: RecyclerView.OnScrollListener() {
    overridefunonScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            // LinearLayoutManagerval pos = layoutManager.findFirstCompletelyVisibleItemPosition()
            Log.e(TAG, "onScrollStateChanged: $pos")
        }
    }
}

Solution 4:

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            overridefunonScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                ....
            }
        })

Post a Comment for "Snaphelper Item Position"