【发布时间】:2017-05-25 07:58:31
【问题描述】:
我有一个问题。
我在里面使用带有 NestedScrollView 的 CoordinatorLayout。 NestedScrollView 包含 TextView + RecyclerView。我也有带有附加行为的 AppBarLayout。
当我与 RecyclerView 交互时,行为正常。但是如果我尝试与 TextView 交互,行为会导致 onStartNestedScroll(..) 然后 onStopNestedScroll(..) 和 onStartNestedScroll(..) 再次发生。
为什么会这样?如何预防?
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/aspot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:background="#39868686"
android:focusableInTouchMode="true"
android:padding="100dp"
android:text="Some text"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@color/transparent"
app:elevation="0dp"
app:layout_behavior="com.example.coordinatorbehavior.ScrollingBehavior">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="AppBarLayout"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
ScrollingBehavior 非常简单。
public class ScrollingBehavior extends CoordinatorLayout.Behavior<AppBarLayout> {
public ScrollingBehavior() {
}
public ScrollingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
boolean started = nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
Log.d("log", "onStartNestedScroll: " + started);
return started;
}
@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target) {
Log.d("log", "onStopNestedScroll");
super.onStopNestedScroll(coordinatorLayout, child, target);
}
}
【问题讨论】:
-
你找到答案了吗?
-
我记得我没有找到解决方案。我暂时不需要它
标签: java android android-coordinatorlayout