7

这是我的设置,我正在运行DrawerLayout其中包含一个 CoordinatorLayout ,其中包含一个 AppBarLayout 和一个 nestedscrollview。我试图让 nestedscrollview 正常滚动,工具栏在向下滚动时隐藏并在向上滚动时重新出现。附在里面的是我的 XML 代码。将不胜感激任何帮助.. 已阅读所有相关问题并实施了他们的答案,但没有任何成功。

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

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

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

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar"
            app:layout_scrollFlags="scroll|enterAlways" />

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

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

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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/menu_drawer" />

4

3 回答 3

5

我已经有一个星期了同样的问题,并尝试了几乎所有的方法来解决它。但是我设法解决了这个问题。

你在哪里有类似...

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar"
    app:layout_scrollFlags="scroll|enterAlways" />

app_bar.xml...用您的布局中的任何内容替换它。例如:

<android.support.v7.widget.Toolbar
    android:id="@+id/main_toolbar"
    style="@style/AppTheme.Toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:layout_scrollFlags="scroll|enterAlways"/>

似乎由于某种原因,使用tag时滚动CoordinatorLayout不起作用<include>

于 2015-07-19T03:28:43.403 回答
1

我认为使用新的CollapsingToolbarLayout会有所帮助…… 对新的 Android 设计支持库的一些非常有用的探索的简短描述显示了如何将工具栏包装在 CollapsingToolbarLayout 中并通过设置 layout_collapseMode 自定义效果。

更新

我认为在您的 ListView 中添加一个 onScrollListener 并显示/隐藏工具栏,如下例所示

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

listView.setOnScrollListener(new OnScrollListener() {
    int mLastFirstVisibleItem = 0;

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

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        if (view.getId() == listView.getId()) {
            final int currentFirstVisibleItem = listView.getFirstVisiblePosition();

            if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                getSupportActionBar().hide();
            } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                getSupportActionBar().show();
            }

            mLastFirstVisibleItem = currentFirstVisibleItem;
        }               
    }
});
于 2015-06-24T12:56:15.937 回答
1

正如@Farbod Salamat-Zadehwas 之前所说:使用标签CoordinatorLayout时不起作用。 但是你可以这样使用:<include>
<include>

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />

如果您可以接受参数app:layout_scrollFlags="scroll|enterAlways",则应将其移至您的app_bar.xml

于 2015-08-04T13:33:42.223 回答