<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="4" android:gravity="center" > </GridView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/gridButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:drawableTop="@drawable/ic_launcher" /> </LinearLayout>
private void SavePreferences(String key, String value) { SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, value); editor.commit(); } private String LoadPreferences(String key) { SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); String loadedString = sharedPreferences.getString(key, "list"); return loadedString; }
setContentView(R.layout.main); content = new String[] {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10"}; viewGroup = (ViewGroup) findViewById(R.id.mainLayout); list = getLayoutInflater().inflate(R.layout.list, null); grid = getLayoutInflater().inflate(R.layout.grid, null); if(LoadPreferences("VIEW").equalsIgnoreCase("list")) setList(); else setGrid();
private void setList() { viewGroup.removeAllViews(); viewGroup.addView(list); ListView listView = (ListView) list.findViewById(R.id.listLayout); listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.textItem, content)); } private void setGrid() { viewGroup.removeAllViews(); viewGroup.addView(grid); GridView gridview = (GridView) findViewById(R.id.gridLayout); gridview.setAdapter(new ArrayAdapter<String>(this, R.layout.grid_item, R.id.gridButton, content)); }
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item" android:title="Change view"></item> </menu>
@Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(LoadPreferences("VIEW").equalsIgnoreCase("list")) { setGrid(); SavePreferences("VIEW", "grid"); } else { setList(); SavePreferences("VIEW", "list"); } return true; }
Source: https://habr.com/ru/post/150143/
All Articles