如何在Recyclerview中为所有imageView添加相同的onClick?

时间:2019-07-10 07:06:54

标签: java android android-recyclerview onclicklistener

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

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

我尝试添加此代码并实现onClick方法,但它没有执行onClick内部的功能。

我想将relative_bus_seat onClick的操作与此50 imageView的操作相同。

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



}

5 个答案:

答案 0 :(得分:1)

要使代码变小,请使用lambda

holder.yourImage.setOnClickListener(view -> {
    // do something here
});

答案 1 :(得分:1)

您可以实现onCLickListener并在onClick()方法中检查其imageView,如果其imageView您可以编写逻辑。

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

@Override
public void onClick(View view) {
    if(view instanceof ImageView)
   //write your logic here or you can get view.getId method also to write logic for individual imageview
}

答案 2 :(得分:1)

这可以使用一些逻辑游戏来实现。尽管我没有进行测试,因为我现在没有可用的IDE,但是代码应该可以工作。

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    private Button mButton; 

     private ViewHolder(View view)
    { 
        .....Your existing code

        button1.setOnClickListener(this); 
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        .
        .
        .
        button50.setOnClickListener(this);
    } 

    @Override
    public void onClick(View view) 
    { 
        switch (view.getId()) { 
        case R.id.button1: 
        case R.id.button2:
        case R.id.button3:
        .
        .
        .
        case R.id.button50:
            // Do something... by doing this all case will come to this block to execute.
            // Remember, do not use break in any of the above case.
        } 
    } 
}

让我知道它是否解决了您的问题。

答案 3 :(得分:1)

首先为您的ViewHolder

实现接口 OnClickListener
 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 

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

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

      //Your code here after

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


}

然后使用覆盖方法onClick的用户Switch语句快速执行

public void onClick(View v) {

        switch (v.getId()) {

            case R.id.imageV_seat1:

               // Code Here
                break;

     // add for reset ids

}
}

答案 4 :(得分:0)

只需这样做:-

onSubmitTribeData() {
  console.log(this.tribeForm.value);
}

addTribe(){
  if (this.showForm === false) {
    this.showForm = true;
    this.tribeForm.controls['dtcLocation'].disable(); 
    this.tribeForm = this.fb.group({
      tribeName: [''],
      tribeLeader: [''],
      location: [''],
      tribeSquad: ['']
    });
  } else {
    this.showForm = false;
  }
}