【问题标题】:AppBarLayout autocomplete Expand/collapse when user stop scrolling当用户停止滚动时,AppBarLayout 自动完成展开/折叠
【发布时间】:2019-11-15 21:20:05
【问题描述】:

我想在用户停止滚动时自动完成滚动到顶部/底部。像这样image。 我已经测试过 OnOffsetChangedListener 但总是有问题。 请有什么想法。

没有用于此操作的 java 代码。

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/backLayout"
android:animateLayoutChanges="true"
android:fitsSystemWindows="true"
>

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:fitsSystemWindows="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <!--app:layout_scrollFlags="scroll|enterAlwaysCollapsed" -->
    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
        app:contentScrim="?attr/colorPrimary">

        ...no toolbar

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/collLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7"
            >

            ...content
        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScroll"
    android:fitsSystemWindows="true"
    android:clipToPadding="false"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        ...content
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    这里有一些标志来检查滚动:

    int TO_UP = -1;
    int TO_DOWN = +1;
    boolean AutoScroll = false;
    

    如果用户释放,则检查触摸并检查标志以自动滚动到顶部:

    nsv_scroll.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
    
      if(AutoScroll){
        gotoTop();
        return true;
      }
    }
    return false;
    }
    });
    

    检查用户是否滚动到顶部也可以滚动到顶部并将标志设置为 true ,否则为 false 并且没有自动滚动:

        nsv_scroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            Log.e("Scroll", scrollY + "");
            if (scrollY > 0) {
                if (nsv_scroll.canScrollVertically(TO_UP))
                    AutoScroll = true;
            } else {
                AutoScroll = false;
            }
        }
    });
    

    只需转到顶部:

    void gotoTop(){
        nsv_scroll.fling(0);
        nsv_scroll.smoothScrollTo(0, 0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2017-01-14
      • 1970-01-01
      • 2015-09-15
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多