0

I have a fragment where I create the RecyclerView and I want to make another instance of that fragment with the same RecyclerView but with different Adapter to bind different data, someone knows how to do it ?

Should I create the adapter outside the fragment and send it by the fragment constructor ?

  • If your data, model and adapter is different, then why you want the same recyclerview? Is it because of the UI of RecyclerView ? Or why you want another instance of the same fragment? ...do you want to load different data in that same fragment? Please specify, i think i can help. – User9211 Oct 27 '20 at 02:43
  • Yes, I want to load different data in the same fragment, make it good for reusability, but I need a different adapter to load different data. – Breno César Oct 27 '20 at 02:50
  • When you want this to happen? i mean is there any button or some condition when it should happen? I'm sure there's. So do it like this. In your UI activity, pass the list of different data in a different adapter, then set that adapter to the same RV. Also put a condition that when to use which of the 2 adapters. Have you tried it? – User9211 Oct 27 '20 at 02:54
  • Yes, there is, but how can I set a different adapter in the fragment, I can only do it, if I pass the adapter by the fragment constructor, no ? – Breno César Oct 27 '20 at 03:02
  • If I send a new adapter via the constructor I can make my fragment hold any RV with any adaptor, If I put a condition I decrease the reusability of the fragment, no ? – Breno César Oct 27 '20 at 03:05
  • So i think you're opening your fragment from your activity. So yes,use constructor. Pass different data in a different adpater. Also put a boolean in a SharedPreferences, so if that's true, Recyclerview uses adapter1 , if false then adapter2. – User9211 Oct 27 '20 at 03:09
  • You mean a condition in the activity, right ? – Breno César Oct 27 '20 at 03:11
  • Not condition actually. A boolean variable. Put that in SharedPreferences. Retrive it in your fragment. You are going from Activity to Fragment right? If so why not use conditional statement to load any of the two adapter in same RV? – User9211 Oct 27 '20 at 03:17
  • Gotcha you, but I think it's a better approach if I send the right adapter to the fragment and just simply use it there, without needing any condition. Like: Activity sends adapter -> fragment gets the adapter and use it with your RV. With that approach I do not need to compare the adapter received and I can use any adapter, not only 2, gotcha ? – Breno César Oct 27 '20 at 03:22
  • I was searching and found RV with multiple view type, and I think it could be a good approach to this problem too, no ? Link: https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type?rq=1 – Breno César Oct 27 '20 at 03:30
  • Okay. Try. Hope that works for you. Good luck! – User9211 Oct 27 '20 at 03:49

0 Answers0