0

我们可以在同一布局中(即在 main.xml 中)有 3 个不同的 ExpandableLists 吗?如果是这样,我们可以在 android:list 方面以不同的方式使用它们吗?

我有一个要求,我需要在同一布局中实现 3 个单独的可扩展列表。可以做到吗?

4

1 回答 1

0

是的你可以。我想简单地使用不同的 id 会比 android:list 更直接。但我想知道,您是否试图实现一个可扩展的列表,其中每个行下有 3 个组行和 1 个子行组?这样它们是连接的并且看起来无缝的?如果是这样,您可以使用一个可扩展的列表来完成。仅供参考。如果这是您的意图,我会给您一个示例代码,向您展示它是如何完成的。这很容易。

编辑:您收到该错误的原因是因为您很可能正在尝试使用 expandablelistactivity 类,该类假设(实际上)活动中只有一个可扩展列表。您应该使用常规活动课程。这是我刚刚测试和工作的内容。

package com.mango.stackoverflow;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExpandableListView TestExpandListView1 = (ExpandableListView) findViewById(R.id.expandableListView1);
        ExpandableListView TestExpandListView2 = (ExpandableListView) findViewById(R.id.expandableListView2);
        ExpandableListView TestExpandListView3 = (ExpandableListView) findViewById(R.id.expandableListView3);
        TestExpandableListAdapter TestExpandAdapter = new TestExpandableListAdapter();
        TestExpandListView1.setAdapter(TestExpandAdapter);
        TestExpandListView2.setAdapter(TestExpandAdapter);
        TestExpandListView3.setAdapter(TestExpandAdapter);
    }


    public class TestExpandableListAdapter extends BaseExpandableListAdapter {

        private String[] groups = { "People Names -- 1st GROUP", "Dog Names", "Cat Names", "Fish Names" };
        private String[][] children = {
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" },
                { "Goldy", "Bubbles" }
        };

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(MainActivity.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public boolean hasStableIds() {
            return true;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

我也发布了 xml,以防我不清楚 id 实例化(请注意——尽管为了速度我在 xml 中使用了“wrap_content”作为 layout_height,但这绝对不是最好的为可扩展列表视图或任何列表视图这样做的做法,它使用的资源比其他替代方案要多得多):

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

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

        <ExpandableListView
        android:id="@+id/expandableListView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/expandableListView1" >
    </ExpandableListView>

                <ExpandableListView
        android:id="@+id/expandableListView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/expandableListView2" >
    </ExpandableListView>


</RelativeLayout>

这是一张图片

模拟器结果

  • 发布后,我不想指导您如何编写代码,而只是为了与您保持一致。在一个活动中拥有这么多可扩展的列表视图,特别是如果您正在膨胀视图,这不会是最......优雅的实现。我不知道您要做什么,但如果它们要像我的那样连接,甚至只是在一个垂直面板中,那么只需一个就可以完成并且效率更高。
于 2012-10-08T05:55:01.347 回答