Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Android设计支持库平滑动画折叠工具栏_Android_Android Layout_Material Design_Android Design Library_Android Collapsingtoolbarlayout - Fatal编程技术网

使用Android设计支持库平滑动画折叠工具栏

使用Android设计支持库平滑动画折叠工具栏,android,android-layout,material-design,android-design-library,android-collapsingtoolbarlayout,Android,Android Layout,Material Design,Android Design Library,Android Collapsingtoolbarlayout,是否有任何方法可以使Android设计支持库的折叠动画在滚动时更平滑?当我释放滚动时,它会突然停止。但我想要的是:即使停止滚动,折叠动画也会继续平滑。 这是一个相当新的库,但是AppBarLayout最近进行了更新,以使用一个名为 用法: app:layout_scrollFlags="scroll|snap" 我会尝试寻找我的来源,并在我找到时更新我的答案 编辑:当然,它来自。添加代码 app:layout_scrollFlags="scroll|enterAlways" 在AppBar

是否有任何方法可以使Android设计支持库的折叠动画在滚动时更平滑?当我释放滚动时,它会突然停止。但我想要的是:即使停止滚动,折叠动画也会继续平滑。
这是一个相当新的库,但是AppBarLayout最近进行了更新,以使用一个名为

用法:

app:layout_scrollFlags="scroll|snap"
我会尝试寻找我的来源,并在我找到时更新我的答案

编辑:当然,它来自。

添加代码

 app:layout_scrollFlags="scroll|enterAlways"
在AppBarLayout内部的视图中。这是我用Android设计支持库折叠工具栏的演示代码

  <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways">

            <bubee.inews.Items.ItemMenu
                android:id="@+id/itemMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

我也在研究这个问题,并提出了可能不是非常优化的解决方案,但您可以改进它。一旦我改进了,我肯定会编辑答案,直到用户看到为止

public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
private static final String TAG = "app_AppBarStateChange";
public enum State {
    EXPANDED,
    COLLAPSED,
    IDLE
}

private State mCurrentState = State.IDLE;
private int mInitialPosition = 0;
private boolean mWasExpanded;
private boolean isAnimating;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
    if (i == 0) {
        if (mCurrentState != State.EXPANDED) {
            onStateChanged(appBarLayout, State.EXPANDED);
        }
        mCurrentState = State.EXPANDED;
        mInitialPosition = 0;
        mWasExpanded = true;
        Log.d(TAG, "onOffsetChanged 1");
        isAnimating = false;
        appBarLayout.setEnabled(true);
    } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
        if (mCurrentState != State.COLLAPSED) {
            onStateChanged(appBarLayout, State.COLLAPSED);
        }
        mCurrentState = State.COLLAPSED;
        mInitialPosition = appBarLayout.getTotalScrollRange();
        mWasExpanded = false;
        Log.d(TAG, "onOffsetChanged 2");
        isAnimating = false;
        appBarLayout.setEnabled(true);
    } else {
        Log.d(TAG, "onOffsetChanged 3");
        int diff = Math.abs(Math.abs(i) - mInitialPosition);
        if(diff >= appBarLayout.getTotalScrollRange()/3 && !isAnimating) {
            Log.d(TAG, "onOffsetChanged 4");
            isAnimating = true;
            appBarLayout.setEnabled(false);
            appBarLayout.setExpanded(!mWasExpanded,true);
        }
        if (mCurrentState != State.IDLE) {
            onStateChanged(appBarLayout, State.IDLE);
        }
        mCurrentState = State.IDLE;
    }
}

public abstract void onStateChanged(AppBarLayout appBarLayout, State state);

public State getCurrentState() {
    return mCurrentState;
}
}

创建这个类并调用下面的代码

private AppBarStateChangeListener mAppBarStateChangeListener = new AppBarStateChangeListener() {
    @Override
    public void onStateChanged(AppBarLayout appBarLayout, State state) {
        Log.d(TAG, "ToBeDeletedActivity.onStateChanged :: " + state);
        if(state == State.EXPANDED || state == State.IDLE) {
            getSupportActionBar().setTitle("");
        } else {
            getSupportActionBar().setTitle("Hello World");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mAppBarLayout.setElevation(0);
            }
        }
    }


};

mAppBarLayout.addOnOffsetChangedListener(mAppBarStateChangeListener);
请注意,不要设置annonymous类OffsetChangedListener,因为这是作为弱引用保留的,将由GC收集。我发现自己很难做到这一点


请浏览此代码并对其进行改进(任何人)并重新共享。谢谢您可以使用新的layout_scrollFlag snap在AppBarLayout状态下进行平滑滚动。但我所经历的是,当RecyclerView到达顶部时,滚动停止。i、 如果没有另一个滚动条,折叠工具栏布局将无法展开。为了使RecyclerView平滑地向上滚动并展开CollasingToolbarLayout,我在RecyclerView上使用了一个ScrollListener

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        int scrollDy = 0;
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            scrollDy += dy;
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(scrollDy==0&&(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE))
            {
                AppBarLayout appBarLayout = ((AppBarLayout) view.findViewById(R.id.app_bar));

                appBarLayout.setExpanded(true);
            }
        }
    });
我使用“scroll | exitUntilCollapsed”作为布局滚动标志

<android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:minHeight="80dp"
            app:layout_collapseMode="none"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

尝试添加以下代码:

   app:layout_scrollFlags="scroll|snap"

我正在通过AppBarLayout
。通过覆盖
onNestedFling
onNestedPreScroll

诀窍是,如果ScrollingView的顶级子级接近适配器中数据的开头,则重新结束fling事件

来源


已询问A,其中链接到。因此,这似乎是一个缺陷,当库的版本23发布时将得到解决。请尝试并理解此库的代码。解决方案很好,但滚动和展开之间存在速度差异:)完美,最终有一个平滑的滚动。这不是折叠工具栏。thanx有助于在滑动一半时自动上下推
public final class FlingBehavior extends AppBarLayout.Behavior {
private static final int TOP_CHILD_FLING_THRESHOLD = 3;
private boolean isPositive;

public FlingBehavior() {
}

public FlingBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
    if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
        velocityY = velocityY * -1;
    }
    if (target instanceof RecyclerView && velocityY < 0) {
        final RecyclerView recyclerView = (RecyclerView) target;
        final View firstChild = recyclerView.getChildAt(0);
        final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
        consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
    }
    return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
    isPositive = dy > 0;
  }
}
<android.support.design.widget.AppBarLayout
app:layout_behavior="package.FlingBehavior"
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="250dip"
android:fitsSystemWindows="true">