36

我想使用最新的設計庫,使我的工具欄隱藏/滾動顯示。我的問題是滾動的內容,我在片段中,我只是將其注入到FrameLayout容器,它不起作用。這裏是我的活動:AppBarLayout與FrameLayout容器作爲滾動內容不起作用

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <include layout="@layout/layout_toolbar" /> 

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

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

<FrameLayout 
    android:id="@+id/navigation_drawer_container" 
    android:layout_width="@dimen/navigation_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    tools:layout="@layout/fragment_nav_drawer_anon" /> 

和我的片段:

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

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/pull_to_refresh" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</android.support.v4.widget.SwipeRefreshLayout> 

<TextView 
    android:id="@android:id/empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:textSize="18sp" 
    android:fontFamily="sans-serif" 
    android:color="@color/dark_grey" 
    android:padding="60dp"/> 

和工具欄:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="?attr/colorPrimary" 
android:minHeight="?attr/actionBarSize" 
app:layout_scrollFlags="scroll|enterAlways" 
style="@style/Widget.MyApp.ActionBar" /> 

我下面的官方docdemo,仍然無法弄清楚如何使其工作。

+0

您能否提供更多信息給我們?就像你想要實現的一樣,以及你希望如何查看/分割片段。 –

回答

4

在我的應用程序,它只適用於RecyclerView。也許你應該使用RecyclerView而不是ListView。

+0

你有活動或片段中的RecyclerView嗎? – wwang

+0

@wwang剛剛測試過,它不適用於ListView。在我的應用程序中,我使用了可以無縫工作的RecyclerView的片段,並且測試了我添加了一個帶有無法工作的ListView的片段。 – razzledazzle

+0

很高興知道。謝謝。 – wwang

1

ListView沒有實現NestedScrollingChild,所以它不起作用。

RecyclerView不會,所以它可以將滾動傳播到NestedScrollingParent(CoordinatorLayout)。

+0

是否有可能讓ListView實現這個接口,並使其工作? –

+0

我想是的。看看RecyclerView的代碼,並嘗試在自定義的ListView類中實現它。我還沒有嘗試過,所以如果你成功的話,讓我們更新! –

+0

我試過了一段時間,但在給定時間內沒有成功。請讓我知道你是否成功。我也嘗試過使用這個庫來實現這個功能:https://github.com/ksoichiro/Android-ObservableScrollView,但是我沒能讓它們在一起很好地工作。 –

12

該行爲的原因是Framelayout沒有指定行爲。 CoordinatorLayout依賴子視圖來處理行爲。

您可以在這裏

http://android-developers.blogspot.in/2015/05/android-design-support-library.html

它指出

CoordinatorLayout和自定義視圖

一兩件事,重要的是要注意的是,CoordinatorLayout沒有按底部讀't 對FloatingActionButton有任何天生的理解,或者 AppBarLayout的工作 - 它只是提供了一個額外的API,形式爲 Coordinator.Behavior,它允許子視圖更好地控制觸摸事件和手勢,並通過onDependentViewChanged()聲明每個 之間的依賴關係並接收回調。

視圖可以通過使用 CoordinatorLayout.DefaultBehavior(YourView.Behavior.class) 註釋聲明默認行爲,或通過與 應用程序設置它在你的佈局文件:layout_behavior =「com.example.app.YourView $行爲「屬性。 該框架使任何視圖都可以與 CoordinatorLayout進行集成。

編輯:儘管FrameLayout不是一個自定義視圖,它沒有指定CoordinateLayout尋找的行爲。

+1

'該行爲的原因是Framelayout沒有指定Behaviour'。謝謝。這真的有幫助。 –

18

android.support.v4.widget.NestedScrollView

NestedScrollView就像滾動型替換您的FrameLayout,但它支持同時充當嵌套滾動父母和孩子在Android的新的和舊版本。嵌套滾動默認啓用。

Link to doc

+0

該解決方案非常簡單,效果很好。 –

+3

只有當你想讓你的整個片段垂直滾動時纔有效。它不起作用,例如當你的片段包含需要與CoordinatorLayout的其他子節點協調的ViewPager時。看來,如果FragmentContainer不是NestedScrollView,那麼CoordinatorLayout對於通過FragmentTransactions在稍後添加的Children來說效果不佳。如果整個片段不應該垂直滾動,而是對滑動手勢作出反應,那麼使用NestedScrollView是不行的。 –

11

使用的FrameLayout作爲CoordinatorLayout兒童工作得很好。工具欄正在崩潰,就像它應該的那樣。當我使用過時的庫時,我在開始時遇到了問題。

