【问题标题】:Hiding/Showing the action bar when scrolling滚动时隐藏/显示操作栏
【发布时间】:2016-02-09 04:33:10
【问题描述】:

我在片段中使用ObservableRecyclerView(我使用片段是因为我的应用中有标签)。我需要根据ObservableRecyclerView 的滚动来隐藏/显示操作栏。我按照question 中的答案进行了操作,但这有点误导,因为我无法理解oldScrollY 是什么。

我也尝试了这段代码,但它隐藏/显示了没有动画的栏

@Override
    public void onUpOrCancelMotionEvent(ScrollState scrollState) {
        ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (scrollState == ScrollState.UP) {
            if (ab.isShowing()) {
                ab.hide();
            }
        } else if (scrollState == ScrollState.DOWN) {
            if (!ab.isShowing()) {
                ab.show();
            }
        }
    }

编辑:

activity_main.xml

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

    <LinearLayout
        android:id="@+id/toolbarContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary"
            android:elevation="2dp"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <jo.edu.gju.motassem.seniorproject.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary"
            android:elevation="2dp" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="end|bottom"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:src="@drawable/ic_add_white"
        app:backgroundTint="@color/accent"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:rippleColor="@color/primary" />
</android.support.design.widget.CoordinatorLayout>

片段.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

   <com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

</FrameLayout>

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    您可以在包含 toolBar 的 .xml 文件中使用 coordinatorlayout,请参见以下代码:

       <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/all"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:fitsSystemWindows="true">
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|enterAlways"
            >
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:layout_scrollFlags="scroll|enterAlways"
            app:layout_collapseMode="pin"
            />
        </android.support.design.widget.AppBarLayout>
    
        <FrameLayout
            android:id="@+id/content_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >
        </FrameLayout>
    
        </android.support.design.widget.CoordinatorLayout>
    </LinearLayout>
    

    【讨论】:

    • 出现了一些问题,甚至选项卡都消失了,工具栏的高度大于选项卡。我添加了我的 xml 文件
    • 将此属性添加到 ViewPager app:layout_behavior="@string/appbar_scrolling_view_behavior"
    • 结果相同,没有变化。我想为 WhatsApp 中的工具栏设置动画
    • 您没有将 AppBarLayout 作为父级添加到 ToolBar ,请查看我的代码,它在 what'sApp 中对我来说可以正常工作
    • 不要忘记将 scrollFlag 添加到 CoordinatorLayout
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2014-02-06
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多