How open fragment from RecyclerView.Adapter

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

1.TabLayout

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

On CardView Clic

相关标签:
6条回答
  • 2020-12-09 16:11

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-09 16:12

    Try this sniped :

    ShareFragment newFragment = new ShareFragment();
    MainActivity mainActivity = (MainActivity)itemView.getContext()
    mainActivity.getFragmentManager().beginTransaction()
    .replace(R.id.viewFragments, newFragment)
    .addToBackStack(null)
    .commit();
    
    0 讨论(0)
  • 2020-12-09 16:14

    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);
    }
    
    0 讨论(0)
  • 2020-12-09 16:19

    For moving from one fragment to another(from RecyclerView MyViewHolder class) use this

     Fragment fragment = new AttendanceFragment();
                    FragmentManager fragmentManager = ((FragmentActivity) mContext).getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_container, fragment);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 16:29

    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();
    
    
            }
    
    0 讨论(0)
提交回复
热议问题