How to call getFragmentManager on Recycler.Adapter?

前端 未结 9 2147
执笔经年
执笔经年 2020-12-23 16:22

I am converting ListView of my app to RecyclerView. On ListView, it was very easy to implement OnClickListener but in RecyclerView, we have to do it in adapter. I want to op

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 17:14

    To add on to the approved answer: if you still get an error you might need to replace this line;

    FragmentManager manager = ((Activity)context).getFragmentManager();
    

    With this

    FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();
    

    For me this was because I was using the support.v4.app.FragmentManager instead of the regular fragmentmanager

    Still get an error?

    As one comment below pointed out, this might throw a java.lang.ClassCastException: and log ... cannot be cast to android.support.v7.app.AppCompatActivity (check comments for details)

    Their fix was to use this instead (I haven't tested it but it worked for them):

    ((AppCompatActivity)activity).getSupportFragmentManager()
    

提交回复
热议问题