将工具栏顺利隐藏在RecyclerView Scroll上? [英] Hiding Toolbar smoothly on RecyclerView Scroll?

查看:96
本文介绍了将工具栏顺利隐藏在RecyclerView Scroll上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个RecyclerView,其中包含一些项目列表.我正在听RecyclerView的Scroll listener,如果RecyclerView在某个时候说500,它应该隐藏工具栏,并且当它超过500时应该保持隐藏状态.同样,当我达到< = 450时,它会显示工具栏.

Currently I've a RecyclerView that holds some list of items. I'm listening the Scroll listener of RecyclerView and if the RecyclerView at some point say 500, it should hide the toolbar and it should remain hide when it crosses to 500+. Similarly, it shows the toolbar when i reaches <= 450.

这是我到目前为止尝试过的代码.问题是,

This is the code I've tried so far. The problem is,

  1. 它隐藏了工具栏,但在该点处隐藏或显示时闪烁.
  2. 如何实现平滑的工具栏隐藏?

  1. It hides the toolbar but it flashes when it hides or shows at that mentioned point.
  2. How to achieve a smooth toolbar hide?

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);


        }


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

            scrollD = scrollD + dy;
            Log.d("key", "DY is .." + (dy + scrollD));

            if (scrollD >= 500) {
              // code to hide
            }

            if (scrollD <= 450) {


             // code to show

            }
        }
    });

推荐答案

使用CoordinatorLayout而不是线性/相对布局,并将以下属性添加到工具栏.

Use CoordinatorLayout instead of Linear/Relative layout and add the following attribute to the toolbar.

app:layout_scrollFlags="scroll|enterAlways"

app:layout_scrollFlags="scroll|enterAlways"

CoordinatorLayout通过在用户向下滚动时隐藏工具栏并在用户向上滚动时再次显示它来处理工具栏的可见性.

CoordinatorLayout handles visibility of toolbar by hiding it when the user scrolls down and showing it again when the user scrolls up.

代码:

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

<!-- $Id$ -->

<android.support.design.widget.CoordinatorLayout 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">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

     <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways"  />

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

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

请参阅此链接: https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/

这篇关于将工具栏顺利隐藏在RecyclerView Scroll上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