ListView to ExpandableListView in NavigationDrawer

时间:2015-07-08 15:51:47

标签: java android listview layout expandablelistview

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.

2 个答案:

答案 0 :(得分:0)

希望您使用最新的设计支持库。如果你这样做,你不需要将列表视图附加到导航抽屉而不是你可以在DrawerLayout中使用下面的xml

<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"/>

您可以使用headerLayout定义抽屉标题,使用app:menu定义菜单。您可以参考下面的子标题链接,在您的案例中可扩展菜单。

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

答案 1 :(得分:0)

ExpandableListViews比普通列表视图要实现的工作要多得多 - 这里有一个关于你的看法的想法:

  1. 将ListView类型更改为ExpandableListView

  2. 您需要将适配器更改为BaseExpandableListAdapter。这是困难的部分,有很多方法,比如getChildView,getChilViewcount等,你将不得不实现。

  3. 您需要更改存储数据的方式。您将数据作为数组,但如果ExpandableListView要求您为组5,子3,您将为其提供哪个元素元素?考虑实施一个列表,节点或类似的数据来保存您的数据。

  4. 这是您需要做的一般概述。这是一个指南的链接,可以帮助您入门:

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