Scroll multiple horizontal RecyclerView together
Asked Answered
B

4

9

I'm creating an EPG like view for which I have multiple horizontal RecyclerViews (as tv programs) encapsulated inside a LinearLayout. When I scroll one of the RecyclerView, I want the rest of the views to be scrolled together.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    layoutContent.setWeightSum(epg.getChannels().size());

    //prepare recycler views and add into layoutContent based on epg channels
    for(EPG.Channel ch : epg.getChannels()){
        AppLog.error(TAG, "Creating RecyclerView for: " + ch.getDisplayName());

        //create new recycler view
        final RecyclerView rv = new RecyclerView(layoutContent.getContext());
        lstRecyclerViews.add(rv);

        //set layout manager
        rv.setLayoutManager(new LinearLayoutManager(layoutContent.getContext(), LinearLayoutManager.HORIZONTAL, false));

        //create adapter
        rv.setAdapter(new MyAdapter(ch.getPrograms()));
        rv.setItemAnimator(new DefaultItemAnimator());

        //add into parent layout
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        lp.weight = 1;
        layoutContent.addView(rv, lp);
    }
}

I've tried adding a scroll listener to my views but I'm confused with RecyclerView.OnScrollListener's onScrolled method as i can't figure out how to scroll other views.

Any help/suggestion would be helpful.

TV Channels' view

Bast answered 4/8, 2015 at 14:56 Comment(13)
is not StaggeredGridLayoutManager that should be used here?Aurangzeb
@Aurangzeb You maybe right as using LinearLayoutManager is not a final decision :) but even if I use staggered view, so how do I scroll all the RecyclerViews all together when one of the view is scrolled?Bast
with SGLM you have only one RVAurangzeb
@Aurangzeb the reason why I chose multiple RVs is because each RV represents a channel and each channel may have multiple programs. Dont you think putting all together in staggered will mix up all the programs?Bast
SGLM is a grid so its 2D creature: its what you want, isnt it? "A LayoutManager that lays out children in a staggered grid formation. It supports horizontal & vertical layout as well as an ability to layout children in reverse. Staggered grids are likely to have gaps at the edges of the layout. To avoid these gaps, StaggeredGridLayoutManager can offset spans independently or move items between spans. You can control this behavior via setGapStrategy(int)."Aurangzeb
Yes, but then how do I split programs grouped by their channel type on each row?Bast
never used it, but this may help: inducesmile.com/android/…Aurangzeb
as i said i never used it before so i may be wrong ;-) but you can always create your own LayoutManager ;-) or grep the Internet for existing oneAurangzeb
i looked at it and it seems that you may be right in that all the channels can be mixed up (i cannot see how to force "end of row/column" at the layout/adapter level, so that the next items will be placed in the next row/column), sorry for confusion... if you come up with some idea pls let me knowAurangzeb
Might be worth testing GridLayoutManager, in horizontal, with GAP_HANDLING_NONEAloke
by the way, how have you solved the question?Hawkeyed
@Bast Did you manage to solve the question?Astrix
@Astrix not using RecyclerView. But I solved the issue by creating Views dynamically on a Vertical/Horizontal Scrollbar View. Doing so allowed me to scroll in any direction with fling. Though it was a resource expensive layout in total but it worked as I expected.Bast
H
3

HorizontelScrollView

{

Linear Layout

{

Vertical list of horizontal recycler views , override horizontal recycler view's LayoutManager's
canScroolhorizontally() method to return false ,so that they all scroll together according to the Grand Parent ScrollView.

}

Our main focus is to scroll that vertical list of horizontal recycler views horizontally , first i tried to keep all of them on a horizontal scroll view ,but that is something ,which the android system clearly rejects , so i kept one linear layout (in my case vertically oriented ) as a mediator.

so the epg grid now can scroll in vertically as they are inside one vertical recycler view as well as in horizontally because of the Horizontal scroll view. and we should not allow horizontal list to scroll independentaly ,so I extended layoutmanager and disallow horizontal scrolling ,now they only scroll under grandParent scroll .

Hippopotamus answered 23/10, 2015 at 13:39 Comment(2)
Please elaborate a bit on your answer, see: How to answer.Skirt
@Skirt ,thanks for the feedback ,i tried to explain in this edit.Hippopotamus
A
2

You need to always re-calculate position of current items in horizontal recycler views(next h.r.v.) After scrolling in one h.r.v. re-calculate positions of others based on small amount of scroll movement occured in touched h.r.v.

Then override onViewAttachedToWindow in adapter and use method scrollToPositionWithOffset from LinearLayoutManager of particularly h.r.v to set it to right position.

Also when calculating movement dx, don't forget to disable onScrolled method when finger is down to avoid multiple handling of the same event.

Allegorize answered 17/11, 2015 at 11:33 Comment(0)
L
0

FlexboxLayout (made by Google) lets you make something like this with a RecyclerView and a FlexboxLayoutManager. Since its version 3.0(June 28th 2017) you can drag horizontally through it.

For your example use it like this:

    FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getActivity());
    layoutManager.setFlexDirection(FlexDirection.ROW);
    layoutManager.setFlexWrap(FlexWrap.WRAP);
    recyclerView.setLayoutManager(layoutManager);

And don't forget to set the width of your RecyclerView to a bigger number than the screen size, in order to scroll, do not use match_parent:

<android.support.v7.widget.RecyclerView
        android:layout_width="3600dp"
        android:layout_height="match_parent"/>

Note: the ViewHolders are recycled by rows, not by columns, so be careful if you have too heavy items on a very long RecyclerView

Luxor answered 18/12, 2017 at 17:49 Comment(0)
S
0

You need to add textviews in horizontal rows. The length of the textview depends on the duration of the program. I have hosted a sample project on Github. Feel free to clone. https://github.com/xardox69/android_EPG

Seeseebeck answered 28/5, 2018 at 9:45 Comment(2)
Good efforts, but you don't seem to make use of views recycling by using a ListView/RecyclerView. This implementation is quite expensive in terms of processing and memory. In my implementation, I wanted to retain this feature yet would be able to show thousands of programs.Bast
@Bast for that you can use FixedGridLayoutManager. Here is the link to it: github.com/devunwired/recyclerview-playground/blob/master/app/…Seeseebeck

© 2022 - 2024 — McMap. All rights reserved.