RecyclerView:在AppBarLayout向上滚动时填充屏幕高度(隐藏)
问题描述:
我有AppBarLayout,带有工具栏和2个按钮。当我滚动RecyclerView并显示当我向下滚动时隐藏。 问题是,当AppBarLayout隐藏时,RecyclerView设置为低于AppBarLayout并保留其位置,并且图片上显示条件。我需要将我的RecyclerView滚动到状态栏,然后在向下滚动时返回原始位置。到目前为止,我甚至找不到这样的例子。 谢谢。 RecyclerView:在AppBarLayout向上滚动时填充屏幕高度(隐藏)
这是MT布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ru.skyeng.listening.AudioFiles.AudioListActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
>
<ImageButton
android:id="@+id/action_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gear"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_gravity="end"/>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_category"
android:layout_width="158dp"
android:layout_height="36dp"
android:layout_margin="16dp"
android:background="@drawable/selector_button_category"
android:text="@string/categories"
android:textColor="@color/colorBlue2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:id="@+id/button_length"
android:layout_width="158dp"
android:layout_height="36dp"
android:layout_margin="16dp"
android:background="@drawable/selector_button_length"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="@string/length"
android:textColor="@color/colorBlue2"/>
<View
android:id="@+id/shadow"
android:layout_below="@+id/button_length"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@drawable/shadow">
</View>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_below="@+id/appBarLayout"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="24dp">
</FrameLayout>
</RelativeLayout>
答
如果你想要一个图像显示在波纹管的工具栏。你应该尝试CollpsingLayoutToolbar。
这里是一个例子。我希望这可以帮助
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:elevation="0dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapsedTitleGravity="left"
app:expandedTitleGravity="bottom|right"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@style/CollapsedAppBarStyle"
app:layout_scrollFlags="scroll"
app:title="@string/pod">
<ImageView
android:id="@+id/collapsing_ImageView"
android:layout_width="match_parent"
android:layout_height="256dp"
android:contentDescription="@string/app_name"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/bg_image"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
你能告诉我们你的XML吗? –
你需要使用CoordinatorLayout - CollapsingToolbarLayout - 在这里检查9下如何做到这一点http://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/ – Tasos
@Tasso,这绝对有帮助。如果你为别人写了一些解释,我会将你的答案标记为已接受。万分感谢。 – Sermilion