CoordinatorLayout/AppBarLayout ExpandableListView 在屏幕外呈现

CoordinatorLayout/AppBarLayout ExpandableListView being rendered off screen

使用 CoordinatorLayout 和 AppBarLayout 时还有更多问题。

我正在尝试实现让工具栏在向下滚动时滚出屏幕并在向上滚动时返回屏幕的基本功能。

但是,我当前的设置出现了问题:不仅工具栏没有滚动,而且底部的 ListView 似乎呈现在屏幕之外。几乎就像被 AppBarLayout 高度偏移了一样。

这是描述该问题的 gif,请注意,最后一项被截断,滚动条也在屏幕外:

我的布局很标准:

<?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>

CoordinatorLayout 仅适用于 RecyclerView 或 NestedScrollView.Try 将 ExapandableListView 包装在 NestedScrollView 中或使用以下代码为 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);
}

编辑 您可以使用 else 语句使滚动按预期在 21 之前工作。

我会把它写成评论,但就可读性而言,我会放弃此信息作为答案。如果它不起作用,请告诉我,我会删除它... 我猜你应该告诉你的工具栏如何交互。在我的应用程序中,工具栏如下所示:

<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"/>

请注意 "app:layout_collapseMode"

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