RecyclerView onClick不起作用

时间:2017-07-31 15:33:17

标签: android android-recyclerview

我正在尝试通过Recyclerview项目点击启动自定义对话框,但没有任何反应。我正在为Recyclerview使用自定义onClick功能。这是我的代码:

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(context, recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setView(getLayoutInflater().inflate(R.layout.edit_dialog, null));
                        builder.setTitle("Edit Item");
                        builder.setNeutralButton("TEST", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.d("TEST","Test button was clicked in AlertDialog");
                            }
                        });
                        builder.setCancelable(true);
                        builder.show();
                    }

                    @Override public void onLongItemClick(View view, int position) {}
                })

起初,我认为问题在于我没有为对话框设置按钮,但即使设置了中性按钮,它仍然无效。以下是我对Recyclerview的onClick功能:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {return false;}

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {}

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onLongItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {

        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onSingleTapUp(MotionEvent e) {return true;}

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && mListener != null) {
                    mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }
}

这是我的适配器:

public class CustomCursorAdapter extends RecyclerView.Adapter<CustomCursorAdapter.TaskViewHolder>{

private Cursor cursor;
private Context con;

public CustomCursorAdapter(Context context) {
    this.con = context;
}



@Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(con).inflate(R.layout.task_layout, parent, false);
    return new TaskViewHolder(view);
}

@Override
public void onBindViewHolder(TaskViewHolder holder, int position) {

    int index = cursor.getColumnIndex(TaskContract.TaskEntry._ID);
    int descriptionOfIndex = cursor.getColumnIndex(TaskContract.TaskEntry.COLUMN_DESCRIPTION);
    int priorityofIndex = cursor.getColumnIndex(TaskContract.TaskEntry.COLUMN_PRIORITY);

    cursor.moveToPosition(position);

    final int id = cursor.getInt(index);
    String description = cursor.getString(descriptionOfIndex);
    int priority = cursor.getInt(priorityofIndex);

    holder.itemView.setTag(id);
    holder.taskDescriptionView.setText(description);

    String priorityString = "" + priority;
    holder.priorityView.setText(priorityString);

    GradientDrawable priorityCircle = (GradientDrawable) holder.priorityView.getBackground();
    int priorityColor = getPriorityColor(priority);
    priorityCircle.setColor(priorityColor);

}

private int getPriorityColor(int priority) {
    int priorityColor = 0;

    switch(priority) {
        case 1: priorityColor = ContextCompat.getColor(con, R.color.materialRed);
            break;
        case 2: priorityColor = ContextCompat.getColor(con, R.color.materialOrange);
            break;
        case 3: priorityColor = ContextCompat.getColor(con, R.color.materialYellow);
            break;
        default: break;
    }
    return priorityColor;
}

@Override
public int getItemCount() {
    if (cursor == null) {
        return 0;
    }
    return cursor.getCount();
}

public Cursor swapCursor(Cursor c) {
    if (cursor == c) {
        return null;
    }
    Cursor temp = cursor;
    this.cursor = c;

    if (c != null) {
        this.notifyDataSetChanged();
    }
    return temp;
}

class TaskViewHolder extends RecyclerView.ViewHolder {

    TextView taskDescriptionView;
    TextView priorityView;

    public TaskViewHolder(View itemView) {
        super(itemView);

        taskDescriptionView = (TextView) itemView.findViewById(R.id.taskDescription);
        priorityView = (TextView) itemView.findViewById(R.id.priorityTextView);
    }
}

}

1 个答案:

答案 0 :(得分:1)

我确信您可以更轻松地在onBindViewHolder内设置点击监听器。 containerView - 从您的观看者那里获取此视图

    @Override
    public void onBindViewHolder(TaskViewHolder holder, int position) {
            containerView.setOnClickListener(new DefaultInterfaceImplUtils.ClickListener() {
                @Override
                public void onViewClicked(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setView(getLayoutInflater().inflate(R.layout.edit_dialog, null));
                    builder.setTitle("Edit Item");
                    builder.setNeutralButton("TEST", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Log.d("TEST", "Test button was clicked in AlertDialog");
                        }
                    });
                    builder.setCancelable(true);
                    builder.show();
                }
            }
        });
    }