导航抽屉活动android studio expandablelistview点击

时间:2018-11-01 23:43:54

标签: android android-studio navigation-drawer

我正在使用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;
}
 }

0 个答案:

没有答案
相关问题