1

I want to open a new Fragment called optionsFragment from my RecyclerView Adapter onlick. I tried a lot of tutorials but I'm too stupid for this.

The new fragment should be has a back arrow button at the left top corner to go back to the RecyclerView.

Please help me.

This is my onClick function for my holder in the adapter:

FragmentManager benutzerlisteManager = ((NavigationDrawerActivity)customContext).getSupportFragmentManager();

               BenutzerOverviewFragment benutzerOverviewFragment = new BenutzerOverviewFragment();

               benutzerlisteManager.beginTransaction().replace(R.id.content_navigation_drawer,
                       benutzerOverviewFragment,
                       benutzerOverviewFragment.getTag())
                       .addToBackStack(null)
                       .commit();

I've tried the code at the top but the app is crashing with this error:

NavigationDrawerActivity@b42040b must implement OnFragmentInteractionListener

  • open activity and in activity u can open fragment u can't open fragment whithout any activity – Darshan Kachhadiya Feb 22 '17 at 10:11
  • Pass `FragmentManager` to adapter constructor and use later or you can simply call method from activity or register interface and implement it to your activity class – Piyush Feb 22 '17 at 10:11
  • @DarshanKachhadiya I have a navigation drawer activity where I open the RecyclerView as a fragment. And now I need to open the new fragment when I click a row in my RecyclerView –  Feb 22 '17 at 10:13

5 Answers5

5

Here is how you load new fragment from adapter:

  holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 OptionsFragment optionsFrag = new OptionsFragment ();
((ActivityName)context).getSupportFragmentManager().beginTransaction().replace(R.id.container, optionsFrag,"OptionsFragment").addToBackStack(null).commit();
    }
        });

and for back press icon follow this : Manage toolbar's navigation and back button from fragment in android

Community
  • 1
  • 1
Nishan Khadka
  • 385
  • 1
  • 2
  • 14
1
  1. Create an Interface:
    public interface MyItemClickListener { void onItemClick(int position); }

  2. Implement this interface to your Activity/Fragment:

class MyActivity extends Activity implements MyItemClickListener{ }

  1. pass the object of your Interface to Adapter:

MyAdapter adapter = new MyAdapter(this); //this is instance of MyItemClickListner

  1. Call method of interface from the adapter:

// OnClickListener for holder

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
mMyClickListner.onItemClick(getAdapterPosition());
}
});

This will call onItemClick in you activity/fragment. From this method declaration, you can open fragment:

mMyClickListner.onItemClick(getAdapterPosition());

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
AmmY
  • 1,821
  • 1
  • 20
  • 31
1
AppCompatActivity activity = (AppCompatActivity) view.getContext();

Fragment fragment = new DetailFragment();

activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,fragment).addToBackStack(null).commit();

You can write in onClick

Faruk AK
  • 249
  • 2
  • 6
0

I am assuming that you have activity object. You can use activity object like this

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

            activity.getFragmentManager().beginTransaction().replace(R.id.container,new YourFragment()).addToBackStack(null).commit();


            }
        });
ak sacha
  • 2,149
  • 14
  • 19
  • `context.getFragmentManager()` this will not return your `FragmentManager`. Context should be your activity. – Piyush Feb 22 '17 at 10:15
0

Try it

private Context mContext;
ArrayList<Inquiry> mInquiryYearsLists;
InquiryFragment mInquiryFragment;

public InquiryAdapter(Context context, ArrayList<Inquiry> arrayListingg) {
    this.mInquiryYearsLists = arrayListingg;
    this.mContext = context;
}

@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {

            mInquiryFragment = new InquiryDashboardFragment();

            FragmentTransaction mFragmentTransaction = ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction();
            mFragmentTransaction.replace(R.id.fragment_container, mInquiryFragment). mFragmentTransaction.addToBackStack(null).commit();

        }
    });


}