2017-05-25 6 views
0

問題があります。CoordinatorLayout.BehaviorによってonStartNestedScroll(..)が2回発生します

私はCoordinatorLayoutをNestedScrollViewと共に使用します。 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> 

スクロールビヘイビアは非常に簡単です。

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); 
} 

}

答えて

0

NestedScrollViewに属性app:layout_behavior="@string/appbar_scrolling_view_behavior"を追加します。

は、以下のようにレイアウトを更新します。

<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> 
+0

を私はカスタム動作を使用するので、それは私のための決断ではありません。私はAppBarLayoutの動作が異なります。 https://www.dropbox.com/s/ukvuln3e0u01doc/device-2017-05-25-140824.mp4?dl=0 ご覧のとおり、ヘッダーからスクロールすると、 onNested(..)イベントの一部のギャップ – MonStar

+0

あなた自身のレイアウトでNestedScrollViewにapp:layout_behavior = "@ string/appbar_scrolling_view_behavior"を試してみましたか? – FAT

+0

はい、試しました – MonStar

関連する問題