這裏是我現在使用的gradle這個依賴關係:

compile 'com.android.support:appcompat-v7:22.2.1' 
compile 'com.android.support:cardview-v7:22.2.0' 
compile 'com.android.support:recyclerview-v7:22.2.0' 
compile 'com.android.support:design:22.2.0' 

我使用FrameLayout與屬性app:layout_behavior="@string/appbar_scrolling_view_behavior"CoordinatorLayout一個活動的佈局兒童。 FrameLayout充當碎片的容器。我的片段佈局的根元素是RecyclerViewNestedScrollView

下面是活動的佈局:

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

    <FrameLayout 
      android:id="@+id/..." 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      /> 

    <android.support.design.widget.AppBarLayout 
      android:layout_height="192dp" 
      android:layout_width="match_parent" 
      > 
     <android.support.design.widget.CollapsingToolbarLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed" 
       > 
      <ImageView 
        android:id="@+id/.." 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:fitsSystemWindows="true" 
        android:scaleType="centerCrop" 
        android:src="@drawable/..." 
        app:layout_collapseMode="parallax"/> 
      <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar_sessions" 
        android:layout_height="?attr/actionBarSize" 
        android:layout_width="match_parent" 
        app:layout_collapseMode="pin" 
        /> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

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

我的第一個片段的佈局是這樣的:

<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="..." 
    /> 

我的第二個片段的佈局是這樣的:

<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="..." 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="..." 
    > 
... 
</android.support.v4.widget.NestedScrollView> 
1

你只需要更換

的FrameLayout

android.support.v4.widget.NestedScrollView

這樣:

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/root_coordinator" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="192dp" 
       android:scaleType="centerCrop" 
       android:src="@drawable/header" 
       app:layout_collapseMode="parallax" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/app_bar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
       app:layout_collapseMode="pin" /> 

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

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

    <!-- This was before FrameLayout --> 
    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
    </android.support.v4.widget.NestedScrollView> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:layout_margin="16dp" 
     android:src="@drawable/ic_drawer_alertas" 
     app:borderWidth="0dp" 
     app:fabSize="mini" /> 
</android.support.design.widget.CoordinatorLayout> 


<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_drawer" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:headerLayout="@layout/drawer_header" 
    app:itemIconTint="@color/colorAccent" 
    app:itemTextColor="@color/colorSecondaryText" 
    app:menu="@menu/menu_main" /> 

0

您必須添加行爲滾動視圖:

<android.support.v4.widget.SwipeRefreshLayout 
    android:layout_marginTop="5dp" 
    android:id="@+id/swipe_main" 
    android:enabled="false" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
<com.marshalchen.ultimaterecyclerview.UltimateRecyclerView 
    android:id="@+id/rvUserProfile" 
    app:recyclerviewEmptyView ="@layout/ev_home" 
    app:recyclerviewClipToPadding="false" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></com.marshalchen.ultimaterecyclerview.UltimateRecyclerView> 
</android.support.v4.widget.SwipeRefreshLayout> 
2

這是一個很好的問題。我也有同樣的。

您的貨櫃FrameLayout被定義正確問題在於FragmentFragment應具有RecyclerView &而不是ListView,因爲它現在已被棄用。

例子:

<android.support.v7.widget.RecyclerView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipToPadding="false" 
    android:paddingTop="@dimen/activity_vertical_margin"/> 

我用同樣的結構,我的應用程序&它的作品完美。

3

你可以實現你的CoordinatorLayout內的FrameLayout滾動。對於它,你必須在你的frameLayout中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"以及在你想要這種摺疊效果的滾動視圖中。

如果您在您的frameLayout內充氣RecyclerView,那麼您必須在您的recyclerrView中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:orientation="vertical"> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    /> 
</Relativelayout> 

,或者

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:orientation="vertical"> 
<android.support.v4.widget.NestedScrollView 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
     android:layout_above="@+id/bNext" 
     android:fillViewport="true" 
      android:scrollbars="none" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
<!-- your content --> 
</android.support.v4.widget.NestedScrollView> 
</Relativelayout> 

因此,使用這種方法就可以實現與滾動的FrameLayout是否含有回收或nestedScrollview。

+0

我知道它有點晚回答,但我解決我的問題這種方式 – Anupriya

+0

此解決方案不適合我。 –

1

從工具到的FrameLayout移動app:layout_scrollFlags="scroll|enterAlways"。對不起,來晚了。

+0

但它會影響列表滾動。 –