I have a list but I want to make an option for the users to view it in List or Grid. The choices are in the menu. Here's the menu_user_list.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.user.UserListActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_listview" android:title="List" app:showAsAction="never" android:onClick="showlist" android:orderInCategory="200"/>
<item android:id="@+id/action_gridview" android:title="Grid" app:showAsAction="never" android:onClick="showgrid" android:orderInCategory="300"/>
How could I do this?
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_user_list_recyclerView);
The R.id.activity_user_list_recyclerView is located on the Activity layout. It is working fine there. My main point is that how could set the menu that it will change the layout to either of the these two:
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
The adapter works fine. I just need to figure out how the onclick will change the layout. Thanks
If you are using a Toolbar but not set as the ActionBar with AppCompat you would handle this by adding a menu to your toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
toolbar.inflateMenu(R.menu.my_menu);
// Then set an OnMenuItemClickListener
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
switch(itemId){
case R.id.action_settings:
// handle settings
break;
case R.id.action_listview:
// Setup the LinearLayoutManager
initListDisplay();
break;
case R.id.action_gridview:
// Setup the GridLayoutManager
initGridDisplay();
break;
}
return true;
}
});
if your using the toolbar as your ActionBar i.e setSupportActionBar(toolbar);
then you would just inflate the menu regularly and add onOptionsItemSelected(MenuItem):
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch(id){
case R.id.action_settings:
// handle settings
break;
case R.id.action_listview:
// Setup the LinearLayoutManager
initListDisplay();
break;
case R.id.action_gridview:
// Setup the GridLayoutManager
initGridDisplay();
break;
}
return super.onOptionsItemSelected(item);
}
Then i would use something like this to do the work:
// Display a list
private void initListDisplay(){
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
// Display the Grid
private void initGridDisplay(){
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
layoutManager.setOrientation(GridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
}
For simplicity, i have left out the few other options you have to inflate menus with ActionBar or ActionBarSherlock, but the onMenuItemClick is pretty standard so, just use what you can with the switch statement.
Good Luck and Happy Coding!