android - RecycleView 内的按钮在单击时没有反应

标签 android android-recyclerview

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

下面是一段如何在 Activity 类和 Adapter 中创建 RecycleView 的代码。

Activity 类别:

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

最佳答案

我通常使用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
       }

  });
}

关于android - RecycleView 内的按钮在单击时没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43457685/

相关文章:

android - 检测 fragment 何时通过选项卡单击变得可见

java - 如果选中前一个复选框(其他复选框首先被禁用),如何在回收器 View 中启用其他复选框?

android - 在 FrameLayout 中的 RecyclerView 上方展开 View

java - 方法 getItemCount 中出现空指针异常

android - 如果每个项目有 2 个 ID,如何为 RecyclerView 生成唯一 ID?

java - 我的适配器做错了什么?

java - libgdx 中的居中对齐文本

android - Google Play 管理中心 - 更改不良行为阈值

android - 当在 onHandleIntent 中创建处理程序时,使用处理程序发布的来自 IntentService 的 Toast 不显示

android - Groupie RecyclerView单击特定项目