【问题标题】:CoordinatorLayout/AppBarLayout ExpandableListView being rendered off screenCoordinatorLayout/AppBarLayout ExpandableListView 在屏幕外渲染
【发布时间】:2015-09-30 13:46:38
【问题描述】:

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

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

但是,我当前的设置显示了一个问题:不仅工具栏没有滚动,ListView 似乎在底部呈现在屏幕之外。就好像它被 AppBarLayout 的高度抵消了一样。

这是一个描述问题的 gif,注意最后一项被切断了,而且 ScrollBar 也离开了屏幕:

我的布局很标准:

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

【问题讨论】:

  • 看看我的回答让我知道它是否有效

标签: android android-coordinatorlayout android-appbarlayout


【解决方案1】:

CoordinatorLayout 仅适用于 RecyclerView 或 NestedScrollView。尝试将 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 之前按预期工作。

【讨论】:

  • 是的,在 ExpandableListView 上设置嵌套滚动可以修复它。我已经在您对 v21 之前的设备的回答中添加了一个 hack,希望您不要介意。
  • SwipeRefreshLayout 怎么样?
  • 您现在可以将ViewCompat.setNestedScrollingEnabled((View view, boolean enabled) 用于兼容性目的。没有检查它是否真的适用于旧 API。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    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;
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2015-04-08
      • 2015-08-24
      • 2012-12-11
      • 1970-01-01
      相关资源
      最近更新 更多