RecycleView中的按钮在点击时没有反应

时间:2017-04-17 18:55:14

标签: android android-recyclerview

我在RecycleView内创建了一个LinearLayout组件以及其他一些组件。问题是每当我点击RecycleView组件的元素时它应该在控制台中创建TAG,但事实并非如此。我添加了onClickListiner但它根本没有反应。

下面是一段代码,我如何在RecycleView课程和Activity内创建Adapter

活动类:

  mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mAdapter = new MyAdapter(washLocations, this);
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));    

适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        private List<WashLocation> washLocations;
        private LayoutInflater layoutInflater;

        public MyAdapter(List<WashLocation> washLocations, Context context) {
            layoutInflater = LayoutInflater.from(context);
            this.washLocations = washLocations;
        }

        @Override
        public MyAdapter.ViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);

            // Inflate the custom layout
            View contactView = inflater.inflate(R.layout.row_recycleview, parent, false);

            // Return a new holder instance
            ViewHolder viewHolder = new ViewHolder(context, contactView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
            // Get the data model based on position
            WashLocation w = washLocations.get(position);
            String info = w.getWashName();

            // Set item views based on your views and data model
            TextView textView = holder.info;
            textView.setText(info);

            Integer fav = w.getFav();
            Boolean favorite = fav == 1 ? true : false;
            Button addToFav = holder.favorite;
            addToFav.setText(favorite == true ? "Usuń z ulubionych" : "Dodaj do ulubionych");
        }

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


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

            private Context context;
            public TextView info;
            public Button favorite;

            public ViewHolder(Context context, View itemView) {
                super(itemView);
                itemView.setOnClickListener(this);
                this.context = context;
                info = (TextView) itemView.findViewById(R.id.textView);
                favorite = (Button) itemView.findViewById(R.id.addToFav);
            }

            @Override
            public void onClick(View v) {
                int position = getAdapterPosition(); // gets item position
                if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
                    // We can access the data within the views
                    Toast.makeText(context, info.getText(), Toast.LENGTH_SHORT).show();
                    Log.d("myTag", "This is my message");
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

您正在ViewGroup上为元素设置onClickListener。你应该把它放在按钮上。

尝试使用:

public ViewHolder(Context context, View itemView) {
    super(itemView);
    this.context = context;
    info = (TextView) itemView.findViewById(R.id.textView);
    favorite = (Button) itemView.findViewById(R.id.addToFav);
    favorite.setOnClickListener(this);
}

答案 1 :(得分:1)

我通常使用onBindViewHolder设置Listener,因为在此方法中我们有位置。尝试将您的方法更改为:

@覆盖

public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
    // Get the data model based on position
   final WashLocation w = washLocations.get(position);
   final String info = w.getWashName();

    // Set item views based on your views and data model
    TextView textView = holder.info;
    textView.setText(info);

    Integer fav = w.getFav();
    Boolean favorite = fav == 1 ? true : false;
    Button addToFav = holder.favorite;
    addToFav.setText(favorite == true ? "Usuń z ulubionych" : "Dodaj do ulubionych");
  holder.setOnClickListener(new View.OnClickListener(){

       @Override
       public void onClick(View view){
        //Here you have all the data that you want
        //you can use w variable : w.getWashName();
       }

  });
}

为每个按钮添加2个不同的侦听器:

holder.yourButton1.setOnClickListener(new View.OnClickListener(){

       @Override
       public void onClick(View view){
       //Click for button 1
       }

  });
}

holder.yourButton2.setOnClickListener(new View.OnClickListener(){

       @Override
       public void onClick(View view){
       //Click for button 2
       }

  });
}

答案 2 :(得分:1)

更改您的观看者代码,

public ViewHolder(Context context, View itemView) {
                super(itemView);
                this.context = context;
                info = (TextView) itemView.findViewById(R.id.textView);
                favorite = (Button) itemView.findViewById(R.id.addToFav);
                favorite2 = (Button) itemView.findViewById(R.id.addToFav2);
                favorite.setOnClickListener(this);
                favorite2.setOnClickListener(this);
            }
       @Override
       public void onClick(View view){
            switch(view.getId()) {
                 case R.id.addToFav :
                      // onclick of button 1
                      break;
                 case R.id.addToFav2 :
                      // onclick of button 1
                      break;
            }
       }