如何在xml中使用android ExpandableListView?
我试图在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在xml文件中将id(即默认id)更改为
活动中的自定义id而不是public class MLT_File extends ExpandableListActivity
通过使用 extends Activity 编写 public class MLT_File extends Activity 我们可以编写自己的(自定义)可扩展列表,然后
为组和子项使用不同的布局
In the xml file change the id i.e default id to customized id
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
use different layouts for group and child
尝试此代码 -
Main.xml -
ChildLayout.xml -
Grouplayout.xml -
这些是 CustomExpandable ListView 的布局。
您可以使用此处中的整个代码进行尝试。
Try this code -
Main.xml -
ChildLayout.xml -
Grouplayout.xml -
These are the layouts for CustomExpandable ListView.
You can try this with this whole code from here.