我想向ScrollView添加滑动功能(左侧和左侧的严格),但不会拦截所有触摸事件,因此子按钮仍然可以点击。
我在滚动视图中添加了触摸侦听器,如下所示:
this.getView().findViewById(R.id.scrollView1).setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeTop() {
Toast.makeText(DetailFragment.this.getActivity(), "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(DetailFragment.this.getActivity(), "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(DetailFragment.this.getActivity(), "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(DetailFragment.this.getActivity(), "bottom", Toast.LENGTH_SHORT).show();
}
});
如果我喜欢它,那么我的触摸听众永远不会被调用。所以我必须继承ScrollView
并覆盖onInterceptTouchEvent
,这样滚动视图将决定它想拦截哪些触摸事件:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercept = false;
final int action = ev.getAction();
ViewConfiguration vc = ViewConfiguration.get(this.getContext());
int slop = vc.getScaledTouchSlop();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP: {
float currX = ev.getX();
if (Math.abs(currX - originalX) > slop) {
intercept = true;
}
break;
}
case MotionEvent.ACTION_DOWN: {
originalX = ev.getX();
break;
}
}
return (super.onInterceptTouchEvent(ev) || intercept);
}
所以我的想法是只截取左/右滑动,所有其他事件应留给儿童观看。滚动视图中的按钮似乎有效,但我的听众从未被调用过。
我在xml中的视图如下所示:
<com.damluar.mobile.inbox.MyScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:id="@+id/detailLayout">
<LinearLayout
android:id="@+id/detailButtonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:background="@color/default_color">
</LinearLayout>
</LinearLayout>
</com.damluar.mobile.inbox.MyScrollView>
答案 0 :(得分:1)
请检查并标记是否有用。 我已经更新了OnSwipeTouchListener以获得onTouch的回调。
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector = new GestureDetector(
new GestureListener());
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 50;
private static final int SWIPE_VELOCITY_THRESHOLD = 50;
private static final int SWIPE_DISTANCE = 50;
private boolean isFlingCall = false;
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
isFlingCall = false;
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e){
if(!isFlingCall) {
onTouch();
return false;
}
else {
return true;
}
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD
&& Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > SWIPE_DISTANCE) {
isFlingCall = true;
onSwipeRight();
return true;
}
else {
onSwipeLeft();
return true;
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
onTouch();
return true;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onTouch() {
}
}
由于
JRH