From an NavigationDrawer
Android Studio template I have replaced the menu with an Expandable RecyclerView, now I would like to add an event OnClick
to be able to change the fragments in the main content. I already have an elaborate menu; I don't know where to add the transactions for each fragment change. I have not found a similar example on the web, in some cases they do it with a toast. I have for example two fragments named: Biblioteca
and Evu
. Below is the code of my project.
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);
}
}
}
In MainActivity
it is where the 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);
}
}
View Holder
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);
}
}
The answer I am going to give is almost the same as the one I gave in this question , with the difference that in this one, the class
ViewHolder
is declared outside the adapter and that is why you have to pass the interface that is created in the adapter as a parameter in its constructor. to be able to capture the onClick events that are generated when pressing the items of theRecyclerView
.To achieve what you want you have to:
OnClickListener
in the ViewHolder class.onClick()
the OnClickListener interface method.setOnClickListener()
you declared in the adapter.MenuAdapter
ItemMenuViewHolder
MainActivity