Onclicklistener没有处理recyclerview

时间:2015-08-14 07:58:13

标签: android onclicklistener android-recyclerview

我正在使用recyclerview,下面给出的相同代码对我来说工作正常但是 我试过以下代码,但它无法正常工作

recyclerView = (RecyclerView)v.findViewById(R.id.datelist);
    recyclerView.setClickable(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);
    adapter.setOnItemClickListener(this);
适配器中的

public  class UpdateHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener,CompoundButton.OnCheckedChangeListener {
        TextView uname;
        TextView uversn;
        TextView usitting;


        public UpdateHolder(View itemView) {
            super(itemView);
            uname = (TextView) itemView.findViewById(R.id.update_name);
            uversn = (TextView) itemView.findViewById(R.id.update_version);
            usitting=(TextView) itemView.findViewById(R.id.sitting_type);

            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            myClickListener.onItemClick(getPosition(), v);
        }
}
public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

我也在onCreateViewHolder

中尝试了适配器

请帮帮我

2 个答案:

答案 0 :(得分:13)

对我来说,解决方法是从CardView xml文件中删除以下内容。

android:clickable="true"

显然,此属性会覆盖添加到视图中的所有clickListeners。

答案 1 :(得分:3)

在recyclerView中你必须像下面的代码一样创建viewHolder,你必须在viewHolder View上设置click监听器,如下所示。

public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        private final MyEditText edtTest;

        public ViewHolder(View v) {
            super(v);
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "Element " + getPosition() + " clicked.");
                }
            });
            v.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Log.e("Demo", "Long click done bro--------------------->");
                    return false;
                }
            });
            textView = (TextView) v.findViewById(R.id.textView);
            edtTest = (MyEditText) v.findViewById(R.id.edtTest);
        }

        public TextView getTextView() {
            return textView;
        }

        public EditText getEdtTest() {
            return edtTest;
        }
    }