【问题标题】:toolbar hide and show on scroll not working工具栏在滚动时隐藏和显示不起作用
【发布时间】:2018-05-17 14:51:58
【问题描述】:

我添加了一个工具栏,我正在尝试使用工具栏滚动上的隐藏和显示功能,但由于某种原因它无法正常工作,我按照教程中的每一步操作仍然无法正常工作。我在片段中使用工具栏,所以也许这也是一个原因,但我对此知之甚少。 所以我会很感激一些帮助

xml

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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">

    <!-- AppBarLayout is a wrapper for a Toolbar in order to apply scrolling effects. -->
    <!-- Note that AppBarLayout expects to be the first child nested within a CoordinatorLayout -->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <!-- Toolbar is the actual app bar with text and the action items -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:titleTextColor="@color/White"
            app:layout_scrollFlags="scroll|enterAlways|snap">
            <com.ct.listrtrial.Custom.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Feeds"
                android:textSize="30sp"
                android:textColor="@color/White"
                android:layout_gravity="center"
                android:id="@+id/toolbar_title"
                android:layout_marginRight="@dimen/_200sdp"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <!-- This could also be included from another file using the include tag -->
    <!-- i.e `res/layout/content_main.xml` -->
    <!-- `app:layout_behavior` is set to a pre-defined standard scrolling behavior -->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/feed_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

   <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="bottom|center"
        android:layout_marginBottom="5dp"
       android:layout_gravity="end|bottom"
       android:src="@drawable/ic_search_black_24dp" />

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

片段代码

toolbar = (Toolbar)view.findViewById(R.id.toolbar);
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        CustomTextView customTextView = (CustomTextView)toolbar.findViewById(R.id.toolbar_title);
        //getActivity().getTitle();
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);

【问题讨论】:

    标签: android xml android-toolbar


    【解决方案1】:

    如果您想在滚动时隐藏和显示工具栏,请将您的工具栏标志替换为。

    app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap">
    

    【讨论】:

    • 我在片段中使用工具栏。
    • 我建议你只在你的活动容器中使用工具栏。因为它对所有片段都是通用的
    • 但我只想要一个片段中的工具栏
    • 我认为你错了,因为我们只在基础容器中添加了一个工具。
    • 对不起我没听懂
    【解决方案2】:

    在您的片段 onCreateView 中添加以下代码

    ViewCompat.setNestedScrollingEnabled(recyclerView, true);
    

    我想这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      相关资源
      最近更新 更多