我有一个很长ListView的用户可以在返回上一个屏幕之前滚动.当用户ListView再次打开它时,我希望列表滚动到之前的相同点.关于如何实现这一点的任何想法?
我对Android的RecyclerView.State有疑问.
我正在使用RecyclerView,我如何使用它并将其与RecyclerView.State绑定?
我的目的是保存RecyclerView的滚动位置.
我有一个Recycler视图,它位于SwipeRefreshLayout内部.此外,还可以打开另一个活动中的每个项目.返回Recycler后,我需要滚动到所选项目,或者前一个Y.如何做到这一点?
是的,我用Google搜索,在StackOverFlow中找到关于保存布局管理器实例的文章,如下所示:RecyclerView在活动之间存储/恢复状态.但是,它对我没有帮助.
UPDATE
现在我有这种解决问题,当然,它也没有用.
private int scrollPosition;
...//onViewCreated - it is fragment
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
data = new ArrayList<>();
adapter.setData(getActivity(), data);
recyclerView.setAdapter(adapter);
...
@Override
public void onResume() {
    super.onResume();
    recyclerView.setScrollY(scrollPosition);
}
@Override
public void onPause() {
    super.onPause();
    scrollPosition = recyclerView.getScrollY();
}
是的,我尝试过scrollTo(int,int) - 不行.
现在我尝试滚动,例如,Y = 100,但它根本不滚动.
我已经实现了一个应用程序,我RecyclerView在fragments. 每个项目上有 3 个按钮、1 个图像和一些文本。如果我滚动并单击“查看个人资料”按钮,则会打开新活动,该活动正在成功运行,但是当我回拨按钮时,该项目将进入开始位置。我需要物品会出现在同一位置。
物品图片

片段代码
public class Broader_Match_Tab extends Fragment{
int lastVisibleItemPosition;
SessionManager session;
private List<SuperHero> listSuperHeroes;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public ProgressBar progressBar;
private RequestQueue requestQueue;
private int requestCount1 = 1;
private Boolean isStarted = false;
private Boolean isVisible = false;
boolean isLastPageLoaded = false;
public String email;
TextView tvMSG;
public Broader_Match_Tab() {}
@Override
public void onStart() {
    super.onStart();
    isStarted = true;
    if (isVisible && isStarted){
        getData();
    } …要求主持人:这不是重复的问题,请阅读以下信息。
在问这个问题之前,我已经尝试了几乎所有可用的SO解决方案,但是没有一个对我有用,有的导致崩溃而有的根本不起作用(我是初学者,有可能我已经在我的代码中做错了什么,我不怪任何人在SO上无法得到的答案都不能在我的情况下工作)。以下是我的代码,请看一下:
fragment_tab1.xml(包含recyclerview):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#333333"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="96dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:background="@color/colorPrimaryDark"
            android:id="@+id/sort1"
            android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sort"
            android:layout_marginLeft="10dp"
            android:fontFamily="@font/quicksand"
            android:textStyle="bold"
            android:text="Sort by:"
            android:textColor="#ffffff"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                style="@style/Widget.AppCompat.Button.Colored"
                android:text="Oldest first"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                style="@style/Widget.AppCompat.Button.Colored"
            android:text="Newest first"/>
        </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_below="@id/sort1"
        android:clipToPadding="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:padding="5dp" />
    </RelativeLayout>
</LinearLayout>
Fragmenttab1.java
public class tab1  extends Fragment {
    RecyclerView mRecyclerView;
    FirebaseDatabase mFirebaseDatabase;
    DatabaseReference mRef;
    LinearLayoutManager manager;
    private static final String TAG = "tab1";
    ProgressDialog …