Я реализую представление navigation drawer menu
с expandablelistview
.
Я хотел бы знать, чувствую ли я себя хорошо в своем коде, то, что я ищу, - это возможность щелкнуть опции, которые вы создаете в списке, и перейти к activities
, как если бы это было обычное меню.
Спасибо.
Это мой код.
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ExpandableListView expListView;
private ActionBarDrawerToggle mDrawerToggle;
ExpandableListAdapter listAdapterExpandable;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
// prepara datos para Header y Listado en ExpandableListView.
prepareListData();
// configura Adapter.
listAdapterExpandable = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// configura Adapter en ExpandableListView.
expListView.setAdapter(listAdapterExpandable);
// Puedes expandir los grupos por default.
int count = listAdapterExpandable.getGroupCount();
for ( int i = 0; i < count; i++ )
expListView.expandGroup(i);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
В этих параметрах массивов я хочу нажать на другие действия
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Agrega Encabezados.
listDataHeader.add("Lenguajes de Programación");
listDataHeader.add("hola");
// Agrega datos.
List<String> lenguajes = new ArrayList<String>();
lenguajes.add("Profile");
lenguajes.add("Inbox");
lenguajes.add("Mis aliados");
lenguajes.add("favorites");
lenguajes.add("My preferences and privacy");
lenguajes.add("Mis ordenes");
lenguajes.add("log out");
List<String> hola = new ArrayList<String>();
listDataChild.put(listDataHeader.get(0), lenguajes);
listDataChild.put(listDataHeader.get(1), hola);
}
}
ExpandableListAdapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> miListDataHeader; // Titulos en encabezados.
private HashMap<String, List<String>> miListDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this.context = context;
this.miListDataHeader = listDataHeader;
this.miListDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.miListDataChild.get(this.miListDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.miListDataChild.get(this.miListDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this.miListDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.miListDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}