Ryan Ryan - 14 days ago 10
Android Question

Load a fragment on clicking CardView from RecyclerView

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 to do that with FragmentManager or SupportFragmentManager. Please give me some directions.

My RecyclerViewAdapder and ViewHolder are ..

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private String[] eventName;
private String[] eventBrief;

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView textViewName, textViewBrief;
public CardView cardView;

public ViewHolder(View v) {
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) {
EventFragment eventFragment = EventFragment.newInstance();
// Load and view eventFragment here??
}
});
}
}

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]);
}

@Override
public int getItemCount() {
return eventName.length;
}
}

Answer

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:

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()
       }
});
Comments