2

布局如下:

在此处输入图像描述

View 1 是自定义布局FlipperLayout,View 2 是扩展 ListView PinnedHeaderListView,我想添加一个“拉动刷新”功能。视图 1 需要监听触摸事件才能向左或向右翻转。视图 2 也需要触摸事件。问题是,如果 View 1 消耗了触摸事件,View 2 就无法获得它。

即使在 View 1 使用后,我如何将事件发送到 View 2?

4

2 回答 2

1

考虑以下自定义布局:

class FlingLinearLayout extends LinearLayout {

    private GestureDetector mDetector;

    public FlingLinearLayout(Context context) {
        super(context);
        setOrientation(LinearLayout.VERTICAL);
        OnGestureListener listener = new SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.d(TAG, "onFling vx: " + velocityX + ", vy: " + velocityY);
                if (Math.abs(velocityX) > Math.abs(velocityY)) {
                    String text = (velocityX > 0? "left to right" : "right to left") + " fling";
                    Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show();
                    return true;
                }
                return false;
            }
        };
        mDetector = new GestureDetector(context, listener);
        setBackgroundColor(0xaaff0000);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //Log.d(TAG, "onTouchEvent " + event);
        // handle own events (initiated in red area)
        mDetector.onTouchEvent(event);
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //Log.d(TAG, "onInterceptTouchEvent " + ev);
        // handle child events
        // note: if you horizontally fling over button its onClick() is not performed
        return mDetector.onTouchEvent(ev);
    }
}

及其在您的活动中的用法:

ViewGroup ll = new FlingLinearLayout(this);
Button b;
OnClickListener l = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Button b = (Button) v;
        Log.d(TAG, "onClick " + b.getText());
    }
};
b = new Button(this);
b.setText("first");
b.setOnClickListener(l);
ll.addView(b);
b = new Button(this);
b.setText("second");
b.setOnClickListener(l);
ll.addView(b);
b = new Button(this);
b.setText("third");
b.setOnClickListener(l);
ll.addView(b);
setContentView(ll);
于 2013-06-08T04:22:31.743 回答
0

触摸事件返回一个布尔值,指定事件是否被消费,只返回false,事件将继续隧道到子视图

于 2013-06-07T08:17:45.343 回答