@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("currentOffset", demoUtils.currentOffset); outState.putInt("itemCount", adapter.getCount()); for (int i = 0; i < adapter.getCount(); i++) { outState.putParcelable("item_" + i, (Parcelable) adapter.getItem(i)); } }
@Override protected void onRestoreInstanceState(@NotNull Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); demoUtils.currentOffset = savedInstanceState.getInt("currentOffset"); int count = savedInstanceState.getInt("itemCount"); List<DemoItem> items = new ArrayList<>(count); for (int i = 0; i < count; i++) { items.add((DemoItem) savedInstanceState.getParcelable("item_" + i)); } adapter.setItems(items); }
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.one_column) { setNumColumns(1); } else if (id == R.id.two_columnns) { setNumColumns(2); } else if (id == R.id.three_columns) { setNumColumns(3); } else if (id == R.id.four_columns) { setNumColumns(4); } else if (id == R.id.five_columns) { setNumColumns(5); } else if (id == R.id.onetwenty_dp_columns) { setColumnWidth(120); } else if (id == R.id.twoforty_dp_columns) { setColumnWidth(240); } else if (id == R.id.append_items) { adapter.appendItems(demoUtils.moarItems(50)); } else if (id == R.id.reset_items) { demoUtils.currentOffset = 0; adapter.setItems(demoUtils.moarItems(50)); } else if (id == R.id.reordering) { listView.setAllowReordering(!listView.isAllowReordering()); item.setTitle(listView.isAllowReordering() ? "Prevent reordering" : "Allow reordering"); } else if (id == R.id.debugging) { int index = listView.getFirstVisiblePosition(); View v = listView.getChildAt(0); int top = (v == null) ? 0 : v.getTop(); listView.setDebugging(!listView.isDebugging()); item.setTitle(listView.isDebugging() ? "Disable debugging" : "Enable debugging"); listView.setAdapter(adapter); listView.setSelectionFromTop(index, top); } else if (id == android.R.id.home) { drawerLayout.openDrawer(GravityCompat.START); } return super.onOptionsItemSelected(item); }