我有一個帶有2個按鈕的RecyclerView現在我正在嘗試爲RecyclerView中的按鈕設置onClickListner。我如何爲recyclerView中的按鈕設置onClickListener?如何在RecyclerView中爲按鈕設置OnClickListener()
public class CartRecycleAdapter extends RecyclerView.Adapter<CartRecycleAdapter.RecycleViewHolder> {
private ArrayList<CartDataProvider> arraylist = new ArrayList<>();
public CartRecycleAdapter(ArrayList<CartDataProvider> arraylist){
this.arraylist = arraylist;
}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.modelcart, parent,false);
RecycleViewHolder recycleViewHolder = new RecycleViewHolder(view);
return recycleViewHolder;
}
@Override
public void onBindViewHolder(RecycleViewHolder holder, int position) {
CartDataProvider homeCycleDataProvider = arraylist.get(position);
holder.ItemImage.setImageResource(R.drawable.placeholder);
holder.TXV_ItemName.setText(homeCycleDataProvider.getItemName().toString());
holder.TXV_PastPrice.setText(String.valueOf(homeCycleDataProvider.getPastPrice()).toString());
holder.TXV_FixedPrice.setText(String.valueOf(homeCycleDataProvider.getFixedPrice()).toString());
holder.ratingBar.setRating(homeCycleDataProvider.getRating());
}
@Override
public int getItemCount() {
return arraylist.size();
}
public static class RecycleViewHolder extends RecyclerView.ViewHolder{
ImageView ItemImage;
TextView TXV_ItemName, TXV_PastPrice, TXV_FixedPrice, TXV_Percentage,TXV_Quantity;
ImageButton BTN_QuantityMinus, BTN_QuantityPlus;
RatingBar ratingBar;
public RecycleViewHolder(View view){
super(view);
ItemImage = (ImageView) view.findViewById(R.id.item_image);
TXV_ItemName = (TextView) view.findViewById(R.id.item_name);
TXV_PastPrice = (TextView) view.findViewById(R.id.past_price);
TXV_FixedPrice = (TextView) view.findViewById(R.id.fixed_price);
TXV_Percentage = (TextView) view.findViewById(R.id.percentiage);
ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);
TXV_Quantity = (TextView) view.findViewById(R.id.txv_quantity);
BTN_QuantityMinus = (ImageButton) view.findViewById(R.id.quantity_minus);
BTN_QuantityPlus = (ImageButton) view.findViewById(R.id.quantity_plus);
}
}
}
這裏是我的RecyclerView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_cart, container, false);
TXT_TotalAmout = (TextView) view.findViewById(R.id.txt_total_amount);
TXT_Quantity = (TextView) view.findViewById(R.id.txv_quantity);
BTN_QuantityMinus = (ImageButton) view.findViewById(R.id.quantity_minus);
BTNQuantityPlus = (ImageButton) view.findViewById(R.id.quantity_plus);
CartItemRecyclerView = (RecyclerView) view.findViewById(R.id.item_recycler_id);
int i =0;
for (String name : itemname){
CartDataProvider cartDataProvider = new CartDataProvider(itemname[i], imageurl[i],
rating[i],pastprice[i],fixedprice[i]);
arrayList.add(cartDataProvider);
TotalPrice += fixedprice[i];
i++;
}
TXT_TotalAmout.setText(Double.valueOf(TotalPrice).toString());
adapter = new CartRecycleAdapter(arrayList);
CartItemRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
CartItemRecyclerView.setLayoutManager(layoutManager);
CartItemRecyclerView.setAdapter(adapter);
CartItemRecyclerView.setNestedScrollingEnabled(false);
return view;
}
如果我在我的片段中調用setOnClickListener應用程序崩潰 –
'adapter = new CartRecycleAdapter(arrayList); CartItemRecyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(getActivity()); CartItemRecyclerView.setLayoutManager(layoutManager); CartItemRecyclerView.setLayoutManager(layoutManager); CartItemRecyclerView.setAdapter(adapter); CartItemRecyclerView.setNestedScrollingEnabled(false); BTNQuantityPlus.setOnClickListener(新View.OnClickListener(){ @覆蓋 公共無效的onClick(視圖v){ } }); 返回視圖; }' –
好的,但這是一個單獨的問題。 – joelg