如何在RecyclerView中为Button设置OnClickListener()

时间:2016-12-15 19:54:10

标签: android button android-recyclerview

我有一个带有2个按钮的RecyclerView,现在我试图在RecyclerView中为我的按钮设置onClickListner。如何在recyclerView中为我的Buttons设置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);

    }
}

}

这是我的Fragment for 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;
}

3 个答案:

答案 0 :(得分:1)

由于您在自定义ViewHolder实现中引用了两个按钮,因此只需向每个按钮添加一个OnClickListener,如下所示:

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

您可以在onBindViewHolder方法中设置侦听器。

答案 1 :(得分: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方法中绑定setOnClickListener方法,其中数据通过创建实例来实际给出布局组件而不是用于设置适配器的片段。 < / p>

答案 2 :(得分:1)

onCreateViewHolder

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

RecycleViewHolder构造函数中执行相同的操作。

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