gpt4 book ai didi

android - 在 ScrollView 中实现向左/向右滑动

转载 作者:行者123 更新时间:2023-11-30 03:13:25 24 4
gpt4 key购买 nike

我想向 ScrollView 添加滑动功能(从右到左和从左到右),但不拦截所有触摸事件,这样子按钮仍然可以点击。

我像这样向 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,这样 ScrollView 将决定它要拦截哪些触摸事件:

@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);
}

所以我的想法是只拦截向左/向右滑动,所有其他事件应该留给 subview 。 ScrollView 中的按钮似乎可以工作,但我的监听器无论如何都不会被调用。

我在 xml 中的 View 如下所示:

<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>

最佳答案

请检查并标记它是否有用。我已经稍微更新了 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() {
}
}

谢谢

JH

关于android - 在 ScrollView 中实现向左/向右滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20549073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com