Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

1.TabLayout

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

On CardView ClickListner open another fragment in tab3. So how to open fragment in tab3.

Error is in getFragmentManager():

FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

Instead of this, I tried:

FragmentTransaction transaction = activity.getFragmentManager().beginTransaction(); FragmentTransaction transaction = itemview.getContext().getFragmentManager().beginTransaction(); 

But error is not resolve.

Here is my code:

 public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {      List<NatureItem> mItems;     private int lastPosition = -1;     Context context;     TaskFragment main;     public CardAdapter(Context context,TaskFragment ma)     {          this.context=context;         main=ma;     }       public CardAdapter() {         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);             }        @Override     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {         View v = LayoutInflater.from(viewGroup.getContext())                 .inflate(R.layout.custom_list, viewGroup, false);         ViewHolder viewHolder = new ViewHolder(v);       return viewHolder;     }        @Override     public void onBindViewHolder(ViewHolder viewHolder, int i) {         NatureItem nature = mItems.get(i);         viewHolder.tvNature.setText(nature.getName());         viewHolder.tvDesNature.setText(nature.getDes());         viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());       //  setAnimation(viewHolder.card,i);     }       @Override     public int getItemCount() {         return mItems.size();     }      class ViewHolder extends RecyclerView.ViewHolder {          private int lastPosition = -1;         public ImageView imgThumbnail;         public TextView tvNature;         public TextView tvDesNature;        // Button btnclear,btncancle;         CardView card;         Activity activity;         Context co;         public ViewHolder(final View itemView) {             super(itemView);             imgThumbnail = (ImageView)     itemView.findViewById(R.id.img_thumbnail);             tvNature = (TextView) itemView.findViewById(R.id.tv_nature);             tvDesNature = (TextView) itemView.findViewById(R.id.tv_des_nature);             card = (CardView) itemView.findViewById(R.id.card);        card.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View v) {          Toast.makeText(itemView.getContext(), "Clicked Card...", Toast.LENGTH_LONG).show();             ShareFragment newFragment = new ShareFragment();             FragmentTransaction transaction =  getFragmentManager().beginTransaction();             transaction.replace(R.id.viewFragments, newFragment);             transaction.addToBackStack(null);             transaction.commit();              }     });          }     } } 
like image 253
Nikita Sukhadiya Avatar asked Dec 16 '15 11:12

Nikita Sukhadiya


People also ask

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.

What is the relationship between RecyclerView adapter and RecyclerView ViewHolder?

ViewHolder is not bound to an item or the given RecyclerView. Adapter is not part of this Adapter (if this Adapter merges other adapters).


1 Answers

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();           } 
like image 152
Parinda Rajapaksha Avatar answered Sep 24 '22 02:09

Parinda Rajapaksha