gpt4 book ai didi

java - 为什么我无法检测到 ScrollView 内的滑动?

转载 作者:行者123 更新时间:2023-11-30 02:39:33 25 4
gpt4 key购买 nike

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

我的 Activity 中的这段代码:

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 上)内部的滑动没有被检测到全部。

为什么会这样?我知道我在这里遗漏了一个极小的部分,但我现在无法发现它。你能推我一下吗?

最佳答案

你必须为 ScrollView 覆盖 onInterceptTouchEvent 它允许你将触摸事件传递给它的 subview

您可以引用:ScrollView with children view, how to intercept scroll conditionally

引用:http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29

关于java - 为什么我无法检测到 ScrollView 内的滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25971440/

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