How Open Fragment From Recyclerview.adapter
Solution 1:
Open new fragment as follows in your onclick
@OverridepublicvoidonClick(View view){
AppCompatActivityactivity= (AppCompatActivity) view.getContext();
FragmentmyFragment=newMyFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
}
Solution 2:
Problem :
Error:cannot find symbol method getSupportFragmentManager()
Solution :
When you use adapter
context then for access fragment manager you need to cast your context
. So you should use this way.
YourParentActivitymyActivity= (YourParentActivity)context
Now you can access method for fragment manager like ,
myActivity.getSupportFragmentManager();
But keep in your mind that your Fragment
should be imported as a android.support.app.v4.Fragment
otherwise there might be a casting problem.
If you open fragment for particular one tab then you should use getChildSupportFragmentManager()
instead of getSupportFragmentManager()
Note : If you want to call fragment
from adapter
class then you should make interface
and pass listener to your button click method and implement your activity with that interface.
Update :
Also you can pass FragmentManager
to your adapter constructor. Like,
public FragmentManager f_manager;
public CardAdapter(Context context,TaskFragment ma , FragmentManager f_manager)
{
this.context = context;
this.f_manager = f_manager;
main=ma;
}
And after that you can use this.f_manager
in your adapter class getView()
method.
Solution 3:
If you have used support library fragments or default fragments, be sure to use same of it every every where. And use "getSupportFragmentManager" or "getFragmentManager" carefully.
Pass context inside constructor.
publicCardAdapter(Context context) {
super();
mItems = newArrayList<NatureItem>();
NatureItemnature=newNatureItem();
nature.setName("The Paris Attack 2015");
nature.setDes("Lorem ipsum dolor sit amet.");
nature.setThumbnail(R.drawable.news1);
mItems.add(nature);
}
Inside onClick
....Your Code
ShareFragmentnewFragment=newShareFragment();
FragmentTransactiontransaction=/* Here is the change.*/context.getFragmentManager().beginTransaction();
transaction.replace(R.id.viewFragments, newFragment);
...Your Code
Update
Inside onClick call mainActivity setFragment method to replace fragment.
((MainActivity) context).setFragment(yourFragment);
publicvoidsetFragment(Fragment frag){
FragmentTransactiontransaction= getFragmentManager().beginTransaction();
transaction.replace(R.id.viewFragments, frag);
}
Solution 4:
For moving from one fragment to another(from RecyclerView MyViewHolder class) use this
Fragmentfragment=newAttendanceFragment();
FragmentManagerfragmentManager= ((FragmentActivity) mContext).getSupportFragmentManager();
FragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Solution 5:
For the ones using Kotlin, assuming the fragment class you want to start is called MyFragment
, and the container where you will put this fragment has id fragment_container_view
. Probably you will do this inside the bind
method inside your view holder.
itemView.setOnClickListener {
val activity = it.context as? AppCompatActivity
activity?.supportFragmentManager?.commit {
addToBackStack(MyFragment::class.toString())
setReorderingAllowed(true)
replace<MyFragment>(R.id.fragment_container_view)
}
}
Post a Comment for "How Open Fragment From Recyclerview.adapter"