我有两个 View 寻呼机,一个嵌套在另一个中。为了获得正确的行为(滑动内部 View 寻呼机而不更改外部 View 寻呼机),我必须重写内部 View 寻呼机 onTouchListener
并将所有 onTouch/onClick 逻辑放入其中(从 here 获得想法) )。
工作一切正常,但由于我不再有 onClickListener
我失去了选择器效果。当我将 android:clickable="true"
放在布局元素上时,我得到了选择器效果,但 View 寻呼机行为再次错误。
有没有办法通过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;
}
});
最佳答案
This为我解决了。只需将以下代码添加到我的 OnTouchListener
中,并将卡片 View 替换为我的内部 View 寻呼机当前项目:
// 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;
}
});
关于android - 在没有可点击布局的情况下获得波纹/选择器效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34059912/