Load a fragment on clicking CardView from RecyclerView

前端 未结 3 1873
清酒与你
清酒与你 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 {
    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()
           }
    });
    

提交回复
热议问题