简体   繁体   English

recyclerView如何保持其滚动位置而无需任何额外的编码

[英]how is the recyclerView maintains its scroll position without any extra coding

Created a simple activity which has a recyclerView and displays the data list. 创建了一个具有recyclerView并显示数据列表的简单活动。 When turned on the setting's 'don't keep activity alive' and set 'Background process limit' to 'No background process' . 启用该设置后, 'don't keep activity alive' ,并将'Background process limit''No background process' Expect the acitivity will be killed by os in background. 期望活动将在后台被os杀死。

When minimize the app and reopen it, saw the activity's onCreate() is called on a new instance and it does all again to create a new set of recyclerView and adapter with new datalist. 当最小化应用程序并重新打开它时,看到活动的onCreate()在新实例上被调用,并且再次执行所有操作以创建一组新的recyclerView和带有新数据列表的适配器。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        mRecyclerView = findViewById(R.id.recyclerView);
        setSupportActionBar(toolbar);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
    }

But the ui still shows the item in last scrolled position (correctly restored). 但是ui仍将项目显示在最后滚动的位置(正确还原)。

Anyone knows how the previous position is transfered to the new recyclerView instance, etc.? 有人知道先前的职位如何转移到新的recyclerView实例等吗?

the layout: 布局:

<LinearLayout 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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextColor="@android:color/white"
         />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>
</LinearLayout>

the activity: 活动:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        mRecyclerView = findViewById(R.id. recyclerView);
        setSupportActionBar(toolbar);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager); 
    }


    @Override
    protected void onResume() {
        super.onResume();

        new FetchItemASyncTask(this).execute();
        // in the asyncTask it will do setup adapter when data ready
        // mAdapter = new ItemAdapter(dataList)
        // mRecyclerView.setAdapter(mAdapter);
    }
}

This is part of the default saving of state that Android performs for many views. 这是Android对许多视图执行的默认状态保存的一部分。 The only thing you need to do to enable this is to give the view an android:id attribute (if you take that id away, you'll notice that it no longer saves the scroll position). 启用此功能所需要做的唯一一件事就是为视图提供android:id属性(如果删除该ID,您会注意到它不再保存滚动位置)。 The same is true for eg the text entered into an EditText . 对于输入到EditText的文本也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在保持滚动位置的同时更新RecyclerView - Update RecyclerView while keeping its scroll position 刷新 RecyclerView 中的数据并保持其滚动位置 - Refreshing data in RecyclerView and keeping its scroll position 如何在 recyclerview 中使用动态 position 滚动 RecyclerView 的 smoothScrollToPosition? - How to scroll with smoothScrollToPosition of RecyclerView with dynamic position in recyclerview? RecyclerView 在没有动画的情况下滚动到所需位置 - RecyclerView scroll to desired position without animation 如何在3个位置RecyclerView之后添加额外的View? - How to add extra View after 3 position RecyclerView? 如何在第一个索引处添加项目后保留在RecyclerView中的滚动位置? - How to remain at a scroll position in RecyclerView after adding items at its first index? 在第一个索引处添加项目并调用notifydatasetchange后,如何在RecyclerView中保持滚动位置 - How to remain at a scroll position in RecyclerView after adding items at its first index and call notifydatasetchange 如何使RecyclerView滚动到onCreate的最后一个位置? - How to make RecyclerView scroll to last position in onCreate? 如何在片段中保存recyclerview的滚动位置 - How to save scroll position of recyclerview in fragment 如何维护屏幕旋转的recyclerView的Scroll position - How to Maintain Scroll position of recyclerView on Screen Rotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM