如何以编程方式在 CoordinatorLayout 中显示折叠的元素?
How to show collapsed elements in CoordinatorLayout programmatically?
我 CoordinatorLayout
如博客所述:http://android-developers.blogspot.ru/2015/05/android-design-support-library.html
<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.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways">
<TextView
...
app:layout_scrollFlags="scroll|enterAlways">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
在 AppBarLayout
内,我有 Toolbar
和 TextView
以及其他状态信息。 AppBarLayout
可以折叠(滚动后)。有时我需要显示 AppBarLayout
以显示已更改的状态。
如何以编程方式进行?
由于 Tuấn Trần Anh, founded 的回答,您可以使用这两种方法以编程方式折叠和扩展 CoordinatorLayout
:
public void collapseToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.onNestedFling(rootLayout, appbarLayout, null, 0, 10000, true);
}
}
public void expandToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.setTopAndBottomOffset(0);
behavior.onNestedPreScroll(rootLayout, appbarLayout, null, 0, 1, new int[2]);
}
}
在我的例子中,我使用这个解决方案
public void resetAppBarLayout() {
if (mContentContainer != null) {
final AppBarLayout.ScrollingViewBehavior container_behavior = ((AppBarLayout.ScrollingViewBehavior)
((CoordinatorLayout.LayoutParams) mContentContainer.getLayoutParams()).getBehavior());
if (container_behavior != null) {
container_behavior.setTopAndBottomOffset(appBarLayout.getTotalScrollRange());
}
final AppBarLayout.Behavior appbar_behavior = ((AppBarLayout.Behavior)
((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior());
if (appbar_behavior != null) {
appbar_behavior.setTopAndBottomOffset(0);
}
}
}
强制显示应用栏布局。将滚动视图重置为 appbarLayout 最大滚动范围并将 appBarLayout 重置为起始位置 = 0.
如所述:
Using support libs v23 you can call appBarLayout.setExpanded(true/false)
我 CoordinatorLayout
如博客所述:http://android-developers.blogspot.ru/2015/05/android-design-support-library.html
<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.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways">
<TextView
...
app:layout_scrollFlags="scroll|enterAlways">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
在 AppBarLayout
内,我有 Toolbar
和 TextView
以及其他状态信息。 AppBarLayout
可以折叠(滚动后)。有时我需要显示 AppBarLayout
以显示已更改的状态。
如何以编程方式进行?
由于 Tuấn Trần Anh, founded CoordinatorLayout
:
public void collapseToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.onNestedFling(rootLayout, appbarLayout, null, 0, 10000, true);
}
}
public void expandToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.setTopAndBottomOffset(0);
behavior.onNestedPreScroll(rootLayout, appbarLayout, null, 0, 1, new int[2]);
}
}
在我的例子中,我使用这个解决方案
public void resetAppBarLayout() {
if (mContentContainer != null) {
final AppBarLayout.ScrollingViewBehavior container_behavior = ((AppBarLayout.ScrollingViewBehavior)
((CoordinatorLayout.LayoutParams) mContentContainer.getLayoutParams()).getBehavior());
if (container_behavior != null) {
container_behavior.setTopAndBottomOffset(appBarLayout.getTotalScrollRange());
}
final AppBarLayout.Behavior appbar_behavior = ((AppBarLayout.Behavior)
((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior());
if (appbar_behavior != null) {
appbar_behavior.setTopAndBottomOffset(0);
}
}
}
强制显示应用栏布局。将滚动视图重置为 appbarLayout 最大滚动范围并将 appBarLayout 重置为起始位置 = 0.
如
Using support libs v23 you can call
appBarLayout.setExpanded(true/false)