简体   繁体   中英

ListView to ExpandableListView in NavigationDrawer

I want to add ExpandableListView to the NavigationDrawer . It is just in ListView currently. What shall I do to make it in ExpandableListView ?

I have included below the main activity file and xml file.

MainActivity.java

package com.example.ayush.yaido;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem; 
import android.widget.ListView;
import java.util.ArrayList;


public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

private ActionBarDrawerToggle actionBarDrawerToggle;
private Toolbar toolbar;
private ActionBar actionBar;
private DrawerLayout drawerLayout;
private ListView navList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar)findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navList = (ListView) findViewById(R.id.navlist);
    ArrayList<String> navArray = new ArrayList<String>();
    navArray.add("Home");
    navArray.add("Fragment1");
    navArray.add("Fragment2");
    navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, navArray);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(this);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.opendrawer, R.string.closedrawer);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
    loadSelection(0);
}

private void loadSelection(int i) {
    navList.setItemChecked(i, true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarDrawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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;
    } else if (id == android.R.id.home) {
        if (drawerLayout.isDrawerOpen(navList)) {
            drawerLayout.closeDrawer(navList);
        } else {
            drawerLayout.openDrawer(navList);
        }
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    return super.onContextItemSelected(item);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    switch (position) {
        case 0:

            break;
        case 1:

            break;

        case 2:

            break;
    }
    drawerLayout.closeDrawer(navList);
}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar"/>
<android.support.v4.widget.DrawerLayout

android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/fragmentholder"
android:layout_width="match_parent"
android:layout_height="match_parent">


</FrameLayout>

<ListView
android:id="@+id/navlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#dedede"
android:choiceMode="singleChoice"
android:divider="@android:color/black"
android:dividerHeight="1dp"
android:indicatorRight="440dp"></ListView>


</android.support.v4.widget.DrawerLayout>
</LinearLayout>

app_bar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
app:theme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v7.widget.Toolbar>

Thanks.

Hope you are using latest design support library. If you do so you don't need to attach a list view to navigation drawer rather you can use the below xml inside DrawerLayout

<android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/drawer_header" app:menu="@menu/drawer"/>

You can define drawer header with headerLayout and menu with app:menu. you can refer below link on the sub headings, in your case expandable menu.

http://android-developers.blogspot.com/2015/05/android-design-support-library.html

ExpandableListViews are a lot more work to implement than a normal listview -- Here's an idea of what you are lookig at:

  1. Change the ListView type to an ExpandableListView

  2. You need to change the adapter to a BaseExpandableListAdapter. This is the hard part, there's lots of methods like getChildView, getChilViewcount, etc, that you are going to have to implement.

  3. You need to change how you store data. You have your data as an array, but if the ExpandableListView asks you for Group 5, Child 3, what element of the array are you going to feed it? Consider implementing an Array of Lists, Nodes, or something similar to hold your data.

That's a general overview of what you need to do. Here's a link to a guide to get you started:

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM