Android 我们是否可以在同一布局(即main.xml)中有3个不同的可扩展列表?

Android 我们是否可以在同一布局(即main.xml)中有3个不同的可扩展列表?,android,android-layout,expandablelistview,Android,Android Layout,Expandablelistview,我们是否可以在同一布局(即main.xml)中有3个不同的可扩展列表?如果是这样的话,我们可以在android:list方面使用它们吗 我有一个要求,我需要实现3个单独的可扩展列表在同一布局。可以吗?是的,你可以。我想简单地使用不同的ID比android:list更直接。但我想知道,您是否在尝试实现一个可扩展列表,每个列表下有3组行和1组子行?这样它们就可以连接起来,看起来天衣无缝了?如果是这样,你可以用一个可扩展的列表来完成。仅供参考。如果这是你的意图,我会给你一个示例代码来演示它是如何完成的

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


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

是的,你可以。我想简单地使用不同的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”作为布局高度,但对于ExpandableListView或任何listview来说,这样做肯定不是最佳做法,它比其他替代方法使用了更多的资源):


这是一张照片

  • 在这里,我不想指导您如何编写代码,只是为了与您保持一致。在一个活动中有这么多可扩展的ListView,特别是如果您正在膨胀视图,这将不是最重要的。。。优雅的实现。我不知道你想做什么,但如果它们像我的一样连接起来,甚至只是在一个垂直的面板上,那么只需一个面板就可以完成,而且效率更高

事实上,我希望它们作为3个单独的列表。当我共同实现xmls并重用代码,在同一布局中的不同实例中使用不同的列表调用它们时,只给出了第三个带有值的expandableList。前两个可扩展列表根本没有加载。不要使用android:list来识别它们。如果在你的活动中只有一个列表视图可以发挥很大作用,那么它是很有用的。你需要3个不同的ID。它们可以是任何东西。顺便说一句,如果你想得到关于代码的更准确的答案,那么发布代码是有帮助的,但是它太长了。我将张贴代码,在同一时间,我已经尝试了上述使用不同的ID。但是我得到了一个RuntimeException:无法启动activity ComponentInfo:您的内容必须有一个id属性为“android.R.id.List”的ExpandableListView。有什么建议吗?忘了提到叶节点应该有复选框。我会编辑这个问题,非常感谢你。但是在以上述方式更改代码之后,我遇到了一个问题。我确信这是一个配置问题。当我尝试运行时,logcat显示此消息:“打开跟踪文件时出错:没有这样的文件或目录(2)”,未显示其他异常。我尝试删除该项目并创建一个新项目。但同样的问题。有什么想法吗?这不是一个好的UI/UX设计。你必须重新考虑你的设计。您的应用程序在ldpi设备上的表现如何?重新考虑你的设计。谢谢你,拉古。对设计有什么建议吗?我们可以一起去俱乐部吗?但是实现了多级树结构的可扩展列表。叶子节点也应该有复选框。如果允许的话,你能发布你的截图吗?
<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>