如何在Recyclerview中为所有imageView添加相同的onClick? [英] How can add same onClick for all imageView in Recyclerview?

查看:85
本文介绍了如何在Recyclerview中为所有imageView添加相同的onClick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目进行公交车票预订因此,当单击recyclierview中的item时,该公交车座位将以50 imageView的形式打开.我知道如何为一个imageView添加一个onClick但为一个我不知道的50 imageView添加.我尝试了一些在stackOverflow中看到的解决方案.但是没有任何作用.您能给我一些建议或解决方案来预订巴士吗?

My project do bus ticket booking So, when click item in recyclierview that opens bus seat as 50 imageView. I know how can add onClick for one imageView but for 50 imageView that I don't know. I tried some solutions that I saw in stackOverflow. But nothing work. Could you give me advice or solution for making bus booking?

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

我尝试添加此代码并实现onClick方法,但是它不执行onClick内部的内容.

I tried add this code and implement onClick method but it don't do what is inside onClick.

我想添加relative_bus_seat onClick的内容以与此50 imageView相同.

I want add what relative_bus_seat onClick do to same for this 50 imageView do.

BusAdapter.java

BusAdapter.java

 public class BusAdapter extends RecyclerView.Adapter<BusAdapter.ViewHolder> {
        private Context context;
        private List<Bus> busList;
        private static String phoneNumber;
        private static final int PERMISSION_CODE = 101;
        private RecyclerView recyclerView;
        private Locale trlocale;


        VoyageAdapterToResultActivityListener voyageAdapterToResultActivityListener;

        public BusAdapter(Context _context, List<Bus> _busList, RecyclerView recyclerView) {
            this.context = _context;
            this.busList = _busList;
            this.recyclerView = recyclerView;
            this.trlocale = new Locale("tr-TR");
            voyageAdapterToResultActivityListener = (VoyageAdapterToResultActivityListener) context;
        }
        public class ViewHolder extends RecyclerView.ViewHolder {
            private TextView tvPrice, text_seat;
            private CardView cardV_row_voyage;
            private RelativeLayout relative_bus_seat;

            // Seats
            private ImageView imageV_seat1, imageV_seat2, imageV_seat3, imageV_seat4, imageV_seat5, imageV_seat6, imageV_seat7,
                    imageV_seat8, imageV_seat9, imageV_seat10, imageV_seat11, imageV_seat12, imageV_seat13, imageV_seat14, imageV_seat15,
                    imageV_seat16, imageV_seat17, imageV_seat18, imageV_seat19, imageV_seat20, imageV_seat21, imageV_seat22, imageV_seat23,
                    imageV_seat24, imageV_seat25, imageV_seat26, imageV_seat27, imageV_seat28, imageV_seat29, imageV_seat30, imageV_seat31,
                    imageV_seat32, imageV_seat33, imageV_seat34, imageV_seat35, imageV_seat36, imageV_seat37, imageV_seat38, imageV_seat39,
                    imageV_seat40, imageV_seat41, imageV_seat42, imageV_seat43, imageV_seat44, imageV_seat45, imageV_seat46, imageV_seat47,
                    imageV_seat48, imageV_seat49, imageV_seat50;

            private ViewHolder(View view) {
                super(view);
                recyclerView.setHasFixedSize(true);
              // this.setIsRecyclable(false);
                constraintLayout_expanded = view.findViewById(R.id.constraintLayout2);
                cardV_row_voyage = view.findViewById(R.id.cardV_row_voyage);
                relative_bus_seat = view.findViewById(R.id.relative_seat);
               //  expandable_layout = view.findViewById(R.id.expandable_layout);
                progressB_dialog_seat = view.findViewById(R.id.progressB_dialog_seat);
                relativeL_row_voyage_view = view.findViewById(R.id.relativeL_row_voyage_view);
                relativeL_row_voyage_view_main = view.findViewById(R.id.relativeL_row_voyage_view_main);

                textView_seat2 = view.findViewById(R.id.textView_seat2);
                textView_seat5 = view.findViewById(R.id.textView_seat5);

                tvPrice = view.findViewById(R.id.tv_Price);
                textV_travel_duration_row_voyage = view.findViewById(R.id.textV_travel_duration_row_voyage);
                textV_from_time = view.findViewById(R.id.textV_from_time_row_voyage);
                imgLogo = view.findViewById(R.id.textV_company_name);
                textV_from_row_voyage = view.findViewById(R.id.textV_from_row_voyage);
                textV_to_row_voyage = view.findViewById(R.id.textV_to_row_voyage);
                text_seat = view.findViewById(R.id.text_seat);
                imageV_seat1 = view.findViewById(R.id.imageV_seat1);imageV_seat2 = view.findViewById(R.id.imageV_seat2);imageV_seat3 = view.findViewById(R.id.imageV_seat3);imageV_seat4 = view.findViewById(R.id.imageV_seat4);
                imageV_seat5 = view.findViewById(R.id.imageV_seat5);imageV_seat6 = view.findViewById(R.id.imageV_seat6);imageV_seat7 = view.findViewById(R.id.imageV_seat7);imageV_seat8 = view.findViewById(R.id.imageV_seat8);
                imageV_seat9 = view.findViewById(R.id.imageV_seat9);imageV_seat10 = view.findViewById(R.id.imageV_seat10);imageV_seat11 = view.findViewById(R.id.imageV_seat11);imageV_seat12 = view.findViewById(R.id.imageV_seat12);
                imageV_seat13 = view.findViewById(R.id.imageV_seat13);imageV_seat14 = view.findViewById(R.id.imageV_seat14);imageV_seat15 = view.findViewById(R.id.imageV_seat15);imageV_seat16 = view.findViewById(R.id.imageV_seat16);
                imageV_seat17 = view.findViewById(R.id.imageV_seat17);imageV_seat18 = view.findViewById(R.id.imageV_seat18);imageV_seat19 = view.findViewById(R.id.imageV_seat19);imageV_seat20 = view.findViewById(R.id.imageV_seat20);
                imageV_seat21 = view.findViewById(R.id.imageV_seat21);imageV_seat22 = view.findViewById(R.id.imageV_seat22);imageV_seat23 = view.findViewById(R.id.imageV_seat23);imageV_seat24 = view.findViewById(R.id.imageV_seat24);
                imageV_seat25 = view.findViewById(R.id.imageV_seat25);imageV_seat26 = view.findViewById(R.id.imageV_seat26);imageV_seat27 = view.findViewById(R.id.imageV_seat27);imageV_seat28 = view.findViewById(R.id.imageV_seat28);
                imageV_seat29 = view.findViewById(R.id.imageV_seat29);imageV_seat30 = view.findViewById(R.id.imageV_seat30);imageV_seat31 = view.findViewById(R.id.imageV_seat31);imageV_seat32 = view.findViewById(R.id.imageV_seat32);
                imageV_seat33 = view.findViewById(R.id.imageV_seat33);imageV_seat34 = view.findViewById(R.id.imageV_seat34);imageV_seat35 = view.findViewById(R.id.imageV_seat35);imageV_seat36 = view.findViewById(R.id.imageV_seat36);
                imageV_seat37 = view.findViewById(R.id.imageV_seat37);imageV_seat38 = view.findViewById(R.id.imageV_seat38);imageV_seat39 = view.findViewById(R.id.imageV_seat39);imageV_seat40 = view.findViewById(R.id.imageV_seat40);
                imageV_seat41 = view.findViewById(R.id.imageV_seat41);imageV_seat42 = view.findViewById(R.id.imageV_seat42);imageV_seat43 = view.findViewById(R.id.imageV_seat43);imageV_seat44 = view.findViewById(R.id.imageV_seat44);
                imageV_seat45 = view.findViewById(R.id.imageV_seat45);imageV_seat46 = view.findViewById(R.id.imageV_seat46);imageV_seat47 = view.findViewById(R.id.imageV_seat47);imageV_seat48 = view.findViewById(R.id.imageV_seat48);
                imageV_seat49 = view.findViewById(R.id.imageV_seat49);imageV_seat50 = view.findViewById(R.id.imageV_seat50);



            relative_bus_seat.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final int position = getAdapterPosition();
                        final Bus bus = busList.get(position);

                        final CartStateResultBus resultBus1 = Cart.getInstance(context).ifValidAddToCartBus(bus);
                        ////aynı şeye iki kere tıklanırsa
                        if (resultBus1.isSuccessBus) {
                            ////carttan siler aynı directionları ve yayın yapar result activitye
                            Cart.getInstance().addBusToCart(bus);
                            notifyDataSetChanged();
                            voyageAdapterToResultActivityListener.updateAdded();



                        } else  {                           Cart.getInstance(context).removeFromCartByDirectionBetweenSameTypesBus(bus.getDirection());
                            notifyDataSetChanged();
                            voyageAdapterToResultActivityListener.updateRemoved();

                        }

                    }
                });
            }


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



}

推荐答案

首先为ViewHolder

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 

然后将点击侦听器添加到图像视图中

Then add the click listeners to image view like this

private ViewHolder(View view) {
                super(view);

      //Your code here after

 imageV_seat1.setOnClickListener(this);
    //add for all the image views


}

然后在Override方法onClick用户的Switch语句上获得快速执行

And on Override method onClick user Switch statement to get the fast execution

public void onClick(View v) {

        switch (v.getId()) {

            case R.id.imageV_seat1:

               // Code Here
                break;

     // add for reset ids

}
}

这篇关于如何在Recyclerview中为所有imageView添加相同的onClick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