Android 我们是否可以在同一布局(即main.xml)中有3个不同的可扩展列表?
我们是否可以在同一布局(即main.xml)中有3个不同的可扩展列表?如果是这样的话,我们可以在android:list方面使用它们吗Android 我们是否可以在同一布局(即main.xml)中有3个不同的可扩展列表?,android,android-layout,expandablelistview,Android,Android Layout,Expandablelistview,我们是否可以在同一布局(即main.xml)中有3个不同的可扩展列表?如果是这样的话,我们可以在android:list方面使用它们吗 我有一个要求,我需要实现3个单独的可扩展列表在同一布局。可以吗?是的,你可以。我想简单地使用不同的ID比android:list更直接。但我想知道,您是否在尝试实现一个可扩展列表,每个列表下有3组行和1组子行?这样它们就可以连接起来,看起来天衣无缝了?如果是这样,你可以用一个可扩展的列表来完成。仅供参考。如果这是你的意图,我会给你一个示例代码来演示它是如何完成的
我有一个要求,我需要实现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,特别是如果您正在膨胀视图,这将不是最重要的。。。优雅的实现。我不知道你想做什么,但如果它们像我的一样连接起来,甚至只是在一个垂直的面板上,那么只需一个面板就可以完成,而且效率更高
<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>