工具栏和选项卡布局在我向上滚动 RecyclerView 时不会隐藏。

huangapple go评论79阅读模式
英文:

Toolbar and TabLayout don't hide when I scroll the RecyclerView up

问题

我在向上滚动时遇到了隐藏ToolbarTabLayout的问题。我有3个不同的xml文件:1) Toolbar,2) TabLayoutFragment(这里有TabLayoutViewPager,并且我在这里includetoolbar),以及3) HomeFragment(包含RecyclerView)。我需要在向上滚动时隐藏Toolbar,我已经看到许多其他帖子中大多数人使用CoordinatorLayout而不是Relative LayoutLinear Layout,然后他们必须添加app:layout_scrollFlags="scroll|enterAlways",我也已经添加了这个,但是我仍然无法使RecyclerView向上滚动时隐藏Toolbar

我还在RecyclerView中添加了以下内容,但仍然没有效果... app:layout_behavior="@string/appbar_scrolling_view_behavior"

有人可以指出这里缺少什么吗?

Toolbar

<!-- 这里是Toolbar的代码 -->

TabLayoutFragment

<!-- 这里是TabLayoutFragment的代码 -->

HomeFragment

<!-- 这里是HomeFragment的代码 -->

(请注意,上述内容是您提供的代码的翻译,仅包含所需的翻译部分。)

英文:

I am having trouble getting the Toolbar and TabLayout to hide when I scroll up. I have 3 different xml files: 1) Toolbar, 2) TabLayoutFragment (here I have the TabLayout and ViewPager, and I include the toolbar here, and 3) HomeFragment (contains the RecyclerView). I need for the Toolbar to hide when I scroll up, and I have seen in many other posts that most people use a CoordinatorLayout instead of a Relative Layout or Linear Layout, and then they have to add app:layout_scrollFlags=&quot;scroll|enterAlways&quot; which I have done, but I still can't get the Toolbar to hide when scrolling the RecyclerView up.

I've also added this to the RecyclerView, but still nothing... app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;

Could someone point out what is missing here?

Toolbar

&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:id=&quot;@+id/toolbar_events&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:background=&quot;#fff&quot;
    android:fitsSystemWindows=&quot;true&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:id=&quot;@+id/bar&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;

        &lt;androidx.appcompat.widget.Toolbar
            android:id=&quot;@+id/toolbar_home_fragment&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:background=&quot;#fff&quot;
            app:contentInsetEnd=&quot;0dp&quot;
            app:contentInsetLeft=&quot;0dp&quot;
            app:contentInsetRight=&quot;0dp&quot;
            app:contentInsetStart=&quot;0dp&quot;
            app:contentInsetStartWithNavigation=&quot;0dp&quot;
            app:layout_scrollFlags=&quot;scroll|enterAlways&quot;
            app:popupTheme=&quot;@style/ThemeOverlay.AppCompat.Light&quot;&gt;

            &lt;RelativeLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;&gt;

                &lt;TextView
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_centerHorizontal=&quot;true&quot;
                    android:layout_centerVertical=&quot;true&quot;
                    android:fontFamily=&quot;@font/scriptbl&quot;
                    android:text=&quot;Events&quot;
                    android:textColor=&quot;@color/colorBlack&quot;
                    android:textSize=&quot;60sp&quot; /&gt;

                &lt;ImageView
                    android:id=&quot;@+id/search&quot;
                    style=&quot;@style/SelectableIconThemeBackground&quot;
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_alignParentEnd=&quot;true&quot;
                    android:layout_centerVertical=&quot;true&quot;
                    android:layout_marginEnd=&quot;10dp&quot;
                    android:contentDescription=&quot;@string/camera&quot;
                    android:padding=&quot;10dp&quot;
                    android:src=&quot;@drawable/icon_search&quot; /&gt;

                &lt;ImageView
                    android:id=&quot;@+id/filter&quot;
                    style=&quot;@style/SelectableIconThemeBackground&quot;
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_centerVertical=&quot;true&quot;
                    android:layout_toStartOf=&quot;@+id/search&quot;
                    android:contentDescription=&quot;@string/camera&quot;
                    android:padding=&quot;10dp&quot;
                    android:src=&quot;@drawable/icon10_sort&quot; /&gt;

                &lt;ImageView
                    android:id=&quot;@+id/options&quot;
                    style=&quot;@style/SelectableIconThemeBackground&quot;
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_alignParentStart=&quot;true&quot;
                    android:layout_centerVertical=&quot;true&quot;
                    android:layout_marginStart=&quot;10dp&quot;
                    android:contentDescription=&quot;@string/options&quot;
                    android:padding=&quot;10dp&quot;
                    android:src=&quot;@drawable/icon_short_text&quot; /&gt;

            &lt;/RelativeLayout&gt;

        &lt;/androidx.appcompat.widget.Toolbar&gt;

    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

