Java CoordinatorLayout.行为导致onStartNestedScroll(..)两次
我有个问题 我使用内部嵌套滚动视图的CoordinatorLayout。NestedScrollView包含TextView+RecyclerView。我也有附加行为的AppBarLayout 当我与RecyclerView互动时,行为表现良好。但如果我尝试与TextView交互,行为会再次导致onStartNestedScroll(..)然后onStopNestedScroll(..)和onStartNestedScroll(..) 为什么会发生这种情况?我怎样才能预防它Java CoordinatorLayout.行为导致onStartNestedScroll(..)两次,java,android,android-coordinatorlayout,Java,Android,Android Coordinatorlayout,我有个问题 我使用内部嵌套滚动视图的CoordinatorLayout。NestedScrollView包含TextView+RecyclerView。我也有附加行为的AppBarLayout 当我与RecyclerView互动时,行为表现良好。但如果我尝试与TextView交互,行为会再次导致onStartNestedScroll(..)然后onStopNestedScroll(..)和onStartNestedScroll(..) 为什么会发生这种情况?我怎样才能预防它 <androi
<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>
滚动行为非常简单
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);
}
公共类滚动行为扩展了CoordinatorLayout.Behavior{
公共滚动行为(){
}
公共滚动行为(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
公共布尔值onStartNestedScroll(CoordinatorLayout CoordinatorLayout,AppBarLayout子级,View directTargetChild,View target,int nestedScrollAxes){
boolean start=nestedScrollAxis==viewcompt.SCROLL\u AXIS\u VERTICAL;
Log.d(“Log”,“onStartNestedScroll:+已启动”);
返回开始;
}
@凌驾
公共无效onStopNestedScroll(CoordinatorLayout CoordinatorLayout,AppBarLayout子级,视图目标){
Log.d(“Log”、“onStopNestedScroll”);
super.onStopNestedScroll(坐标布局、子对象、目标);
}
}将属性
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
添加到NestedScrollView
更新布局如下:
<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.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">
<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.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<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.CoordinatorLayout>
这不是我的决定,因为我使用自定义行为。我对AppBarLayout有不同的行为。如您所见,当我从标题中获取滚动时,它会出现故障,因为在已登录(..)事件中存在一些间隙。您是否尝试过将app:layout_behavior=“@string/appbar_scrolling_view_behavior”应用到您自己的布局中的NestedScrollView?您是否找到了此问题的答案?我申请了此项。我记得当时没有找到解决方案。我现在不需要它