将 AppBar 触发回到其在 CoordinatorLayout 中的正常位置
Trigger AppBar back to its normal position inside CoordinatorLayout
我有下一个布局:
<RelativeLayout 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.CoordinatorLayout
android:id="@+id/layout_offer_detail_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_offer_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways|snap" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/layout_offer_detail_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</>
</>
</>
这将在用户在列表中向下滚动时隐藏应用栏,并在用户向上滚动时显示它。
我的问题是,在 NestedScrollView
的末尾,我有一个 "Back to top" 按钮,单击该按钮会触发 scrollView 向上滚动:scrollView.smoothScrollTo(0, 0);
问题是 appBar 没有像用户滚动到顶部时那样返回。我无法以编程方式触发应用栏。
有什么想法吗?!
编辑
功能取自此处:see link
您必须使用折叠工具栏才能实现此目的。
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
这比我想象的要容易。经过三个小时的搜索和尝试,我不得不这样做:
appBarLayout.setExpanded(true, true);
好吧,这对我来说是不必要的浪费时间...
我有下一个布局:
<RelativeLayout 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.CoordinatorLayout
android:id="@+id/layout_offer_detail_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_offer_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways|snap" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/layout_offer_detail_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</>
</>
</>
这将在用户在列表中向下滚动时隐藏应用栏,并在用户向上滚动时显示它。
我的问题是,在 NestedScrollView
的末尾,我有一个 "Back to top" 按钮,单击该按钮会触发 scrollView 向上滚动:scrollView.smoothScrollTo(0, 0);
问题是 appBar 没有像用户滚动到顶部时那样返回。我无法以编程方式触发应用栏。
有什么想法吗?!
编辑
功能取自此处:see link
您必须使用折叠工具栏才能实现此目的。
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
这比我想象的要容易。经过三个小时的搜索和尝试,我不得不这样做:
appBarLayout.setExpanded(true, true);
好吧,这对我来说是不必要的浪费时间...