1

I've created a nested RecyclerView like this (It's a vertical RecyclerView and its child views are TextView for the section title and horizontal RecyclerView for the items list).

Some of the items can be in 2 (or more) horizontal RecyclerView at the same time, and I want to keep theme in sync (so if something changes in one of them the others will know it too).

How can I achieve that?

enter image description here

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Shalev Moyal
  • 644
  • 5
  • 12
  • 1
    Try to put the common object items in sperate ArrayList so when you edit info of that object it will be edited in both RecyclerViews – Moh'd Awad May 25 '16 at 14:50
  • what exactly do you mean when you say that if something changes in one `recyclerView`? Is the change due to some user interaction or from network response? – Amit Tiwari May 25 '16 at 14:50
  • @AmitTiwari user interaction. I have a 'Like' button that user can press to like the item. – Shalev Moyal May 25 '16 at 14:51
  • The only way to do this is to use notifyDatasetchanged on the adapters. When you change an item property in a single recycle view make the same change in other adapters and ask them to update their views accordingly. Hope its clear. – Malith Lakshan May 25 '16 at 14:56
  • If you are using a mvc or mvvm patern you will be able to use the same model for both views the only left is to update the view if a property change this can be achieved by the observerable patern – Mike Holtkamp May 25 '16 at 14:58
  • @MikeHoltkamp can you explain how do I use observable in this case please? – Shalev Moyal May 25 '16 at 15:04
  • Take a look at this http://www.javaworld.com/article/2077258/learn-java/observer-and-observable.html – Mike Holtkamp May 26 '16 at 06:49

4 Answers4

1

Its more like a data structure related problem than a problem with RecyclerView. I would suggest to keep a HashMap of the items you're populating in your horizontal lists.

So when a list item is changed (e.g. an item is marked favourite) just change the value in the HashMap from true to false and vice versa.

Then call notifyDatasetChanged() on your adapter to reflect the changes in your horizontal lists.

Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

I assume you are getting a List of Lists from the network or some database and that same items on different horizontal lists would have some common identifier like an id or primary key.

All you need to do on a user interaction is, iterate over all the items in all the horizontal lists, and check if it is the same item whose value has been modified and update the value at that point too. So, it is as simple as calling a notifyDatasetChanged() on each horizontal list in which you changed the data.

Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
0

I have not tested this code and it's kind of psuedo code, also not sure if it's a good solution but wanna try to point you right direction. Maybe you find something useful. Ignore the bad naming..

Create an interface that all the nested recycler view implements, that does the actual scrolling.

public interface ScrollListener {
        void update(int scrollOffset);
}

implement in nested recycler views

public class NestedRecyclerViewFragment implements NestedScrollListener {

        RecyclerView mRecyclerView;

        @Override
        public onCreate(Bundle savedInstance) {
            mRecyclerView.addOnScrollListener { 
                public onScroll(int xOffset, int yOffset) {
                   if (getActivity() instanceof OnNestedRecyclerViewScrollListener) {
                        ((OnNestedRecyclerViewScrollListener) getActivity()).onNestedScroll(xOffset);
                    }
                }
            }
        }
        ....

        @Override
        protected void update(int scrollOffset) {
            mRecyclerView.scrollTo(scrollOffset, 0);
        }

        ....
}

Then you might have this other interface which the parent implements.

public interface OnNestedRecyclerViewScrollListener {
        void onNestedScroll(int scrollOffset);
}

Which has the nested recycler views.

public class ParentFragment implements OnNestedRecyclerViewScrollListener {

           Adapter nestedRecyclerViewAdapter;

            ....

            @Override
            protected void onNestedScroll(int scrollOffset) {
                for (int i = 0; i < nestedRecyclerViewAdapter.getItemCount(); i++) {
                    RecyclerView recyclerView = nestedRecyclerViewAdapter.getItemAt(i);
                    if (recyclerView instanceof NestedScrollListener) {
                        ((NestedScrollListener) recyclerView).update(scrollOffset);
                    }
                }
            }

            ....
    }

Note: Might be bad to iterate the items in the adapter all the time but it might point you in right direction

malmling
  • 2,398
  • 4
  • 19
  • 33
0

If you don't want to unify RecyclerViews data models, you can use LocalBroadcastReceiver. Every RecyclerView should set itself as observer and when needed you can notify any changes in one RecyclerView and do what you want in the other RecyclerViews.

lubilis
  • 3,942
  • 4
  • 31
  • 54