Reputation: 126
Following is my code where I want to add one on clicking BtnAdd and reduce the count by clicking BtnSub but I can't find a way to handle event on button and hence update the count..
I looked for https://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnItemTouchListener.html
but this gives click on whole item. I need to handle on item inside row item of recycler view.
Please suggest if there is any method to achieve this.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ItemHolder>{
private ArrayList<Item> items;
public MyAdapter(ArrayList<Item> items) {
this.items = items;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_layout, parent, false);
view.setOnClickListener(mOnClickListener);
ItemHolder orderObjectHolder = new ItemHolder(view);
return orderObjectHolder;
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
final Item item = items.get(position);
holder.countText.setText("");
holder.currentItem = item;
}
@Override
public int getItemCount() {
return items.size();
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public Item currentItem;
TextView countText;
Button AddBtn, SubBtn;
public ItemHolder(View itemView) {
super(itemView);
countText = (TextView) itemView.findViewById(R.id.order_item_name);
AddBtn = (Button) itemView.findViewById(R.id.order_service1_btn_add);
SubBtn = (Button) itemView.findViewById(R.id.order_service1_btn_sub);
AddBtn.setOnClickListener(this);
SubBtn.setOnClickListener(this);
}
}
}
Upvotes: 0
Views: 4174
Reputation: 3873
try this:
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int i){
myViewHolder.textView.setText(list.get(i));
myViewHolder.btnButton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
/// button click event
}
});
}
Upvotes: 4