5

I had extended recyclerview so i can scale it. It works the way I wanted it but now I wanted to programmatically scroll by x amount of pixels. The user story for this is that, if I tap on the upper half part of the screen, it should scroll by x amount of pixel, same goes to the lower half part of the screen.

Here's 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 tap, it doesn't scroll by x amount of pixels. Is this because I created a custom view extending from RecyclerView? Where should I properly call smoothScrollBy? This is suppose to be easy but I am stuck.

Neon Warge
  • 1,817
  • 6
  • 29
  • 53
  • 1
    Not sure if fragments work differently for recyclerview but try this solution -- http://stackoverflow.com/questions/30256036/vertical-scroll-the-recyclerview-in-android-by-pixels – Tasos Sep 14 '16 at 03:46
  • I don't know... This Google is notoriously known for. Look at this method, what does it say? `smoothScrollBy`. The docs are clear at this as well `Animate a scroll by the given amount of pixels along either axis.`. But it doesn't do it!!!! – Neon Warge Sep 14 '16 at 03:49
  • are your items equal in height? – Tasos Sep 14 '16 at 03:55
  • if they are then you can calculate dp to pixels -- http://stackoverflow.com/questions/8295986/how-to-calculate-dp-from-pixels-in-android-pro by code -- if item is 100dp in height calculate the pixels then divide that by how many pixels you want to scroll and round off the result -- that should give you approximate positions to move and scroll by position adding or subtracting the the top position offset if scrolling up or bottom position if scrolling down -- http://stackoverflow.com/questions/24989218/get-visible-items-in-recyclerview – Tasos Sep 14 '16 at 04:02
  • @Tasos, your links are quite useful. To give you an idea, I am doing a very simple comic viewer. For proof-of-concept. I've done this, zooming and all that. Its all good.Technically, I wanted to pan the viewer downwards by x amount of pixels if I tap the screen. I don't really care how high each item is actually. – Neon Warge Sep 14 '16 at 04:12
  • @Tasos also, you have to understand that my original question is, why on earth is RecyclerView not responding when I call smoothScrollBy? In my mind and from the docs this means I can command my RecyclerView to scroll by specific amount of pixels. It doesn't do that base on the code I provided. I hope I cleared things up. – Neon Warge Sep 14 '16 at 04:14
  • 1
    As you cant scroll by pixels for recycleView as the items get recycled so the total height of the screen that contains the items is always the same so you will need to do some maths to scroll by position -- check here how to scroll by pixels -- https://blog.stylingandroid.com/scrolling-recyclerview-part-3/ -- it will be more exact if the items are of equal height – Tasos Sep 14 '16 at 04:16
  • @Tasos, that make sense! Of course, what am I thinking, recyclerview "Recycles" views, it only shows a couple of items as you scroll. It wouldn't be practical if recycling is base on pixel and not on position queried. God damn. I am currently reading the sample code provided on part 3. Thanks a lot! I'll mark you answer as correct. – Neon Warge Sep 14 '16 at 04:18
  • ok thanks i will do that – Tasos Sep 14 '16 at 04:21

1 Answers1

5

As recyclerView.smoothScrollBy(0, pixels); is not working for your custom view you can try an alternative way.

What you can do is scroll by position by doing some Math but it wont be exact to the pixel.

Hypothetically speaking if your Items are of equal height of 100dp you can convert dp to pixels for any screen type by code see here

Lets say 100dp comes to 100px per item and you want to scroll 400px down the Recycler. That's 4 positions (400 / 100).

All you need then is the current in View bottom item position number, add 4 and scroll or smooth scroll by position.

Here is a helper class to show you how to get the Top or Bottom item position in View if you wish to go both up or down the Recycler.

For a complete Solution Check here

Tasos
  • 5,321
  • 1
  • 15
  • 26