1

我正在尝试捕获ImageView包含在RecyclerView项目内的点击。我已经实现RecyclerView.OnItemTouchListener并让手势检测器处理正常点击和长按RecyclerView项目的运动事件。

由于我希望使用相同的触摸框架来处理RecylcerView项目内子视图上的触摸事件,因此我也设置View.OnTouchListener了图像视图,覆盖了onTouch返回 true 的实现,希望触摸在ImageView被点击时被消耗。

我故意onInterceptTouchEvent总是从 中返回 false RecyclerView,希望子视图首先处理事件,如果它们都没有消耗,那么RecyclerView的项目视图应在处理期间处理点击的手势事件onTouchEvent

有了上述理解,我可以ImageView在点击它时接收到触摸事件。

问题是,当我点击RecyclerView外面的项目时ImageViewRecyclerView'sonTouchEvent根本没有被调用。我对触摸框架的理解是,如果子视图不处理触摸事件,则应该有机会处理相同的RecyclerView事件。onTouchEvent然而,这不是我能够实现的。

谁能建议背后发生了什么以及处理上述点击事件的正确方法是什么?

4

1 回答 1

4

这将解决您的问题:

public class OnItemClickListener implements View.OnClickListener {
    private int position;
    private OnItemClickCallback onItemClickCallback;

    public OnItemClickListener(int position, OnItemClickCallback onItemClickCallback) {
        this.position = position;
        this.onItemClickCallback = onItemClickCallback;
    }

    @Override
    public void onClick(View view) {
        onItemClickCallback.onItemClicked(view, position);
    }

    public interface OnItemClickCallback {
        void onItemClicked(View view, int position);
    }
}

获取“OnItemClickCallback”接口的实例并将其放入您的活动或片段中:

private OnItemClickListener.OnItemClickCallback onItemClickCallback = new OnItemClickListener.OnItemClickCallback() {
    @Override
    public void onItemClicked(View view, int position) {
        // switch(view.getId()) ...
    }
};

然后,将该回调传递给您的 recyclerView:

recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(Arrays.asList("1", "2", "3"), onItemClickCallback));

最后,这将是您的适配器:

public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
    private List<String> mValues;
    private OnItemClickListener.OnItemClickCallback onItemClickCallback;

    public SimpleStringRecyclerViewAdapter(List<String> items, OnItemClickListener.OnItemClickCallback onItemClickCallback) {
        mValues = items;
        this.onItemClickCallback = onItemClickCallback;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mTextView = (TextView) view.findViewById(R.id.txt_title);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mTextView.setText(mValues.get(position));
        holder.mView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
        holder.mTextView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }
}
于 2015-08-17T13:15:34.767 回答