How open fragment from RecyclerView.Adapter<CardAdapter.ViewHolder>

断了今生、忘了曾经 提交于 2019-11-30 08:10:10

Open new fragment as follows in your onclick

@Override
        public void onClick(View view){

            AppCompatActivity activity = (AppCompatActivity) view.getContext();
            Fragment myFragment = new MyFragment();
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();


        }

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.

YourParentActivity myActivity = (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.

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.

public CardAdapter(Context context) {
        super();
        mItems = new ArrayList<NatureItem>();
        NatureItem nature = new NatureItem();
        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

ShareFragment newFragment = new ShareFragment();
FragmentTransaction transaction = /* 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);


public void setFragment(Fragment frag){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.viewFragments, frag);
}

Try this sniped :

ShareFragment newFragment = new ShareFragment();
itemView.getContext().getFragmentManager().beginTransaction()
.replace(R.id.viewFragments, newFragment)
.addToBackStack(null)
.commit();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!