android - RecyclerView 适合屏幕时不要折叠 Toolbar

标签 android toolbar android-recyclerview android-appcompat android-design-library


我使用 Android 设计库 制作了一个应用程序,带有一个工具栏和 TabLayout。
实际上有 2 个选项卡,都带有 2 个 RecyclerView,滚动时会自动折叠工具栏。

我的问题是:当 RecyclerView 的项目很少并且完全适合屏幕时(如 TAB 2 中),我可以禁用工具栏折叠吗?

我见过很多例子,比如 CheeseSquare ,由 Google 员工制作,问题仍然存在:即使 RecyclerView 只有 1 个项目,工具栏也会在滚动时继续隐藏。

enter image description here

我想我可以找出 RecyclerView 的第一项是否在屏幕上可见,如果是,则禁用工具栏折叠。前者容易实现,后者呢?

这是我的布局:

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

        <android.support.design.widget.AppBarLayout
            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/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
            android:background="?attr/colorPrimary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/glucosio_pink"
            app:tabSelectedTextColor="@android:color/white"
            app:tabIndicatorColor="@color/glucosio_accent"
            app:tabTextColor="#80ffffff"/>
        </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/main_fab"
        android:layout_margin="16dp"
        android:onClick="onFabClicked"
        app:backgroundTint="@color/glucosio_accent"
        android:src="@drawable/ic_add_black_24dp"
        android:layout_gravity="bottom|right"
        />
    </android.support.design.widget.CoordinatorLayout>




最佳答案

最终解决方案(感谢 Michał Z.)

关闭/打开工具栏滚动的方法:

public void turnOffToolbarScrolling() {
    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);

    //turn off scrolling
    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
    toolbarLayoutParams.setScrollFlags(0);
    mToolbar.setLayoutParams(toolbarLayoutParams);

    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    appBarLayoutParams.setBehavior(null);
    appBarLayout.setLayoutParams(appBarLayoutParams);
}

public void turnOnToolbarScrolling() {
    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);

    //turn on scrolling
    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
    toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    mToolbar.setLayoutParams(toolbarLayoutParams);

    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
    appBarLayout.setLayoutParams(appBarLayoutParams);
}


查看 RecyclerView 的最后一项是否在我的 Fragment 中可见。
如果是,请禁用滚动:

public void updateToolbarBehaviour(){
    if (mLayoutManager.findLastCompletelyVisibleItemPosition() == items.size()-1) {
        ((MainActivity) getActivity()).turnOffToolbarScrolling();
    } else {
        ((MainActivity)getActivity()).turnOnToolbarScrolling();
    }
}

关于android - RecyclerView 适合屏幕时不要折叠 Toolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32404979/

相关文章:

java - 我可以在 Firebase 数据库中存储 Int 值吗

android - 带有自定义 View 的工具栏标题

android - 在工具栏或操作栏的中心设置 ActionBarDrawerToggle,android?

android - 突出显示 RecyclerView 中的选定项目

android - 在 RecyclerView 中滚动时自动在行之间添加空间

Android DrawBitmap 在 DIP 中?

java - AndroidPlot - 使图形与 x 轴标签一起滚动

android - 工具栏标题不居中

android - 加载recyclerView时出现Glide错误 "You must pass in a non null View"

android - PhoneGap 中的 INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES 错误