Android:在应用重新打开后还原RecyclerView项目位置

时间:2018-09-05 03:54:42

标签: android android-recyclerview linearlayoutmanager

我有一个CardView项目的RecyclerView列表。我将CardView数据保存到SQLite数据库。用户可以在列表中上下拖动CardViews来更改项目的顺序。当用户退出应用程序时,我想保存RecyclerView项目的当前顺序。然后,当用户重新打开应用程序时,我想恢复RecyclerView项目的确切顺序。

我已经尝试了多种基于其他SO帖子的方法,但是没有运气:

-How to save RecyclerView's scroll position using RecyclerView.State?

-RecyclerView store / restore state between activities

-How to save scroll position of RecyclerView in Android?

每次重新打开应用程序时,我得到的是基于CardView原始时间戳的默认订单。它在列表顶部显示最新的CardView项,然后降到最旧的CardView的最后一项。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    private ArrayList<ListItem> allList;
    private RecyclerView mRecyclerView;
    private SQLiteDB sqLiteDB;
    private MyRecylerAdapter adapter;
    private static final String KEY_RECYCLER_STATE = "recycler_state";
    private Parcelable recyclerViewState;

    protected void onCreate(Bundle savedInstanceState) {

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        allList = new ArrayList<>();
        allList.clear();
        allList = sqLiteDB.getAllDBItems();
        adapter = new MyRecylerAdapter(this, allList);
        mRecyclerView.setAdapter(adapter);
    }

    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState); 

        outState.putParcelable(KEY_RECYCLER_STATE, mRecyclerView.getLayoutManager().onSaveInstanceState());     
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        recyclerViewState = savedInstanceState.getParcelable(KEY_RECYCLER_STATE);
    }    

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

        if (mRecyclerView !=null) {
            mRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
        }
    }
}    

public class SQLiteDB extends SQLiteOpenHelper {

    ...
    public ArrayList<ListItem> getAllDBItems() {
    ArrayList<ListItem> modelList = new ArrayList<>();

    SQLiteDatabase db = getReadableDatabase();        

    String[] columns = {
            ItemContract.ItemEntry.A,
            ItemContract.ItemEntry.B,
            ItemContract.ItemEntry.C,
            ItemContract.ItemEntry.D,
            ItemContract.ItemEntry.E,
            ItemContract.ItemEntry.F,
            ItemContract.ItemEntry.G,
            ItemContract.ItemEntry.H,
            ItemContract.ItemEntry.I,
            ItemContract.ItemEntry.J
    };

    Cursor getCursor = db.query(
            TABLE_NAME, 
            columns, 
            null,    
            null,   
            null,   
            null,   
            null    
    );

    try {
        if (getCursor.getCount() > 0) {
            getCursor.moveToFirst(); 

            while (!getCursor.isAfterLast()) {

                do { 
                    ListItem listItem = new ListItem();
                    listItem.setId(Integer.parseInt(getCursor.getString(getCursor.getColumnIndex(A))));
                    listItem.setType(getCursor.getString(getCursor.getColumnIndex(B)));
                    listItem.setTypeColor(Integer.parseInt(getCursor.getString(getCursor.getColumnIndex(C))));
                    listItem.setTodo(getCursor.getString(getCursor.getColumnIndex(D)));
                    listItem.setNote1(getCursor.getString(getCursor.getColumnIndex(E)));
                    listItem.setNote2(getCursor.getString(getCursor.getColumnIndex(F)));
                    listItem.setDuedate(getCursor.getString(getCursor.getColumnIndex(G)));
                    listItem.setDuetime(getCursor.getString(getCursor.getColumnIndex(H))); listItem.setTimestamp(Long.parseLong(getCursor.getString(getCursor.getColumnIndex(I))));
                    listItem.setRandint(Integer.parseInt(getCursor.getString(getCursor.getColumnIndex(J))));
                  modelList.add(0,listItem);
                } while (getCursor.moveToNext());
            }
        }
    } finally {
        if (getCursor != null && !getCursor.isClosed()) {
            getCursor.close();
        }
    } if(db.isOpen()) {
        db.close();
    }
    return modelList;
} 

public class ListItem {

    private int _id;
    private int _sortorder;

    public void setSortorder(int sortorder) {
    _sortorder = sortorder;

    } 
}

1 个答案:

答案 0 :(得分:1)

在数据库表中添加一个名为“ SortOrder”的字段,并在每次添加新行时递增此列中的值。 (您可以使用MAX函数来确保该值始终是当前最高值的下一个)。

检索项目时按此列排序。

将卡片拖到新位置后放下时,请使用新的排序顺序/位置更新该字段。