gpt4 book ai didi

android - 同步 ScrollView 和 NestedScrollView

转载 作者:行者123 更新时间:2023-11-29 01:25:20 26 4
gpt4 key购买 nike

我有以下布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedscrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/inner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<NESTED VIEWS>

</LinearLayout>
</android.support.v4.widget.NestedScrollView>

<LinearLayout
android:id="@+id/outer_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<OUTER VIEWS>

</LinearLayout>
</LinearLayout>
</ScrollView>

我的问题是我希望 ScrollView 先滚动,如果 ScrollView 移动了一点点,否则 NestedScrollView 可以消耗触摸。目前,NestedScrollView 获取触摸事件并仅在 ScrollView 接收到触摸之后才使用滚动。我试过使用 onInterceptTouchEvent 并进行了试验,但无济于事。有什么指点吗?

这是正确的方法还是我应该使用其他 View 组合? (可能是协调器布局?)

最佳答案

所以我扩展了 ScrollView 并让它按如下方式工作:

private static final int SCROLL_THRESHOLD = 10;

private boolean mScrolling;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (getScrollY() > SCROLL_THRESHOLD) {
mScrolling = true;
onTouchEvent(ev);
return false;
} else if (mScrolling) {
mScrolling = false;
return false;
}
if (ev.getActionMasked() == MotionEvent.ACTION_UP) {
mScrolling = false;
}
return super.onInterceptTouchEvent(ev);
}

适合我。如果有人有更好的解决方案,请告诉我。

关于android - 同步 ScrollView 和 NestedScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34430584/

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