How open fragment from RecyclerView.Adapter

前端 未结 6 2019
暗喜
暗喜 2020-12-09 15:46

1.TabLayout

- tab1 (Fragment1)
- tab2 (Fragment2)
- tab3 (Fragment3)
     * RecyclerView + CardView (OnClick)

On CardView Clic

6条回答
  •  独厮守ぢ
    2020-12-09 16:24

    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.

提交回复
热议问题