RecyclerView的孩子onTouchEvent

时间:2015-01-13 16:18:22

标签: android ontouchevent android-recyclerview

我正在使用RecyclerView,最近我在应该打开PopoupWindow的每一行上添加了一个3点菜单。问题是我无法处理3点菜单图标上的触摸,因为该行消耗了触摸事件。我正在搞乱onInterceptTouchEvent,但我找不到一种方法来检测菜单上的触摸..

这基本上是我的RecyclerView.OnItemTouchListener代码:

    public class RecyclerItemTouchListener implements RecyclerView.OnItemTouchListener {


    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {

        View childView = view.findChildViewUnder(e.getX(), e.getY());

        //need a way to detect if the user touches the menu icon!!
        boolean touchOccurredOnMenu = ?????;

        if(childView != null && touchOccurredOnMenu) {

            //here the menu will handle the touch event
            return false;

        }


        //here the recycler item will handle the touch event
        return true;

    }

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

        Toast.makeText(getApplicationContext(), "touch on the row", Toast.LENGTH_SHORT).show();


    }
}

我在RecyclerView.Adapter的onBindViewHolder方法中为菜单图标设置了touchListener,如下所示:

holder.overflowMenuIcon.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        Toast.makeText(getApplicationContext(), "touch on the menu!!", Toast.LENGTH_SHORT).show();

        return false;
    }
});

1 个答案:

答案 0 :(得分:0)

向RecyclerView的行添加溢出按钮时遇到了同样的问题。诀窍是,在你的手势检测器中,你获得了被点击的视图的视图,计算按钮的命中矩形坐标("溢出"在我的情况下),如果命中矩形包含事件坐标,您使用事件,否则您点击行。

mRecyclerView.addOnItemTouchListener(mTouchListener);
mGestureDetector = new GestureDetectorCompat(getActivity(), new RecyclerViewOnGestureListener());

private final RecyclerView.OnItemTouchListener mTouchListener = new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
        return false;
    }

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

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

private class RecyclerViewOnGestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        View view = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
        RecyclerView.ViewHolder viewHolder = view != null ? mRecyclerView.getChildViewHolder(view) : null;
        View overflow = viewHolder != null ? ((DataViewHolder) viewHolder).mOverflow : null;
        if (overflow != null) {
            Rect rect = new Rect();
            overflow.getHitRect(rect);
            rect.top += view.getTop();
            rect.bottom += view.getTop();
            int x = (int) (e.getX() + .5f);
            int y = (int) (e.getY() + .5f);
            if (!rect.contains(x, y)) {
                onClick(view);
            }
            // else event consumed by overflow button
        } else {
            onClick(view);
        }
        return super.onSingleTapConfirmed(e);
    }

    public void onLongPress(MotionEvent e) {
    }
}