2016-12-15 91 views
0

我有一個帶有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; 
} 

回答

1

既然你已經在你的自定義ViewHolder落實到這兩個按鈕中的一個參考,只需添加一個OnClickListener到每個像這樣的片段:

BTN_QuantityPlus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // do your magic here 
     } 
}); 

你可以在onBindViewHolde中設置監聽器r方法。

+0

如果我在我的片段中調用setOnClickListener應用程序崩潰 –

+0

'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){ } }); 返回視圖; }' –

+0

好的,但這是一個單獨的問題。 – joelg

1

這樣做:

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

      /*handle the click in the bindViewHolder inside 
      the Recycler Adapter,not the fragment that calls this adapter.*/ 
      holder.BTN_QuantityMinus.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //handle the click here. 
       } 
      }); 
      holder.BTN_QuantityPlus.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // handle the click here. 
       } 
      }); 
     } 

    @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); 

     } 
    } 

注意:您需要在onBindViewHolder方法內CLAS setOnClickListener方法,其中數據acually通過創建一個給定的佈局的構成不是說你正在使用,以設置適配器的片段它的實例。

1

在你onCreateViewHolder

recycleViewHolder.BTN_QuantityMinus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //if you need position, just use recycleViewHolder.getAdapterPosition(); 
      } 
     }); 

OR

不要在你的RecycleViewHolder構造相同。

BTN_QuantityPlus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // to get position use getAdapterPosition(); 
     } 
    }); 
相關問題