我们可以在同一布局中有3个不同的ExpandableLists,即在main.xml中吗?如果是这样,我们可以在android:list?
方面使用它们我要求我需要在相同的布局中实现3个单独的expandableLists。可以吗?
答案 0 :(得分:0)
是的,你可以。我想简单地使用不同的id会比android:list更直接。但我想知道,您是否正在尝试实现一个可扩展的列表,其中包含3组行和1组子行?这些连接和看起来无缝?如果是这样,您可以使用一个可扩展列表。只是一个fyi。如果这是你的意图,我会给你一个示例代码,告诉你它是如何完成的。这很容易。
编辑:您收到该错误的原因是因为您最有可能尝试使用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,但它是绝对不是针对expandablelistviews或任何列表视图的最佳实践,它使用的资源比其他替代方案要多得多:
<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>
这是一张照片