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

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • 1
    is not StaggeredGridLayoutManager that should be used here? – pskink Aug 04 '15 at 15:01
  • @pskink 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? – waqaslam Aug 04 '15 at 15:07
  • with SGLM you have only one RV – pskink Aug 04 '15 at 15:17
  • 1
    @pskink 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? – waqaslam Aug 04 '15 at 15:38
  • 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)." – pskink Aug 04 '15 at 15:39
  • Yes, but then how do I split programs grouped by their channel type on each row? – waqaslam Aug 04 '15 at 15:42
  • never used it, but this may help: http://inducesmile.com/android/android-staggeredgridlayoutmanager-example-tutorial/ – pskink Aug 04 '15 at 15:45
  • 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 one – pskink Aug 04 '15 at 15:51
  • 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 know – pskink Aug 04 '15 at 16:11
  • Might be worth testing GridLayoutManager, in horizontal, with `GAP_HANDLING_NONE` – Budius Oct 23 '15 at 13:51
  • by the way, how have you solved the question? – Kirill Vashilo Nov 21 '16 at 19:39
  • @waqaslam Did you manage to solve the question? – dvdciri Sep 24 '18 at 16:05
  • @dvdciri 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. – waqaslam Sep 24 '18 at 19:54

4 Answers4

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 .

Vikas Pandey
  • 1,115
  • 1
  • 15
  • 25
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.

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

Mario Velasco
  • 3,336
  • 3
  • 33
  • 50
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

ozmank
  • 763
  • 1
  • 8
  • 23
  • 1
    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. – waqaslam May 28 '18 at 13:55
  • @waqaslam for that you can use FixedGridLayoutManager. Here is the link to it: https://github.com/devunwired/recyclerview-playground/blob/master/app/src/main/java/com/example/android/recyclerplayground/layout/FixedGridLayoutManager.java – ozmank Jun 03 '18 at 07:15