TabLayoutFragment

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/fragment_home_tab_layout&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:tag=&quot;Home&quot;
    tools:context=&quot;.Fragment.TabLayoutFragment&quot;&gt;

    &lt;include
        android:id=&quot;@+id/toolbar_events&quot;
        layout=&quot;@layout/toolbar_events&quot; /&gt;

    &lt;com.google.android.material.tabs.TabLayout
        android:id=&quot;@+id/tab_layout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:layout_marginBottom=&quot;8dp&quot;
        android:background=&quot;#fff&quot;
        app:layout_scrollFlags=&quot;scroll|enterAlways&quot;
        app:tabIndicatorColor=&quot;#000&quot;
        app:tabIndicatorFullWidth=&quot;false&quot;
        app:tabRippleColor=&quot;@android:color/transparent&quot;
        app:tabSelectedTextColor=&quot;@color/colorBlack&quot;
        app:tabTextColor=&quot;@color/colorBlack&quot; /&gt;

    &lt;androidx.viewpager.widget.ViewPager
        android:id=&quot;@+id/viewpager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;#fff&quot;
        app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot; /&gt;

&lt;/LinearLayout&gt;

HomeFragment

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/fragment_home&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:fitsSystemWindows=&quot;true&quot;
    android:orientation=&quot;vertical&quot;
    android:tag=&quot;fragment_home&quot;
    tools:context=&quot;.Fragment.HomeFragment&quot;&gt;

    &lt;androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id=&quot;@+id/refresh&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;RelativeLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_below=&quot;@+id/toolbar&quot;&gt;

            &lt;TextView
                android:id=&quot;@+id/welcome&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_centerHorizontal=&quot;true&quot;
                android:layout_marginTop=&quot;100dp&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;Welcome to Events!&quot;
                android:textColor=&quot;#d9d9d9&quot;
                android:textSize=&quot;36sp&quot;
                android:visibility=&quot;gone&quot; /&gt;

            &lt;RelativeLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;&gt;

                &lt;TextView
                    android:id=&quot;@+id/location&quot;
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:drawablePadding=&quot;5dp&quot;
                    android:gravity=&quot;center_horizontal&quot;
                    android:padding=&quot;8dp&quot;
                    app:drawableStartCompat=&quot;@drawable/icon_loc&quot; /&gt;

                &lt;androidx.recyclerview.widget.RecyclerView
                    android:id=&quot;@+id/recycler_view&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_below=&quot;@+id/location&quot;
                    app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot; /&gt;

            &lt;/RelativeLayout&gt;

            &lt;androidx.recyclerview.widget.RecyclerView
                android:id=&quot;@+id/recycler_view_following&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:visibility=&quot;gone&quot;
                app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot; /&gt;

        &lt;/RelativeLayout&gt;

    &lt;/androidx.swiperefreshlayout.widget.SwipeRefreshLayout&gt;

&lt;/RelativeLayout&gt;

答案1

得分: 3

你可以通过添加 addOnScrollListener 方法来在程序中实现它。

RecyclerView.SCROLL_STATE_DRAGGING 方法中隐藏工具栏和 TabLayout,而在 RecyclerView.SCROLL_STATE_IDLE 方法中,如果你想要再次显示它们,你可以再次显示出来。

你可以尝试以下方式:

recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            // 如果你想要显示它们,可以在这里显示你的视图
        } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
            // 在这里隐藏你的视图
        }

        super.onScrollStateChanged(recyclerView, newState);
    }
});

或者你可以使用 RecyclerView 的 addOnScrollListener 方法:

参考:addOnScrollListener

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (dy > 0) {
            // 向下滚动
            // 从这里显示你的控件
        } else if (dy < 0) {
            // 向上滚动
            // 从这里隐藏你的控件
        }
    }
});

此外,你还可以在这里添加一些动画来显示和隐藏你的工具栏和选项卡布局。

英文:

You can make it programmatically by adding the addOnScrollListener method.

On the RecyclerView.SCROLL_STATE_DRAGGING method hide the toolbar and TabLayout and on RecyclerView.SCROLL_STATE_IDLE method you can show it again if you want to show them again.

You can try it in such way:

recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
		// Show your view again.. If you want to show them..
    } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
		// Hide your view..
    }

    super.onScrollStateChanged(recyclerView, newState);

}});

Or You can use recycler view's addOnScrollListener method:

Refer: addOnScrollListener

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (dy &gt; 0) {
        //Scrolling down
        // Show your controllers from here
    } else if (dy &lt; 0) {
        //Scrolling up
        // Hide your controllers from here
    }
}});

Also, you can add some animations there to show and hide your toolbar and tab layouts.

答案2

得分: 0

<ScrollView>
   <Toolbar></Toolbar>
   <Tablayout></Tablayout>
</ScrollView>
英文:

Place the toolbar and the tablayout within the ScrollView group to get the scrolling part.

&lt;ScrollView&gt;
   &lt;Toolbar&gt;&lt;/Toolbar&gt;
   &lt;Tablayout&gt;&lt;/Tablayout&gt;
&lt;/ScrollView&gt;

huangapple
  • 本文由 发表于 2020年8月20日 23:44:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63508690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定