我的xml中有这个:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/jokeIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/app_name"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/jokeIcon" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/allJokesTxt"
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:lineSpacingMultiplier="1.2"
android:padding="16dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
和我的活动中的代码:
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE) {
changeText();
} else {
// consider as something else - a screen tap for example
}
break;
}
return super.onTouchEvent(event);
}
private void changeText(){
// Left to Right swipe action
if (x2 > x1) {
if (jokesCounter > 0) {
--jokesCounter;
currentJoke.setVisibility(View.VISIBLE);
currentJoke.setText(jokesCollector.get(jokesCounter));
} else {
Context context = getApplicationContext();
CharSequence text = "xxx";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
// Right to left swipe action
else {
if (jokesLengthCounter >= jokesCounter) {
++jokesCounter;
currentJoke.setVisibility(View.VISIBLE);
currentJoke.setText(jokesCollector.get(jokesCounter));
} else {
Context context = getApplicationContext();
CharSequence text = "xxx";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
现在假设当用户从左向右滑动或以相反的方式切换屏幕上的文本。问题是只有在ìmageView
上完成滑动时才会检测到滑动,我的意思是根本没有检测到ScrollView
内部的滑动(在实际的textView上)。
为什么会这样?我知道我在这里错过了一小部分,但我现在无法发现它。你能帮我推吗?
答案 0 :(得分:1)
你必须覆盖onInterceptTouchEvent for scrollview,它允许你将触摸事件传递给它的子视图
您可以参考:ScrollView with children view, how to intercept scroll conditionally