2

I am stuck in a situation where I have recyclerview with item data in it and I need to fetch same instance in all the fragments (three) of viewpager to operate on the same recyclerview.

What I have done :

I have made one separate fragment names as ItemListFragment with common buttons and option in the list and now trying to add it as child fragment to my viewpager fragments. I can add them as child fragment but at this point I have to create 3 different ItemListFragment for all the PrentFragments.

Kindly help me to overcome this situation or suggest any other way for using common recyclerlist in all the 3 fragments. I have searched stackoverflow but nothing is of any help.

Here is the image to get an idea about the situation:

enter image description here

Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
Adarsh Sharma
  • 472
  • 2
  • 15
  • Please show some code that illustrates what you have tried. Also show some screenshots or mockups to demonstrate your layout. – Code-Apprentice Jan 24 '17 at 04:54
  • @Code-Apprentice please have a look into the image to get an idea of the situation. Code is pretty simple, one tablayout with 3 fragments in viewpager and each one need to have common child fragment for recyclerview – Adarsh Sharma Jan 24 '17 at 05:21
  • Why do the child fragments have to all be the same instance? Can you just create several instances of the same class? – Code-Apprentice Jan 24 '17 at 05:27
  • @Code-Apprentice I need common list between all three fragments. For now, I am using static list in activity and manipulating the same from all the three instances but this is making my UI bulky and tab swiping is too slow. I am thinking to figure out some way to use single instance of listFragment for all the 3 tabs – Adarsh Sharma Jan 24 '17 at 05:42
  • where does the data for the list come from? – Code-Apprentice Jan 24 '17 at 05:43
  • For now it is from json file in assets. but later I will fetch those from server – Adarsh Sharma Jan 24 '17 at 05:45
  • A static list sounds like a poor design decision. Also, I doubt that sharing a common instance of a fragment will give the performance improvement you want. You should profile your app to measure the performance bottle neck. – Code-Apprentice Jan 24 '17 at 05:50
  • @Code-Apprentice so what would be the better approach for the UI where we need 3 tabs and each one can manipulate one common list. ? Any suggestion ? – Adarsh Sharma Jan 24 '17 at 05:54
  • 1
    What do you mean by "better approach"? Are you still talking about efficiency? From a design perspective, I would have a shared data source rather than sharing a single instance of a fragment. However, from an efficiency perspective, you must measure your code for bottle necks before trying to optimize. – Code-Apprentice Jan 24 '17 at 06:03

1 Answers1

1

You can try using global static reference technique in this case.

Declare your childview in Application class ( a class which extends Application class of Android).

public class AppController extends Application{
  public static final String LOG_TAG = ApplicationController.class
        .getSimpleName();

  // Application Instance
  private static ApplicationController applicationController = null;

  public static ListView childview;
  public static ChildAdapter childAdapter;

   @Override
   public void onCreate() {
    super.onCreate();
    applicationController = this;
   }
}

Now in any of your Fragment suppose Fragment A,check global object of childFragment whether its null or not,if its null initilize it normally like other chilfragments,since its reference is global the varibale in AppController now contains the instance of childfragment.

Now in Fragment B check whether global reference null or not,since you initialized it in Fragment A you will not get null,now attach childFragment to Fragment B like normal childfragments

Ravikant Tiwari
  • 371
  • 3
  • 11