滚动浏览特定视图后滚动时启用工具栏自动隐藏

时间:2017-11-15 07:28:06

标签: android android-toolbar android-coordinatorlayout

我正在尝试使用CoordinateLayout为我的工具栏实现自动隐藏功能。但我也想稍微调整它,以便隐藏/显示仅在我滚动浏览我的imageview后才有效。

enter image description here

我目前能够打开/关闭"通过AppBarLayout.LayoutParams隐藏/显示工具栏的可用性。所以现在如果我在imageview的高度内滚动它就不会隐藏工具栏,如果我滚动浏览imageview就会隐藏它。唯一的问题是,如果我非常快速地滚动布局并在它通过imageview的高度之前立即拉动我的手指,工具栏将无法隐藏。在我看来,这是因为在工具栏的显示/隐藏参数更改为启用之前调用了滚动侦听器的功能。

这里是代码: ArticleActivity.java

public class ArticleActivity extends AppCompatActivity {

private Toolbar toolbar;
private ImageView imageView;
private NestedScrollView scrollView;

private Matrix matrix;

private int imageHeight = 0;
private float scale = 0;
private boolean hideable = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_article);

    imageView = (ImageView) findViewById(R.id.imageView);
    scrollView = (NestedScrollView) findViewById(R.id.scrollView);
    toolbar = (Toolbar) findViewById(R.id.toolbar);

    matrix = new Matrix();

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    scale = (float) displayMetrics.widthPixels / ((float) imageView.getDrawable().getIntrinsicWidth());
    matrix.postScale(scale, scale);
    imageView.setImageMatrix(matrix);

    if(imageView.getViewTreeObserver().isAlive()) {
        imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                imageHeight = imageView.getHeight();
            }
        });
    }

    if(scrollView.getViewTreeObserver().isAlive()) {
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                int scrollY = scrollView.getScrollY();
                if(scrollY <= imageHeight) {
                    matrix.setTranslate(0, scrollY / 4);
                    matrix.postScale(scale, scale);
                    imageView.setImageMatrix(matrix);
                    if(hideable) {
                        AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
                        params.setScrollFlags(0);
                        toolbar.setLayoutParams(params);
                        hideable = false;
                    }
                } else {
                    if(!hideable) {
                        AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
                        // 1 = scroll, 4 = enterAlways, 16 = snap
                        params.setScrollFlags(21);
                        toolbar.setLayoutParams(params);
                        hideable = true;
                    }
                }
            }
        });
    }
}}

activity_article.xml:

<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/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

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

<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="matrix"
            android:src="@drawable/download"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView"
            android:text="What is Lorem Ipsum?"/>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

我该怎么做才能解决这个问题?或者有另一种方法来实现这一目标吗?谢谢

0 个答案:

没有答案
相关问题