在没有可点击布局的情况下获得波纹/选择器效果

时间:2015-12-03 07:16:01

标签: android android-viewpager ontouchlistener android-selector

我有两个视图寻呼机,一个嵌套在另一个。为了获得正确的行为(在不更改外部的情况下滑动内部视图寻呼机),我必须覆盖内部视图寻呼机onTouchListener并将所有onTouch / onClick逻辑放入其中(从here得到了想法) 。

一切正常,但由于我没有onClickListener,我失去了选择器效果。当我在布局元素上放置android:clickable="true"时,我得到了我的选择器效果,但视图寻呼机行为再次出错。

有没有办法从onTouchListener

中获得选择器效果
innerPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (gestureDetector.onTouchEvent(event)) {
                    Log.d(DEBUG_LOG, "Single tap.");
                    return true;
                } else if(event.getAction() == MotionEvent.ACTION_DOWN && v instanceof ViewGroup) {
                    ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
                }

                return false;
            }
        });

1 个答案:

答案 0 :(得分:3)

This为我解决了这个问题。刚刚将以下代码添加到我的OnTouchListener,并将卡片视图替换为我的内部视图寻呼机当前项目:

// Since the host view actually supports clicks, you can return false
// from your touch listener and continue to receive events.
myTextView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent e) {
    // Convert to card view coordinates. Assumes the host view is
    // a direct child and the card view is not scrollable.
    float x = e.getX() + v.getLeft();
    float y = e.getY() + v.getTop();

    // Simulate motion on the card view.
    myCardView.drawableHotspotChanged(x, y);

    // Simulate pressed state on the card view.
    switch (e.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            myCardView.setPressed(true);
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            myCardView.setPressed(false);
            break;
    }

    // Pass all events through to the host view.
    return false;
}
});