Identification Submenu (Group and child) selected in a Navigation Drawer on Android

1

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.

    
asked by Alan Oliver 09.01.2018 в 22:40
source

1 answer

2

Actually, the example you indicate does not consider it correct for the construction of the menu you want to make, I think the option should be to use a ExpandableListView within Navigation Drawer .

If you want to use the same example, then I can tell you that your class MainActivity implements RecyclerAdapter.ItemClickChild , therefore using the onChildClick() method you can only get the index of the child element but not the group that contains it .

 @Override
    public void onChildClick(int position) {
        String name = subNames[position];
        Toast.makeText(getApplicationContext(), "Nombre: " + name + " ,position: " + position, Toast.LENGTH_SHORT).show();
        drawerLayout.closeDrawers();
        fragment.setTitle(name);
    }

To obtain the group you must modify the interface so that the group is also sent, this would be done within the class RecyclerAdapter , make the following modifications in the methods:

    @Override
    public void onBindChildViewHolder(final SubTitleViewHolder holder, final int flatPosition,
                                      final ExpandableGroup group, final int childIndex) {

        final SubTitle subTitle = ((TitleMenu) group).getItems().get(childIndex);
        holder.setSubTitletName(subTitle.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //*Determina el grupo.
                int grupo;
                if(group.getTitle().equals(Constant.name[0])){
                    grupo = 0;
                }else{
                    grupo = 1;
                }
                mListener.onChildClick(grupo, childIndex); //* Agrega grupo.
            }
        });
    }

  public interface ItemClickChild{
        //* Agrega grupo.
        void onChildClick(int group, int position); 
    }

and in this way obtain both the index of the group and the index of the child element of the group:

@Override
public void onChildClick(int group, int position) {
    String name = subNames[position];
    Toast.makeText(getApplicationContext(), "Nombre: " + name + " , Grupo " + group + " ,position: " + position, Toast.LENGTH_SHORT).show();
    drawerLayout.closeDrawers();
    fragment.setTitle(name);
}

I suggest you make the implementation of Navigation Drawer with ExpandableListView to easily get the index of the group and the child element, this is a example .

In this way you can obtain without problems the values within the method onChildClick() of ExpandableListView .

   expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            Toast.makeText(getApplicationContext(), "Group:  " + groupPosition + "Child Element: " + childPosition, Toast.LENGTH_SHORT).show();

            return false;
        }
    });

    
answered by 10.01.2018 / 00:06
source