如何在xml中使用android ExpandableListView?

发布于 2024-12-27 22:25:52 字数 4917 浏览 2 评论 0原文

我试图在 xml 中显示一个在其前面带有按钮的可扩展列表视图。但不知何故,我无法让它在代码中工作。

我的xml(我使用RelativeLayout作为父布局):

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_titleFrame"
    android:background="#FF3300"
    android:padding="5dp"
    android:id="@+id/mlt_file_frameL"
    >

    <Button
        android:id="@+id/mlt_file_btn_edit"
        android:text="Edit"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="left"
        />

</FrameLayout>

<ExpandableListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_frameL"
    >

</ExpandableListView>

我的代码:

public class MLT_File extends ExpandableListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mlt_file_intent);
    // Set up our adapter
    ExpandableListAdapter listAdapter = new customListAdapter();

    ExpandableListView expLV = (ExpandableListView) findViewById(R.id.mlt_expList);//getExpandableListView();
    expLV.setAdapter(listAdapter);

    //registerForContextMenu(expLV);

}//onCreate

/**
 * A simple adapter which maintains an ArrayList of photo resource Ids. 
 * Each photo is displayed as an image. This adapter supports clearing the
 * list of photos and adding a new photo.
 *
 */
public class customListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private File mapFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Map");
    private File recordFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Record");
    private File scribbleFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Scribble");
    private String[] groups = { "Map", "Record", "Scribble" };
    private String[][] children = { mapFolder.list(), recordFolder.list(), scribbleFolder.list() };

    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 View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                                View convertView, ViewGroup parent) {

        TextView textView = new TextView(MLT_File.this);
        textView.setBackgroundColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(20);
        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }//getChildView

    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 = new TextView(MLT_File.this);
        textView.setBackgroundColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(25);
        textView.setText(getGroup(groupPosition).toString());

        /*Button btn = new Button(MLT_File.this);
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        btn.setText("Edit");
        btn.setId(100+groupPosition);
        btn.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

        TableLayout layout = new TableLayout(MLT_File.this);
        layout.setBackgroundColor(Color.WHITE);
        layout.setColumnStretchable(1, true);
        layout.addView(textView);
        //layout.addView(btn);*/

        return textView;
    }

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

    public boolean hasStableIds() {
        return true;
    }

}//customListAdapter
}//class

我的 logcat:

java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'

我尝试将 id 更改为 id="android:id/list",但我不知道如何使用该 id 创建 ExpandableListView 对象,因为它给出了一个错误,指出其为空。

*impt:我想创建一个 ExpandableListView 对象,以便我可以投射 onChildClickListener。

i'm trying to show an expandableListView with a button before it in xml. but somehow, i could not get it work in the code.

my xml (i uses RelativeLayout as parent layout) :

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_titleFrame"
    android:background="#FF3300"
    android:padding="5dp"
    android:id="@+id/mlt_file_frameL"
    >

    <Button
        android:id="@+id/mlt_file_btn_edit"
        android:text="Edit"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="left"
        />

</FrameLayout>

<ExpandableListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_frameL"
    >

</ExpandableListView>

my code :

public class MLT_File extends ExpandableListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mlt_file_intent);
    // Set up our adapter
    ExpandableListAdapter listAdapter = new customListAdapter();

    ExpandableListView expLV = (ExpandableListView) findViewById(R.id.mlt_expList);//getExpandableListView();
    expLV.setAdapter(listAdapter);

    //registerForContextMenu(expLV);

}//onCreate

/**
 * A simple adapter which maintains an ArrayList of photo resource Ids. 
 * Each photo is displayed as an image. This adapter supports clearing the
 * list of photos and adding a new photo.
 *
 */
