【问题标题】:Adding ExpandableListView in a Layout在布局中添加 ExpandableListView
【发布时间】:2012-11-06 09:35:54
【问题描述】:

我在互联网上找到了这个代码,ExpandableListView 的一个例子。它运行完美。

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demonstrates expandable lists backed by a Simple Map-based adapter
 */
public class SmplExpandableTest extends ExpandableListActivity {
    private static final String PARENT_KEY = "pKey";
    private static final String CHILD_KEY = "cKey";

    private ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put(PARENT_KEY, "Hello");
        curGroupMap.put(CHILD_KEY, "First Order System Response");

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();

        Map<String, String> curChildMap = new HashMap<String, String>();
        children.add(curChildMap);
        curChildMap.put(PARENT_KEY, "World");
        curChildMap.put(CHILD_KEY, "Second Order System");

        childData.add(children);

        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(this, groupData,
                android.R.layout.simple_expandable_list_item_1, new String[] {
                        PARENT_KEY, CHILD_KEY }, new int[] {
                        android.R.id.text1, android.R.id.text2 }, childData,
                android.R.layout.simple_expandable_list_item_2, new String[] {
                        PARENT_KEY, CHILD_KEY }, new int[] {
                        android.R.id.text1, android.R.id.text2 });
        setListAdapter(mAdapter);
    }
}

现在我想在布局中插入这个ExpandableListView,这样我就可以在底部添加一个按钮。我该怎么做?

谢谢

【问题讨论】:

    标签: android layout expandablelistview expandablelistadapter


    【解决方案1】:

    要将Button 添加到屏幕底部的ExpandableListView 下,您必须做两件事:

    1. 活动必须从Activity 扩展,而不是ExpandableListActivity。而且您必须设置自定义布局。
    2. 创建包含ExpandableListView 和“按钮”的自定义布局。

    活动类似于:

    public class SmplExpandableTest extends Activity {
        private static final String PARENT_KEY = "pKey";
        private static final String CHILD_KEY = "cKey";
    
        private ExpandableListAdapter mAdapter;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_layout);
            ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv);
    
            List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
            List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(PARENT_KEY, "Hello");
            curGroupMap.put(CHILD_KEY, "First Order System Response");
    
            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
    
            Map<String, String> curChildMap = new HashMap<String, String>();
            children.add(curChildMap);
            curChildMap.put(PARENT_KEY, "World");
            curChildMap.put(CHILD_KEY, "Second Order System");
    
            childData.add(children);
    
            // Set up our adapter
            mAdapter = new SimpleExpandableListAdapter(this, groupData,
                    android.R.layout.simple_expandable_list_item_1, new String[] {
                            PARENT_KEY, CHILD_KEY }, new int[] {
                            android.R.id.text1, android.R.id.text2 }, childData,
                    android.R.layout.simple_expandable_list_item_2, new String[] {
                            PARENT_KEY, CHILD_KEY }, new int[] {
                            android.R.id.text1, android.R.id.text2 });
    
            elv.setAdapter(mAdapter);
        }
    }
    

    布局res/layout/my_layout.xml 类似于:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
            <ExpandableListView 
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/elv" >
            </ExpandableListView>
            <Button 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/btn"
                android:text="@string/btn_text" >
    </LinearLayout>
    

    【讨论】:

    • 很多。你能告诉我当你在 SmplExpandableTest 类中添加这些行时发生了什么吗?
    • 你应该查找它的文档。 setContentView 设置布局(ExpandableListActivity 在内部使用一些预定义的布局)。然后你必须将适配器设置为ExpandableListView(不能使用ExpandableListActivitysetListAdapter实用功能)。
    【解决方案2】:

    你需要在xml中使用Expandable listview并绑定为BaseExtendedAdapter

    这里是example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 2019-04-30
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多