How to scroll RecyclerView programmatically using specific pixels?

I have had extended recyclerview, so I can scale it. It works the way I wanted it, but now I wanted to programmatically scroll x the number of pixels. The user story for this is that if I touch the upper half of the screen, it should scroll x pixels, the same applies to the lower half of the screen.

Here is what I did:

Activity:

@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
    // if(mComicViewerFragment != null)
    //     mComicViewerFragment.consumeEvent(event);

    mScaleDetector.onTouchEvent(event);

    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        int y = (int) event.getY();

        if(y >= mScreenSize.y / 2)
        {
            mComicViewerFragment.scrollBy((int)(mScreenSize.y * 0.10f));
            Log.i(ComicApplication.TAG, "ComicViewerActivity.onTouchEvent : You are tapping the lower part : " + y);
        }
        else
        {
            mComicViewerFragment.scrollBy((int)(-mScreenSize.y * 0.10f));
            Log.i(ComicApplication.TAG, "ComicViewerActivity.onTouchEvent : You are tapping the upper part : " + y);
        }
    }

    return super.dispatchTouchEvent(event);
}

Fragment

public void scrollBy(int by)
{
    mScalingRecyclerView.smoothScrollBy(0, by);
}

Every time I click, it does not scroll x the number of pixels. Is it because I created a custom view extending from RecyclerView? Where should I properly call smoothScrollBy? It should be easy, but I'm stuck.

+4
1

recyclerView.smoothScrollBy(0, pixels); , .

, , , .

, 100dp, dp .

, 100dp 100px , 400px Recycler. 4 (400/100).

, , "", 4 .

, , Top Bottom View, Recycler.

+3

Source: https://habr.com/ru/post/1654517/


All Articles