Recently I have been working with Android therefore I am still a newbie as far as this is concerned. I wanted to make a small application with a Navegation Drawer taking as an example the following project: link
It works very well and everything, but as you can see the two submenus are exactly the same (Which takes away the sense of one of them), therefore I decided to make them different.
To do this, simply edit the class Constant
and add another array with the other submenus. In this case "subName2"
public class Constant {
public static String [] name = {"Android", "iOS"};
public static String [] subName = {"google", "Motorola", "Samsung", "Lenevo"};
public static String [] subName2 = {"Alan", "Chris", "Pepe"};
}
The code to separate them is 2 different menus is as follows:
private List<TitleMenu> getList()
{
List<TitleMenu> list = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
List<SubTitle> subTitles = new ArrayList<>();
if (i == 0)
{
for (int j = 0; j < subNames.length; j++)
{
SubTitle subTitle = new SubTitle(subNames[j]);
subTitles.add(subTitle);
}
}
if (i == 1)
{
for (int j = 0; j < subNames2.length; j++)
{
SubTitle subTitle = new SubTitle(subNames2[j]);
subTitles.add(subTitle);
}
}
TitleMenu model = new TitleMenu(names[i], subTitles, null);
list.add(model);
}
return list;
}
With this series of IF's, they can be separated into the 2 distitnos menus. Up to here all very well, but the problem occurs when I want to select one of them. In both cases I only take into account the SubName [] array.
What causes this? Ocaciona that if for example, I select "Alan" in the menu of iOS the action of "google" in the Menu of Android is executed. This is why it takes the "Child" according to its positioning and because in that class it is compared with the subName array and not SubName2 (which is the one Alan finds)
Capture where the subMenus are displayed
I leave the code of the method onChildClick:
public void onChildClick(int position)
{
String name = subNames[position];
drawerLayout.closeDrawers();
fragment.setTitle(name);
}
Knowing all this, how can I do to know which Menu I'm selecting?
That is, how to know if I select an option of iOS or Android . Or failing that I can know that subMenu I select specifically.
Knowing this, I can now place a series of IFs in class onChildClick
to access the corresponding arrangement. And that executes the action due for each subMenu.
Thank you very much, I hope you have explained me well.