public class customListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private File mapFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Map");
    private File recordFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Record");
    private File scribbleFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Scribble");
    private String[] groups = { "Map", "Record", "Scribble" };
    private String[][] children = { mapFolder.list(), recordFolder.list(), scribbleFolder.list() };

    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 View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                                View convertView, ViewGroup parent) {

        TextView textView = new TextView(MLT_File.this);
        textView.setBackgroundColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(20);
        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }//getChildView

    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 = new TextView(MLT_File.this);
        textView.setBackgroundColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(25);
        textView.setText(getGroup(groupPosition).toString());

        /*Button btn = new Button(MLT_File.this);
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        btn.setText("Edit");
        btn.setId(100+groupPosition);
        btn.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

        TableLayout layout = new TableLayout(MLT_File.this);
        layout.setBackgroundColor(Color.WHITE);
        layout.setColumnStretchable(1, true);
        layout.addView(textView);
        //layout.addView(btn);*/

        return textView;
    }

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

    public boolean hasStableIds() {
        return true;
    }

}//customListAdapter
}//class

my logcat :

java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'

i tried to change the id to id="android:id/list", but i dont know how to create the ExpandableListView object with that id, as it gives an error saying its empty.

*impt : i wanted to create an ExpandableListView object is so that i can cast the onChildClickListener.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看透却不说透 2025-01-03 22:25:52

在xml文件中将id(即默认id)更改为

<ExpandableListView 
    android:id="@+id/expandablelist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_frameL"
    >

</ExpandableListView>

活动中的自定义id而不是public class MLT_File extends ExpandableListActivity
通过使用 extends Activity 编写 public class MLT_File extends Activity 我们可以编写自己的(自定义)可扩展列表,然后

expLV= new ExpandableAdapter(Expandablelist.this);
setListAdapter(expLV);

为组和子项使用不同的布局

In the xml file change the id i.e default id to customized id

<ExpandableListView 
    android:id="@+id/expandablelist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_frameL"
    >

</ExpandableListView>

In the Activity instead of public class MLT_File extends ExpandableListActivity
write public class MLT_File extends Activity by using extends Activity we can write our own (customized) expandable listthen use

expLV= new ExpandableAdapter(Expandablelist.this);
setListAdapter(expLV);

use different layouts for group and child

萌面超妹 2025-01-03 22:25:52

尝试此代码 -

Main.xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/listView"
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none"></ExpandableListView>
</LinearLayout>

ChildLayout.xml -

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:layout_width="fill_parent"
    android:layout_height="45dip" android:paddingLeft="5dip"
    android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip"
    android:gravity="center_vertical" android:id="@+id/tvChild"
    android:text="Children" android:textColor="#ffCCCC22"></TextView>
</LinearLayout>

Grouplayout.xml -

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/title">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:id="@+id/tvGroup" android:layout_width="fill_parent"
    android:layout_height="45dip" android:text="Groups" android:gravity="center_vertical|right"
    android:paddingLeft="5dip" android:paddingRight="5dip"
    android:textColor="#ffffffff" android:textStyle="bold"
    android:textSize="17dip"></TextView>
</LinearLayout>

这些是 CustomExpandable ListView 的布局。

您可以使用此处中的整个代码进行尝试。

Try this code -

Main.xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/listView"
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none"></ExpandableListView>
</LinearLayout>

ChildLayout.xml -

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:layout_width="fill_parent"
    android:layout_height="45dip" android:paddingLeft="5dip"
    android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip"
    android:gravity="center_vertical" android:id="@+id/tvChild"
    android:text="Children" android:textColor="#ffCCCC22"></TextView>
</LinearLayout>

Grouplayout.xml -

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/title">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:id="@+id/tvGroup" android:layout_width="fill_parent"
    android:layout_height="45dip" android:text="Groups" android:gravity="center_vertical|right"
    android:paddingLeft="5dip" android:paddingRight="5dip"
    android:textColor="#ffffffff" android:textStyle="bold"
    android:textSize="17dip"></TextView>
</LinearLayout>

These are the layouts for CustomExpandable ListView.

You can try this with this whole code from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文