7

使用CoordinatorLayout和AppBarLayout還有更多問題。CoordinatorLayout/AppBarLayout ExpandableListView被渲染出屏幕

我正試圖實現基本功能,讓工具欄滾動屏幕時向下滾動,並在滾動時返回到屏幕上。

但是,我目前的設置顯示出一個問題:不僅工具欄不滾動,ListView似乎在屏幕底部渲染。它幾乎就像被AppBarLayout高度所抵消一樣。

下面是說明這一問題的GIF,請注意最後一個項目被切斷還滾動條是關閉屏幕:

enter image description here

我的佈局是非常標準:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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:background="@color/background"> 

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

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?android:attr/actionBarSize" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      android:background="@color/orange" 
      app:layout_scrollFlags="scroll|enterAlways"/> 

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


    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipeToRefresh" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <ExpandableListView 
      android:id="@+id/listView" 
      android:groupIndicator="@android:color/transparent" 
      android:layout_width="match_parent" 
      android:dividerHeight="0px" 
      android:layout_height="match_parent"/> 
    </android.support.v4.widget.SwipeRefreshLayout> 

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

看一看我的回答的讓我知道它的工作 – waleedsarwar86

回答

8

CoordinatorLayout僅適用於RecyclerView或NestedScrollView.Try在NestedScrollView中包裝ExapandableListView,或者使用下面的代碼爲ExpandableListView製作NestedScrollingEnable。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    expandablelistView.setNestedScrollingEnabled(true); 
}else { 
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mSwipeLayout.getLayoutParams(); 
    params.bottomMargin = heightOfAppBarCompat; 
    mSwipeLayout.setLayoutParams(params); 
} 

編輯您可以滾動按預期方式工作前21 else語句。

+0

你最好說「RecyclerView - 或 - NestedScrollView「... –

+1

@MartinPfeffer感謝您的更正 – waleedsarwar86

+0

是的,設置嵌套滾動啓用ExpandableListView修復它。我已經在黑客中添加了針對pre v21設備的答案,希望您不要介意。 – Graeme

0

我會寫它作爲評論,但在可讀性方面,我將放棄這個信息作爲答案。如果它不起作用,請告訴我,我將刪除它... 我想你應該告訴你的工具欄如何互動。在我的應用程序的工具欄上看起來是這樣的:

<android.support.v7.widget.Toolbar 
      android:id="@+id/anim_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

請注意「應用程序:layout_collapseMode」

0
private int mPreviousVisibleItem; 


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     expListView.setNestedScrollingEnabled(true); 
    } else { 
     expListView.setOnScrollListener(new AbsListView.OnScrollListener() { 

      @Override 
      public void onScrollStateChanged(AbsListView view, int scrollState) { 
      } 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
       if (firstVisibleItem > mPreviousVisibleItem) { 
        appBarLayout.setExpanded(false, true); 
       } else if (firstVisibleItem < mPreviousVisibleItem) { 
        appBarLayout.setExpanded(true, true); 
       } 
       mPreviousVisibleItem = firstVisibleItem; 
      } 
     }); 
    }