Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use RecyclerView like vertical viewpager

is this possible to use recyclerview like verticalpager .
what I want is when a user scrolls always the first item offset from the top be zero. like when you scroll in viewpager. is this possible?

like image 565
max Avatar asked Jun 23 '16 15:06

max


People also ask

Does ViewPager use RecyclerView?

ViewPager2 comes with right-to-left support, orientation changes, now you can use RecyclerView with ViewPager which can enable usage of DiffUtils, LayoutManager, PageTransformations, Fragments as pages which are modifiable etc.

Can scroll vertically RecyclerView?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .

Can RecyclerView be horizontal?

The RecyclerView needs an adapter to populate the views in each row (horizontal item) with your data.

Is RecyclerView deprecated?

Today, suddenly Recyclerview. Viewholder became deprecated. Other, android project is no deprecated.


2 Answers

Since API level 25 there's a PagerSnapHelper for that:

SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);

See also this answer.

like image 189
user3252261 Avatar answered Sep 21 '22 08:09

user3252261


Yes, you can.

You can use a simple LinearLayoutManager:

recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL));

and for RecyclerView.Adapter View item use layout_height="match_parent" to get View on full width of screen.

or just use this lib: RecyclerViewPager


Updated:

recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(int newState) {
        if(newState == RecyclerView.SCROLL_STATE_IDLE) {
            // special handler to avoid displaying half elements
            scrollToNext();
        }
        animate();
    }

    @Override
    public void onScrolled(int dx, int dy) {
        animate();
    }
});
like image 22
Victor Ponomarenko Avatar answered Sep 18 '22 08:09

Victor Ponomarenko