NiceToMytyuk
NiceToMytyuk

Reputation: 4307

How to add parent items to RecyclerView?

Actually i'm not looking for code but for some suggestion of how can i make something like a parent in RecyclerView.

I mean i have yet a main recyclerView (grey one on the screenshot) where i add items from other recyclerView (not shown on the screen) now i would be able like if i'm pressing on a item from main recyclerView and i press "varianti" it will open an alertDialog where i there is another recyclerView with a lot of item so if i press on one or more of that items that will add that item under the selected item from main recyclerView.

And when obv i delete the item from main recyclerView as "TORTILLA" that have to delet even the parent items from added "VARIANTI"

Here is a screenshot example of what i would to archive

enter image description here

Upvotes: 0

Views: 69

Answers (1)

1nullpointer
1nullpointer

Reputation: 1262

If you have the same List shared across the screens , I guess you can reuse the ViewModel Object .

  1. Create Fragments for both the screens .
  2. Create a View Model with the List of Objects, that will hold all your common objects
  3. Keep the Context of the Viewmodel for both Fragments same - Probably under the same Activity .
  4. You should also consider to keep storing the changes in a local DB . Possibly a Room DB .

A Better approach would be to have both views read data from a Live Data Object (Use it with a ViewModel Object). Whenever there is a change , just update RoomDB/Sqllite or any local Android DB . Your Database will be the single source of truth for all your UI .

The RecyclerView could use Databinding directly to read or Update The List of Objects directly

enter image description here

Upvotes: 1

Related Questions