RecyclerView的動(dòng)態(tài)切換布局模式(宮格<-->列表)

對(duì)于RecyclerView的用法就不再累贅,今天主要是講一下仿小米自帶便簽里的宮格和列表的切換模式

給大家看下小米便簽的
image

image

對(duì)于模式的切換,我想大家都有個(gè)了解
瀑布流采用的是

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);    //兩列且垂直方向布局
recyclerView.setLayoutManager(layoutManager);

列表模式采用的是

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);    //垂直方向布局
recyclerView.setLayoutManager(layoutManager);

對(duì)于兩種模式的切換,就需要添加一個(gè)判斷來判定切換哪種模式

private boolean flag =true; //默認(rèn)的標(biāo)識(shí)

建立一個(gè)方法,用于切換的判斷

private void changeLayout(boolean isChange) {
        //布局切換方法
        //如果isChange == true 就調(diào)用瀑布流模式,反之調(diào)用列表模式
        if (isChange) {
            //瀑布流設(shè)置
            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
            layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
            recyclerView.setLayoutManager(layoutManager);
            adapter = new NoteAdapter(noteBeanList, MainActivity.this, R.layout.item_note);
            adapter.notifyItemInserted(0);
            recyclerView.setAdapter(adapter);
        } else {
            //列表模式
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(layoutManager);
            adapter = new NoteAdapter(noteBeanList, MainActivity.this, R.layout.item_note);
            adapter.notifyItemInserted(0);
            recyclerView.setAdapter(adapter);
        }
    }

小米便簽的切換位置在menu中,其中的標(biāo)題會(huì)隨切換更改

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.list_mode:
                if (flag) {
                    if (item.getTitle().equals("列表模式")) {
                        item.setTitle("宮格模式");
                        flag = !flag;
                        changeLayout(flag);
                    } else {
                        item.setTitle("列表模式");
                        flag = !flag;
                        changeLayout(flag);
                    }
                    FindAll(); //切換布局后需要重新加載數(shù)據(jù),否則會(huì)出現(xiàn)切換后點(diǎn)擊數(shù)據(jù)報(bào)空指針異常
                } else {
                    if (item.getTitle().equals("列表模式")) {
                        item.setTitle("宮格模式");
                        flag = !flag;
                        changeLayout(flag);
                    } else {
                        item.setTitle("列表模式");
                        flag = !flag;
                        changeLayout(flag);
                    }
                    FindAll();
                }
                break;
            default:
                break;
        }
        return true;
    }

基本功能就是這樣,但是只是這樣的話會(huì)發(fā)現(xiàn),每次重啟應(yīng)用后都會(huì)恢復(fù)默認(rèn)的模式,那么怎么記錄下模式,在重啟應(yīng)用后是之前切換的布局呢?
這里就可以采用SharedPreferences存儲(chǔ),在退出應(yīng)用時(shí)把切換的flag表示記錄下來,在重啟應(yīng)用時(shí)把標(biāo)識(shí)取出來,再初始化模式就好了
在onDestroy重載方法中:

@Override
protected void onDestroy() {
    super.onDestroy();
    SharedPreferences.Editor editor = getSharedPreferences("flag", MODE_PRIVATE).edit();
    editor.putBoolean("flag", flag);
    Log.e("onDestroy", flag + "");
    editor.apply();
}

這樣就在應(yīng)用退出后保存了切換的模式
然后在onCreate重載方法中從SharedPreferences讀取保存的標(biāo)識(shí)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    SharedPreferences prefer = getSharedPreferences("flag", MODE_PRIVATE);
    flag = prefer.getBoolean("flag", true);
    Log.e("onCreate", flag +"");
    FindAll();    //數(shù)據(jù)加載的方法
}

基本的內(nèi)容就是這樣,有個(gè)問題,如果在Intent的Activity中新增了一個(gè)數(shù)據(jù),返回主界面時(shí),會(huì)發(fā)現(xiàn)新增的數(shù)據(jù)沒有顯示,此時(shí)就跟Activity的生命周期有關(guān)了,從Intent的Activity返回主界面,首先調(diào)用的就是onRestart重載方法,為了讓新增的數(shù)據(jù)顯示,需要在此方法中調(diào)用加載數(shù)據(jù)的方法

@Override
protected void onRestart() {
    super.onRestart();
    //數(shù)據(jù)重載
    FindAll();
}

最后給出FindAll方法的部分內(nèi)容

private void FindAll() {
        //從LitePal查找所有數(shù)據(jù)
        noteBeanList.clear();
        LitePal.findAllAsync(NoteBean.class).listen(new FindMultiCallback<NoteBean>() {
            @Override
            public void onFinish(List<NoteBean> list) {
                noteBeanList = list;
                Log.e("數(shù)量", noteBeanList.size() + "");
                //布局模式切換
                changeLayout(flag);
                adapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(NoteBean note, int position) {
                        ...//item的界面跳轉(zhuǎn)
                    }
                });
            }
        });
    }

這樣就完成了一個(gè)仿小米便簽切換布局模式
對(duì)這個(gè)編輯器不夠了解,代碼間距可能有點(diǎn)問題,但問題不大,主要是思路
有錯(cuò)誤請(qǐng)各位大佬指教

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容