Java 为什么我能';在scrollview中检测不到滑动?
我的xml中有以下内容:Java 为什么我能';在scrollview中检测不到滑动?,java,android,android-activity,android-scrollview,Java,Android,Android Activity,Android Scrollview,我的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="
<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>
}
现在,当用户从左向右滑动时,或者按oposite方式时,应该会更改屏幕上的文本。问题是,只有在ìmageView
上执行时才会检测到滑动,我的意思是滚动视图
(在实际文本视图上)内部的滑动根本没有检测到
为什么会这样?我知道我;我遗漏了一个非常小的部分,但我现在无法发现。您可以推我一下吗?您必须为scrollview覆盖onInterceptTouchEvent,它允许您将触摸事件传递给其子视图 您可以参考: 参考:
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();
}
}