A partir de una plantilla NavigationDrawer
de Android Studio he reemplazado el menú por un RecyclerView Expandible, ahora quisiera agregar evento OnClick
para poder cambiar los fragmentos en el contenido principal. Ya tengo un menú elaborado; no sé donde agregar las transacciones para cada cambio de fragment. en la web no he encontrado un ejemplo parecido, en algunos casos lo hacen con un toast.
Tengo por ejemplo dos fragmentos nombrados: Biblioteca
y Evu
.
A continuación el código de mi proyecto.
public class MenuAdapter extends ExpandableRecyclerViewAdapter<MenuCategoriaViewHolder, ItemMenuViewHolder> implements View.OnClickListener {
private View.OnClickListener listener;
public MenuAdapter(List<? extends ExpandableGroup> groups) {
super(groups);
}
@Override
public MenuCategoriaViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_categoria, parent, false);
return new MenuCategoriaViewHolder(view);
}
@Override
public ItemMenuViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_subcategoria, parent, false);
view.setOnClickListener(this);
return new ItemMenuViewHolder(view);
}
@Override
public void onBindChildViewHolder(ItemMenuViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
ItemMenu itemMenu = (ItemMenu) group.getItems().get(childIndex);
holder.setItemName(itemMenu.getItemName());
holder.setIcono(itemMenu.getImagenId());
}
@Override
public void onBindGroupViewHolder(MenuCategoriaViewHolder holder, int flatPosition, ExpandableGroup group) {
holder.setCategoriaName(group.getTitle());
}
public void setOnClickListener(View.OnClickListener listener){
this.listener=listener;
}
@Override
public void onClick(View view) {
if (listener!=null){
listener.onClick(view);
}
}
}
En el MainActivity
es donde se carga el MenuRecyclerView
:
public class MainActivity extends AppCompatActivity {
//variables recyclerview
private RecyclerView mRecyclerView;
private MenuAdapter mAdapter;
private List<MenuCategoria> menuCategorias;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//RecyclerView en el navigationDrawer
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
getCateg();
mAdapter = new MenuAdapter(menuCategorias);
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mRecyclerView.setAdapter(mAdapter);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
}
public void getCateg() {
menuCategorias = new ArrayList<>(1);
List<ItemMenu> itemMenus = new ArrayList<>(5);
itemMenus.add(new ItemMenu("Home",R.mipmap.home)); // comentarié los demás submenus para ensayar solo con uno
itemMenus.add(new ItemMenu("Biblioteca",R.mipmap.biblioteca));
itemMenus.add(new ItemMenu("EduVirtual",R.mipmap.eduvirtual));
itemMenus.add(new ItemMenu("Directorio",R.mipmap.directorio));
itemMenus.add(new ItemMenu("Preguntas",R.mipmap.preguntasf));
menuCategorias.add(new MenuCategoria("Portal", itemMenus));
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ViewHolder
public class ItemMenuViewHolder extends ChildViewHolder {
private TextView itemName;
private ImageView icono;
public ItemMenuViewHolder(View itemView) {
super(itemView);
itemName = (TextView)itemView.findViewById(R.id.list_item_name);
icono = (ImageView)itemView.findViewById(R.id.list_item_scate_icon);
}
public void setItemName(String name){
itemName.setText(name);
}
public void setIcono(int idResource){
icono.setImageResource(idResource);
}
}