奇怪的RecyclerView行为onItemTouchListener

时间:2016-12-21 11:38:02

标签: android android-recyclerview onitemclick

我正在尝试在RecyclerView上添加RecyclerItemClickListener。它运行正常,我的RecyclerView中的两个组件需要双击才能执行onClick操作。

这是我的MainActivity中的recyclerView.addOnItemTouchListener:

recyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(this, recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, final int pos) {
                    TextView sourceName = (TextView) view.findViewById(R.id.sourceName);
                    ImageView moreSettings = (ImageView) view.findViewById(R.id.moreSettings);

                    // Add on click listeners
                    sourceName.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            initiateDialogAlert(passObjArrayList.get(pos).getName(), passObjArrayList.get(pos).getPassword());
                            boiteAlerte.show();
                        }
                    });

                    moreSettings.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            createPopupMenu(view, pos);
                        }
                    });                        
                }
            });

如何防止需要在组件上单击两次以执行其操作的此行为?

我的RecyclerItemClickListener:

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

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

@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        return true;
    }
    return false;
}



public static boolean isViewClicked(View view, MotionEvent e) {
    Rect rect = new Rect();
    view.getGlobalVisibleRect(rect);
    return rect.contains((int) e.getRawX(), (int) e.getRawY());
}

@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

1 个答案:

答案 0 :(得分:2)

看看onTouch和onClick之间存在差异。对于您在处理onClick时的recyclerview,您必须明智地编写代码。

在默认情况下,Recyclerview不向我们提供onClickListener的功能。因此,您可以创建一个界面并执行它。

请参阅以下示例,使用onClickListener进行recyclerview。

https://stand2code.blogspot.in/2016/12/recyclerview-item-onclicklistener.html

当您的视图被单击时,您将返回true。确保这是问题的制造者吗?

如果您想对onClicks的不同视图执行不同的操作。尝试不同的接口。它会帮助你造成问题。

示例代码:

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

    public interface OnItemClickListener {
        void onItemClick(ContentItem item);
    }

    private final List<ContentItem> items;
    private final OnItemClickListener listener;

    public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
        this.items = items;
        this.listener = listener;
    }

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

    @Override public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(items.get(position), listener);
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            image = (ImageView) itemView.findViewById(R.id.image);
        }

        public void bind(final ContentItem item, final OnItemClickListener listener) {
            name.setText(item.name);
            Picasso.with(itemView.getContext()).load(item.imageUrl).into(image);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    listener.onItemClick(item);
                }
            });
        }
    }
}