Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll multiple horizontal RecyclerView together

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

like image 867
waqaslam Avatar asked Aug 04 '15 14:08

waqaslam


2 Answers

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 .

like image 190
Vikas Pandey Avatar answered Oct 03 '22 06:10

Vikas Pandey


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.

like image 24
Matej Vukosav Avatar answered Oct 03 '22 04:10

Matej Vukosav