KJEjava48
KJEjava48

Reputation: 2055

Android : RecyclerView onclick update another RecyclerView

I have to implement 2 recycler views in my android activity in which the first recyclerview shows the category and the second will show the items from the selected category. I have implemented the first recycler view.

Now on clicking an item in the recyclerview I need to highlight that view and also need to show the items under that category. When I click on another view the first view highlight should be changed to a new view that is clicked and display the items from that category. Also when I come to the activity the first item should be highlighted by default in the first recycler view and the corresponding items should be displayed in the second recycler view. How can I implement this task by using the onclick or onTouch listener of the view?

Please help me. Below is my code:

expGrpAdptr=new RecyclerAdapter(this,listDataHeader,prgmImages);
rcyGroups.setAdapter(expGrpAdptr);

RecyclerAdapter.java :

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

List<COAAccount> listDataHeader;
int []  prgmImages;
Context context;

public RecyclerAdapter(Context activity, List<COAAccount> listDataHeader, int[] prgmImages) {
    this.listDataHeader = listDataHeader;
    this.prgmImages = prgmImages;
    context=activity;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.expense_group_view, parent, false);
    ViewHolder vh = new ViewHolder(v);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.grpCoa=listDataHeader.get(position);
    holder.txtGrpName.setText(holder.grpCoa.getStrName());
    holder.rImgGrpExp.setImageResource(prgmImages[0]);
}

@Override
public int getItemCount() {
    return listDataHeader.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    COAAccount grpCoa;
    TextView txtGrpName;
    ImageView rImgGrpExp;
    ImageButton imgGrpEdit;
    boolean checkedItem;

    public ViewHolder(View itemView) {
        super(itemView);

        txtGrpName=(TextView) itemView.findViewById(R.id.txtGrpName);
        rImgGrpExp=(ImageView) itemView.findViewById(R.id.rImgGrpExp);
        imgGrpEdit=(ImageButton) itemView.findViewById(R.id.imgGrpEdit);

    }
}

}

Upvotes: 2

Views: 2018

Answers (1)

okarakose
okarakose

Reputation: 3742

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    List<COAAccount> listDataHeader;
    int []  prgmImages;
    Context context;

    private final OnCOAAccountClickListener onCOAAccountClickListener;

    public interface OnCOAAccountClickListener {
        void onClicked(COAAccount account);
    }

    public RecyclerAdapter(Context activity, List<COAAccount> listDataHeader, int[] prgmImages, OnCOAAccountClickListener onCOAAccountClickListener) {
        this.listDataHeader = listDataHeader;
        this.prgmImages = prgmImages;
        context=activity;
        this.onCOAAccountClickListener = onCOAAccountClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.expense_group_view, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.grpCoa=listDataHeader.get(position);
        holder.txtGrpName.setText(holder.grpCoa.getStrName());
        holder.rImgGrpExp.setImageResource(prgmImages[0]);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onCOAAccountClickListener.onClicked(listDataHeader.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return listDataHeader.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        COAAccount grpCoa;
        TextView txtGrpName;
        ImageView rImgGrpExp;
        ImageButton imgGrpEdit;
        boolean checkedItem;

        public ViewHolder(View itemView) {
            super(itemView);

            txtGrpName=(TextView) itemView.findViewById(R.id.txtGrpName);
            rImgGrpExp=(ImageView) itemView.findViewById(R.id.rImgGrpExp);
            imgGrpEdit=(ImageButton) itemView.findViewById(R.id.imgGrpEdit);

        }
    }
}

in your activity code :

expGrpAdptr=new RecyclerAdapter(this,listDataHeader,prgmImages, new OnCOAAccountClickListener() {
    @Override
    public void onClicked(COAAccount account) {
        // TODO : Update your another adapter of another recyclerview.
    }
});
rcyGroups.setAdapter(expGrpAdptr);

Upvotes: 2

Related Questions