[英]Setting RecyclerView scrolling position on Android
使用本教程实现“灵活空间”模式(带有折叠工具栏的模式)
一切工作正常,除了我要实现的功能尚不可用:我希望能够像在Lollipop上的“联系人”屏幕中那样扩展或收缩ImageView标头,用户可以在其中进行扩展或缩小。缩小联系人的图像。
现在,活动开始后,图像将达到其完整大小,即AppBar
的大小(由于我以编程方式进行了设置,因此其大小为0)。
我想从最小化image view / appbar
(无论您要查看什么),即不仅可以向下滚动(如本教程中一样,因此图像大小在开始时为全尺寸,然后缩小) ),但也可以向上滚动布局以显示更多图片标题。 现在这是不可能的,因为该活动以全尺寸的image / appbar
开始,所以我无法再对其进行扩展。
mRecyclerView = (RecyclerView) findViewById(R.id.activity_profile_bottom_recyclerview);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
if(mAdapter == null){
mAdapter = new ProfileAdapter(this, user, inEditMode);
mRecyclerView.setAdapter(mAdapter);
}
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
适配器的长度为4个项目,甚至不需要半个屏幕。 那是我的xml布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="0dp" // set programatically
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/header"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/anim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/activity_profile_bottom_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<include
layout="@layout/navigation_view"/>
</android.support.v4.widget.DrawerLayout>
为什么scrollToPosition
不起作用? 也许因为我只有4个项目(这就是适配器的项目数会检索到的)? 还尝试了scrollToPositionWithOffset
。
注意:如果我手动向上滚动,则RecyclerView会上升,它将无法通过代码运行。
我也很乐意听到其他方式来实现我想要的功能,除了以编程方式滚动外。
嗯,尝试添加更多项目并使用以下代码:
recycleView.scrollTo(int x, int y)
要么
recycleView.scrollTo(int x, int y)
要么
recycleView.scrollToPosition(int position)
对于滚动问题,您需要将Recycler视图放在ScrollView中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.