Load a fragment on clicking CardView from RecyclerView

前端 未结 3 1871
清酒与你
清酒与你 2021-01-06 14:19

I\'ve a RecyclerView and CardView inside it. Now what i want to do is load a simple Fragment on clicking the CardView. I\'m not able t

相关标签:
3条回答
  • 2021-01-06 14:49

    There are many ways to go at this, really. I feel that the best implementation is a callback to the activity notifying that an item has been clicked. I'll give you a hand with some code, see below:

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private String[] eventName;
    private String[] eventBrief;
    private OnItemClickListener listener;
    
    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
        public TextView textViewName, textViewBrief;
        public CardView cardView;
        private OnItemClickListener listener;
    
        public ViewHolder(View v, OnItemClickListener listener) {
            super(v);
    
            textViewName = (TextView)v.findViewById(R.id.textViewName);
            textViewBrief = (TextView)v.findViewById(R.id.textViewBrief);
            final Context context = v.getContext();
            cardView = (CardView)v.findViewById(R.id.card_view);
    
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(listener!=null)
                       listener.onItemClick(textViewName.getText().toString(),
             textViewBrief.getText().toString();
                }
            });
        }
    }
    
    public RecyclerViewAdapter(String[] eventName, String[] eventBrief) {
        this.eventName = eventName;
        this.eventBrief = eventBrief;
    }
    
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_card_view, parent, false);
        return new ViewHolder(v);
    }
    
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textViewName.setText(eventName[position]);
        holder.textViewBrief.setText(eventBrief[position]);
        holder.setOnItemClickListener(listener);
    }
    
    @Override
    public int getItemCount() {
        return eventName.length;
    }
    
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener
    }
    
    public interface OnItemClickListener {
           public void onItemClick(String textName, String textViewBrief); 
        } 
    }   
    

    Then in your activity just instantiate it like this:

    RecyclerViewAdapter adapter = new RecyclerViewAdapter(eventName, eventBrief);
    adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener(){
            public void onItemClick(String textName, String textViewBrief){
                EventFragment eventFragment = EventFragment.newInstance();
                //replace content frame with your own view.
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();    ft.replace(R.id.content_frame, eventFragment).commit()
           }
    });
    
    0 讨论(0)
  • 2021-01-06 14:50

    A simple implementation is to add a onClickListener() to the ViewHolder 's itemView in the constructor of the Adapter.

        public MyViewHolder(View view) {
            super(view);
    
            itemView.setOnClickListener(new View.OnClickListener(){
                @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)
  • 2021-01-06 14:50

    I'm not able to do that with FragmentManager or SupportFragmentManager

    That's because RecyclerViewAdapter doesn't hold a reference to the FragmentManager. You should communicate to the Activity/Fragment hosting the RecyclerView, that the event occurred and from there you start the fragment. To do so check my answer here

    0 讨论(0)
提交回复
热议问题