Android – how do I programmatically scroll recyclerview through specific pixels?
I expanded recyclerview, so I can expand it. It works the way I want, but now I want to scroll x pixels programmatically. The user story is that if I click on the top half of the screen, it should scroll x pixels, also to the bottom half of the screen
This is what I do:
Activities:
@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);
}
subsection
public void scrollBy(int by)
{
mScalingRecyclerView.smoothScrollBy(0, by);
}
Each time I click, it will not scroll by X pixels. Is this because I created a custom view extended from recyclerview? Where should I call smoothscrollby correctly? It was supposed to be easy, but I got stuck
resolvent:
as recyclerView.smoothScrollBy(0,pixels); Not applicable to your custom view, you can try another way
What you can do is do some math to scroll the position, but it won't be accurate to pixels
Assuming that your items are equal in height and are 100dp, you can convert DP to pixels of any screen type through the code see here
Let's say that the 100dp of each project reaches 100px. You want to scroll 400px on the recycler. These are four positions (400 / 100)
You just need to see the current value in the item location number at the bottom, add 4 and scroll by location or smoothly
This is a helper class that shows you how to get the top or bottom item position in the view if you want to move up or down in the recycler
For a complete solution, check here