0

我試圖對滾動事件(!)進行操作(嵌套)滾動視圖。CoordinatorLayout + NestedScrolling Grand-Child的自定義行爲

我有一個滾動視圖內的項目,我想「偷看」(翻譯成可見的屏幕區域)幾秒,如果它最初不可見在滾動視圖/屏幕。但只要容器滾動,我想隱藏它(翻譯回原點)。

因此,我試圖使用CoordinatorLayout行爲不緊密耦合scrollView和我(自定義)視圖。

以下the article,我試着編寫一個自定義行爲,但想通了,我不能在我的視圖上應用此行爲,因爲它不是CoordinatorLayout的子項。如果我必須將其設置在一個直接的孩子身上,那麼我如何才能對這位曾祖父的孩子採取行動?

這怎麼可能實現?

佈局看起來某事像那:

<CoordinatorLayout> 
    <NestedScrollView> 
     <LinearLayout> 
      ... 
      <MyView/> 
     <LinearLayout/> 
    </NestedScrollView> 
</CoordinatorLayout> 

的行爲是非常直截了當:

 
public class MyBehaviour extends CoordinatorLayout.Behavior{ 

    ... 

    @Override 
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int 
      nestedScrollAxes) { 
     return true; 
    } 

    @Override 
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int 
      dxUnconsumed, int dyUnconsumed) { 
     super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); 

     // act on MyView 
    } 
} 

回答

0

繼文章,我試着寫一個自定義的行爲,但想通了,那我不能將此行爲應用於我的視圖,因爲它不是CoordinatorLayout的子項。

這並不完全正確。可以模擬視圖對CoordinatorLayoutBehaviour變化作出反應的情況。下面是一個簡單的例子:

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CoordinatorLayout 
     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"> 

      <android.support.design.widget.CoordinatorLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="1700px" 
        android:text="Hello World!"/> 

       <android.support.design.widget.FloatingActionButton 
        android:id="@+id/fab" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom|end" 
        android:layout_margin="@dimen/fab_margin" 
        android:src="@android:drawable/ic_dialog_email"/> 

      </android.support.design.widget.CoordinatorLayout> 

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

    </android.support.design.widget.CoordinatorLayout> 

</android.support.design.widget.CoordinatorLayout> 

正如你可以看到FloatingActionButton裏面

CoordinatorLayout - >CoordinatorLayout - >NestedScrollView - >CoordinatorLayout

在這樣的佈局FAB反作用於Snackbar顯示:

enter image description here enter image description here

但是,這裏的問題是與佈局高度相結合,所以我強烈建議你重新考慮你的設計

+0

我無法確認您的答案。我在這裏上傳了一個演示項目:https://github.com/chriswiesner/CoordinatorLayoutNestedScrolling 如果我把「MyView」作爲根協調器佈局的子項,它工作正常。但只要它在嵌套coordinatorLayout中,它不起作用 在此期間,我解決了我的問題,沒有coordinatorlayout並將scrollListener傳遞給MyView。這再次結合我的觀點,我想避免。所以我仍然很好奇這是如何通過CoordinatorLayout實現的 – cwiesner

相關問題