Android使用expandableListView显示列表

时间:2014-03-05 06:42:29

标签: android android-intent android-listview expandablelistview

我已按照此示例教程开发了一个示例应用程序http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

enter image description here

如果我点击核心我想在核心下的同一个屏幕中再加载一个列表,我怎么能这样做?使用我的下面代码我能够将子列表加载到另一个屏幕中,帮忙吗?

我的ChildClickListener代码在这里:

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub

            Log.d("onChildClick", "onChildClick");

            String position = (String) parentItems.get(groupPosition);
            Log.d("position", position);

            String child = listDataChild.get(position).get(childPosition);

            Log.d("child", child);

            if (child.equalsIgnoreCase("Core")) {

                ArrayList<String> parentItems = new ArrayList<String>();

                HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
                ArrayList<String> childItems = new ArrayList<String>();
                childItems.add("corejava");
                childItems.add("corejava");
                childItems.add("corejava");
                childItems.add("corejava");

                parentItems.add(position);
                listDataChild.put(parentItems.get(0), childItems);
                expandableList = (ExpandableListView) findViewById(R.id.lvExp);

                CoreAdapter adapter = new CoreAdapter(parentItems,
                        listDataChild);
                CustExpListview SecondLevelexplv = new CustExpListview(
                        MainActivity.this);
                adapter.setInflater(
                        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                        MainActivity.this);

                SecondLevelexplv.setAdapter(adapter);
                SecondLevelexplv.setGroupIndicator(null);

                expandableList.setAdapter(adapter);

            }

4 个答案:

答案 0 :(得分:3)

我认为你需要使用嵌套的Expandable listview。所以这个链接可能会为您提供一些帮助。所以使用这个。

http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=c991d70a-4570-4e4b-8378-2191a4247a84

答案 1 :(得分:2)

我为你编写了一个treeView适配器,你可以使用它并根据需要设置多个级别,因为你需要复制以下代码:

MainActivity:

public class MainActivity extends Activity {

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

        // add data for test on one list<AccountHierarchical>


        AccountHierarchical obj = new AccountHierarchical();
        obj.setLevelId(1);
        obj.setTitle("level 1");


        AccountHierarchical obj2 = new AccountHierarchical();
        obj2.setLevelId(1);
        obj2.setTitle("level 2");


        AccountHierarchical obj3 = new AccountHierarchical();
        obj3.setLevelId(1);
        obj3.setTitle("level 3");


        List<AccountHierarchical> emptyList = new ArrayList<AccountHierarchical>();
        obj3.setList(emptyList);

        List<AccountHierarchical> list2 = new ArrayList<AccountHierarchical>();
        list2.add(obj3);list2.add(obj3);list2.add(obj3);list2.add(obj3);list2.add(obj3);
        obj2.setList(list2);

        List<AccountHierarchical> list = new ArrayList<AccountHierarchical>();
        list.add(obj2);list.add(obj2);list.add(obj2);list.add(obj2);list.add(obj2);
        obj.setList(list);


        List<AccountHierarchical> result = new ArrayList<AccountHierarchical>();
        result.add(obj);result.add(obj);result.add(obj);result.add(obj);



        // create Adapter
        TreeView adapter = new TreeView(result, this,
                getLayoutInflater() , false, 0);

        ExpandableListView expandList = (ExpandableListView)findViewById(R.id.expandableList_tree);
        expandList.setGroupIndicator(null);
        expandList.setAdapter(adapter);
    }

CustExpListview:

public class CustExpListview extends ExpandableListView {

    int intGroupPosition, intChildPosition, intGroupid;

    public CustExpListview(Context context) {
           super(context);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//           widthMeasureSpec = MeasureSpec.makeMeasureSpec(400,
//                        MeasureSpec.AT_MOST);
           heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
                        MeasureSpec.AT_MOST);
           super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


}

AccountHierarchical:

public class AccountHierarchical {

    List<AccountHierarchical> list;
    private String Title;
    private int Id;


    public int getId() {
        return Id;
    }

    public void setLevelId(int Id) {
        this.Id = Id;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public List<AccountHierarchical> getList() {
        return list;
    }

    public void setList(List<AccountHierarchical> list) {
        this.list = list;
    }

}

的TreeView:

public class TreeView extends BaseExpandableListAdapter implements
        OnClickListener {

    List<AccountHierarchical> list;
    LayoutInflater inflatter;
    static Context context;
    boolean checkGroup;
    int position;

    static ProgressDialog ProgressDialog;

    public TreeView( List<AccountHierarchical> list, Context context,
            LayoutInflater inflatter, boolean checkGroup, int position
         ) {
        this.list = list;
        TreeView.context = context;
        this.inflatter = inflatter;
        this.checkGroup = checkGroup; // this is true when you call from inner.
        this.position = position;



    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return list.get(groupPosition).getList().get(childPosition).getId();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (checkGroup)
            groupPosition = position;




        List<AccountHierarchical> childtemp = list.get(groupPosition)
                .getList();

        // call this adapter again for creating another level
        if (childtemp.get(childPosition).getList().size() > 0) {
            CustExpListview SecondLevelexplv = new CustExpListview(context);
            TreeView adapter  = null;

            adapter = new TreeView(childtemp, context,
                        inflatter, true, childPosition);
            SecondLevelexplv.setGroupIndicator(null);
            SecondLevelexplv.setAdapter(adapter);
            return SecondLevelexplv;
        }
        // call one layout, this is last child
        else {

            convertView = inflatter.inflate(R.layout.grouprow, null);
            TextView tv = (TextView) convertView.findViewById(R.id.grouprow);



            tv.setText(list.get(groupPosition).getList().get(childPosition)
                    .getTitle());
            tv.setPadding(0, 0, 20, 0);
            convertView.setTag(list.get(groupPosition).getList()
                    .get(childPosition) );
            convertView.setId(position);
            convertView.setOnClickListener(this);
            return convertView;
        }

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public int getGroupCount() {

        if (checkGroup)
            return 1;

        return list.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return list.get(groupPosition).getId();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = inflatter.inflate(R.layout.grouprow, null);

        TextView tv = (TextView) convertView.findViewById(R.id.grouprow);

        if (checkGroup)
            tv.setText(list.get(position).getTitle());
        else
            tv.setText(list.get(groupPosition).getTitle());

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return false;
    }

    @Override
    public void onClick(View v) {
        // your onClick method
    }

}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <ExpandableListView
        android:id="@+id/expandableList_tree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ExpandableListView>

</RelativeLayout>

grouprow.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/grouprow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp" 
        android:text=""/>



</RelativeLayout>

管理单元:

enter image description here

答案 2 :(得分:1)

您必须在xml文件中创建一个布局(从您的核心textview来源)并在下面显示一个图像视图 点击那个imageView,你只需要让你的布局过去和可见 我刚给你指导休息取决于你的技能:)

    <RelativeLayout
        android:id="@+id/option"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:orientation="horizontal" >

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/navigationIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/nn" />


        <ImageView
            android:id="@+id/callOption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/phonee" />

        <ImageView
            android:id="@+id/imageViewfeedback"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/editer" />
    </RelativeLayout>

在Jave文件中

linear.setVisibility(View.GONE);
        option.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    if(count==0)
                    {
                        linear.setVisibility(View.VISIBLE);

                        count=1;
                    }
                    else
                    {


                        linear.setVisibility(View.GONE);
                        count=0;
                    }

            }
        });

答案 3 :(得分:1)

您需要切换到包含子项内子项的Three-Level of Expandable ListView

另请查看ExpandableListView with 3 Childs