滚动recyclerview时隐藏工具栏

时间:2016-06-29 08:53:20

标签: android material-design android-appbarlayout

我是材料设计的新手,滚动列表时我无法隐藏工具栏。有关我共享代码的更多信息,请参阅以下内容:

1.应用程序的常见布局

$_['text_dimensions'] = 'Dimensions:';
        $_['text_by'] = 'x';

2.运行此布局的运行时间:

<?xml version="1.0" encoding="utf-8"?>

<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light">

        <RelativeLayout
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <!--  <include layout="@layout/custom_toolbar" />-->
                <RelativeLayout
                    android:id="@+id/toolbarContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.AppBarLayout
                        android:id="@+id/appbar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fitsSystemWindows="true"
                        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


                        <android.support.v7.widget.Toolbar
                            android:id="@+id/customToolbar"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/colorPrimary"
                            android:minHeight="?attr/actionBarSize"
                            android:textColorPrimary="@color/textColorPrimary"
                            app:colorButtonNormal="@android:color/white"
                            app:colorControlHighlight="?attr/colorPrimaryDark"
                            app:colorControlNormal="?android:attr/textColorPrimary"
                            app:layout_scrollFlags="scroll|enterAlways"
                            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                            <TextView
                                android:id="@+id/actionbar_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="center_horizontal"
                                android:textColor="@color/colorPrimaryDark"
                                android:textSize="@dimen/action_bar_title_text_size"
                                android:textStyle="bold" />

                            <ImageView
                                android:id="@+id/toolbar_logo"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="left"
                                android:src="@drawable/btn_check_on"
                                android:visibility="gone" />
                        </android.support.v7.widget.Toolbar>
                    </android.support.design.widget.AppBarLayout>

                    <ImageView
                        android:id="@+id/drawerIcon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:src="@drawable/img_drawer_logo" />

                </RelativeLayout>

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    app:tabGravity="fill"
                    app:tabMaxWidth="0dp"
                    app:tabMode="fixed"
                    app:tabSelectedTextColor="@color/orange_100"
                    app:tabTextColor="@color/grey" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="gone"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


                <include layout="@layout/horizontal_line" />
                <!-- As the main content view, the view below consumes the entire
                   space available using match_parent in both dimensions. -->
                <FrameLayout
                    android:id="@+id/content_frame"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/baseline"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
            </LinearLayout>

            <moebel.de.app.ui.view.MaterialSearchView
                android:id="@+id/search_view"
                style="@style/MaterialSearchViewStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <moebel.de.app.ui.view.ShopFinderSearchView
                android:id="@+id/shopfinder_search_view"
                style="@style/ShopFinderSearchViewStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <LinearLayout
                android:id="@+id/layout_progressbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@color/transparentColor"
                android:gravity="center"
                android:visibility="visible">

                <ProgressBar
                    android:id="@+id/progressBar1"
                    style="@android:style/Widget.ProgressBar.Large"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

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

    <RelativeLayout
        android:id="@+id/drawer_left"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/drawerListBackground"
        android:clickable="true">

        <ListView
            android:id="@+id/navigation_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:choiceMode="singleChoice"
            android:divider="@null"
            android:listSelector="@drawable/drawer_list_selector" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@drawable/img_drawer_logo" />

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

请帮我找出解决方法。我遇到了这个问题,我不知道这段代码有什么问题。

4 个答案:

答案 0 :(得分:0)

添加:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

到您的RecyclerView标签和片段的根标签。

答案 1 :(得分:0)

你可以做这样的事情

public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
private static final int HIDE_THRESHOLD = 20;
private int scrolledDistance = 0;
private boolean controlsVisible = true;

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
  onHide();
  controlsVisible = false;
  scrolledDistance = 0;
} else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) {
  onShow();
  controlsVisible = true;
  scrolledDistance = 0;
}

if((controlsVisible && dy>0) || (!controlsVisible && dy<0)) {
  scrolledDistance += dy;
}
}

public abstract void onHide();
public abstract void onShow();

}

然后将此监听器添加到您的RecyclerView

recyclerView.setOnScrollListener(new HidingScrollListener() {
@Override
public void onHide() {
  hideViews();
}
@Override
public void onShow() {
  showViews();
}
});

hideViews()方法中,您可以隐藏工具栏 mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2));

showViews()方法中,您可以显示工具栏 mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));

查看此link以获取完整教程

答案 2 :(得分:0)

如果你想使用它:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

要滚动工具栏,请重新设计您的布局。

layout_behavior仅在AppBarLayout

下属于CoordinatorLayout时有效 像这样:

<android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_light">

            <android.support.design.widget.AppBarLayout
                   android:id="@+id/appbar"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:fitsSystemWindows="true"
                   android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
            </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

答案 3 :(得分:0)

在R&amp; D之后我发现AppBarLayout必须是CoordinatorLayout的直接孩子所以我的更正代码如下所示

series: [{
        name: 'On Track',
        data: [80,67,'']
}]