正如您在image上看到的那样,我有一个灰色区域,其中有一些imageview。此区域是Horizontal Scrollview中的LinearLayout。此外,在每个imageview上,都有一个OnTouchListener,它在有ACTION_DOWN时开始拖放。
如您所知,我尝试滚动时出现问题。实际上,ACTION_DOWN被选中"所以我不能scroll。
所以我想到了几个解决方案:
但这些解决方案都不适合我。你知道如何解决我的问题吗?
我的xml代码:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:background="#5d6164"
android:id="@+id/horizontalScrollView" >
<LinearLayout
android:id="@+id/area2_timetable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
</LinearLayout>
</HorizontalScrollView>
我的OnTouch方法:
View.OnTouchListener myOnTouchListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action==MotionEvent.ACTION_DOWN)
{
SharedPreferences mSharedPrefs = getSharedPreferences("User", Context.MODE_PRIVATE);
if(mSharedPrefs.getInt("son_active", 0)==1) Son(VariablesManagement.nom_stockage_meal.get(v.getId()));
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
}
return false;
}
};
我不认为我的java代码很有用,所以我没有把它(为了有一个明确的问题),但不要犹豫,问你认为它是否有帮助。 / p>
非常感谢!
答案 0 :(得分:1)
您需要区分垂直滑动和水平滑动。
尝试如下:
private float y1, y2;
//Adjust this threshold as your need
private static final int MIN_DISTANCE = 20;
View.OnTouchListener myOnTouchListener = new View.OnTouchListener() {
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
y1 = event.getY();
break;
case MotionEvent.ACTION_MOVE:
y2 = event.getY();
float deltaY = y2 - y1;
if (Math.abs(deltaY) > MIN_DISTANCE) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
Toast.makeText(getActivity(), "Swiping vertically!", Toast.LENGTH_SHORT).show();
} else {
// Nothing to do
}
break;
}
return false;
}
};