如何在另一个列表视图中显示其各自组项的子项

时间:2015-03-02 06:11:55

标签: android android-listview expandablelistview

我想将可扩展列表视图中可用的各个组项的子项显示为单独的列表视图。

我做了以下事情,

package com.example.demo_data1;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

@SuppressLint("NewApi")
public class DrawerLayoutTest extends Activity implements OnChildClickListener {

private DrawerLayout drawer;
private ExpandableListView drawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer_layout_test);

    setGroupData();
    setChildGroupData();

    initDrawer();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.drawer_layout_test, menu);
    return true;
}

private void initDrawer() {
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    drawerList = (ExpandableListView) findViewById(R.id.left_drawer);

    drawerList.setAdapter(new NewAdapter(this, groupItem, childItem));

    drawerList.setOnChildClickListener(this);

    // actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer,
    // R.drawable.ic_drawer, R.string.open_drawer,
    // R.string.close_drawer) {
    // public void onDrawerClosed(View view) {
    // getActionBar().setSubtitle("open");
    // }
    //
    // /** Called when a drawer has settled in a completely open state. */
    // public void onDrawerOpened(View drawerView) {
    // getActionBar().setSubtitle("close");
    // }
    //
    // };
    //
    // drawer.setDrawerListener(actionBarDrawerToggle);

}

public void setGroupData() {
    groupItem.add("TECHNOLOGY");
    groupItem.add("MOBILE");
    groupItem.add("MANUFACTURER");
    groupItem.add("EXTRAS");
}

ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();

public void setChildGroupData() {
    /**
     * Add Data For TecthNology
     */
    ArrayList<String> child = new ArrayList<String>();
    child.add("Java");
    child.add("Drupal");
    child.add(".Net Framework");
    child.add("PHP");
    childItem.add(child);

    /**
     * Add Data For Mobile
     */
    child = new ArrayList<String>();
    child.add("Android");
    child.add("Window Mobile");
    child.add("iPHone");
    child.add("Blackberry");
    childItem.add(child);
    /**
     * Add Data For Manufacture
     */
    child = new ArrayList<String>();
    child.add("HTC");
    child.add("Apple");
    child.add("Samsung");
    child.add("Nokia");
    childItem.add(child);
    /**
     * Add Data For Extras
     */
    child = new ArrayList<String>();
    child.add("Contact Us");
    child.add("About Us");
    child.add("Location");
    child.add("Root Cause");
    childItem.add(child);
}


public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    switch (groupPosition)
    {
    case 0:
            switch (childPosition)
            {
            case 0:
               MainActivity();
               break;
            case 1:
                //
            }

    }
    return false;
}

private void MainActivity(){
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
}

现在,在上面的代码中,子项目显示在同一列表视图中的组项目下方,而不是我想在组项目列表视图旁边的不同列表视图中显示子项目。

请帮我解决这个问题,长期坚持下去。

对此有任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

使用此代码

<强> activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical"
tools:context="com.example.listtest.MainActivity" >




<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <!-- Drawer Content -->

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


    </FrameLayout>

    <!-- The navigation menu -->

    <ListView 
        android:id="@+id/left_drawer_child"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
                android:layout_gravity="start"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout>

<强> MainActivity.java

public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
Fragment fragment = null;
ListView mainlist;
ArrayList<String> formain = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainlist = (ListView) findViewById(R.id.left_drawer_child);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
    mDrawerLayout.setDrawerListener(mDrawerListener);
//  childlist = (ListView) findViewById(R.id.listView2);
    formain.add("TECHNOLOGY");
    formain.add("MOBILE");
    formain.add("MANUFACTURER");
    formain.add("EXTRAS");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,formain);
    mainlist.setAdapter(adapter);
    mDrawerLayout.openDrawer(mainlist);
    mainlist.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            String getGroup = formain.get(arg2);
            if (getGroup == "TECHNOLOGY"){
                fragment = new TechnologyFragment(); 
                    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
                mDrawerLayout.closeDrawer(mainlist);
            }
            if (getGroup == "MOBILE"){
             //fragment for mobile
            }
            if (getGroup == "MANUFACTURER"){
                //fragment for manufacturer
            }
            if (getGroup == "EXTRAS"){
            //fragment for extras
            }


        }
    });

}
private DrawerListener mDrawerListener = new DrawerListener() {

    @Override
    public void onDrawerStateChanged(int status) {

    }

    @Override
    public void onDrawerSlide(View view, float slideArg) {

    }

    @Override
    public void onDrawerOpened(View view) {
    }

    @Override
    public void onDrawerClosed(View view) {
    }
};
}

<强> technology_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

<强> TechnologyFragment.java

public class TechnologyFragment extends Fragment {

ListView techlist;
ArrayList<String> forchild = new ArrayList<String>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.technology_fragment, null);
    techlist = (ListView) rootView.findViewById(R.id.listView1);
    forchild.add("Java");
    forchild.add("Drupal");
    forchild.add(".Net Framework");
        forchild.add("PHP");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,forchild);
        techlist.setAdapter(adapter);

    return rootView;
}

}

答案 1 :(得分:0)

不使用ExpandableListView而是使用Normal ListView和片段。

在片段1中显示父列表并单击父1中的项目将显示片段2,其中包含列表中的子项目。