Toolbar
ToolBar initialization
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar); toolbar.setTitle(""); setSupportActionBar(toolbar); // Set up App Logo toolbar.setLogo(R.mipmap.dog); // Set Menu toolbar.inflateMenu(R.menu.menu); /** To use menu, you need to override the onCreateOptionsMenu() method To listen to Menu, you need to override the onoptionsiteselected() method */
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
RecyclerView
Enter the application default display list mode for the first time
adapter = new MyAdapter(this,R.layout.list_item,models); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(adapter);
- 1
- 2
- 3
- 4
- 5
Click menu of toolbar to switch modes, and make logical judgment in the method of monitoring menu of toolbar
/** Define a variable to hold the current state */ switch (item.getItemId()){ case R.id.item: /** Two constants are defined, meaning list mode and grid mode respectively */ if(currentMode == MODE_GRID){ /** Dynamically replace the icon and change the current mode */ item.setIcon(R.mipmap.list); currentMode = MODE_LIST; /** Call the method in the adapter to refactor the layout and reset the adapter */ adapter.reBuildLayout(R.layout.grid_item); mRecyclerView.setLayoutManager(new GridLayoutManager(this,2)); mRecyclerView.setAdapter(adapter); }else if(currentMode == MODE_LIST){ item.setIcon(R.mipmap.grid0); currentMode = MODE_GRID; adapter.reBuildLayout(R.layout.list_item); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(adapter); } break; } return super.onOptionsItemSelected(item);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
Reconfiguration layout method in adapter
public void reBuildLayout(int layoutId){ this.mLayoutResId = layoutId; notifyDataSetChanged(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Design sketch
······
/**The above effect is the optimized effect 1. Added split line 2. Save the current position when switching */
- 1
- 2
- 3
- 4
- 5
- 6
Source code
---------------------This article is from the CSDN blog of Konfyt_Android. For the full address, please click: https://blog.csdn.net/Konfyt_Android/article/details/52734866?utm_source=copy