0

I want to use this line of code : "mAdapter = new MembersAdapter(_______, membersList);" in my fragment class named HomeFragment If it used in Activity then it might be written as mAdapter = new MembersAdapter(MainActivity.this, membersList);

But now, I want to be executed in Fragment and for the Fragment so what should I do ?`

hdiz
  • 1,141
  • 2
  • 13
  • 27
Aarje Ishu
  • 53
  • 2
  • 11

2 Answers2

0

You can write base activity as Fragmentactivity and use the base activity by extending it to other activity so that base activity is maintained in the fragment class.

  youractivity youractivity_object;

  public void onAttach(Activity activity) {
        super.onAttach(activity);
        youractivity_object= (youractivity) activity;

    }
Somasundaram NP
  • 1,018
  • 1
  • 12
  • 36
0

You might try this code: 1/

class Adapter extends RecyclerView.Adapter {
 public interface Adapter2FragementInteraction {
     void onSomeEvent();
 }
 // Override the constructor;
 public Adapter(Adapter2FragmentInteraction listener){
      //Save this listener;
      this.listener = listener;
 }

}

2/ In your Fragment:

`RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setAdapter(new Adapter(
    new Adapter2FragementInteraction(){
        @Override
        public void onSomeEvent(){
              //Do something with your Fragment's Views
        }

    }
);`
Tin Tran
  • 2,265
  • 1
  • 14
  • 17