@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); listView = (AsymmetricGridView) findViewById(R.id.listView); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); actionBar.setHomeAsUpIndicator(R.drawable.ic_menu); actionBar.setDisplayHomeAsUpEnabled(true); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); if (navigationView != null) { setupDrawerContent(navigationView); } if (USE_CURSOR_ADAPTER) { if (savedInstanceState == null) { adapter = new DefaultCursorAdapter(this, demoUtils.moarItems(50)); } else { adapter = new DefaultCursorAdapter(this); } } else { if (savedInstanceState == null) { adapter = new DefaultListAdapter(this, demoUtils.moarItems(50)); } else { adapter = new DefaultListAdapter(this); } } listView.setRequestedColumnCount(3); listView.setRequestedHorizontalSpacing(Utils.dpToPx(this, 3)); listView.setAdapter(getNewAdapter()); listView.setDebugging(true); listView.setOnItemClickListener(this); }
@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); }
private void setColumnWidth(int columnWidth) { listView.setRequestedColumnWidth(Utils.dpToPx(this, columnWidth)); listView.determineColumns(); listView.setAdapter(getNewAdapter()); }
private void setNumColumns(int numColumns) { listView.setRequestedColumnCount(numColumns); listView.determineColumns(); listView.setAdapter(getNewAdapter()); }