【发布时间】:2011-04-27 14:20:54
【问题描述】:
因为我还没有足够的声望点来评论以前的问题,所以我不得不创建一个新问题。我需要一个不占用整个活动的可扩展列表视图,因此我使用此示例在没有 ExpandableListActivity 的情况下完成:
ExpandableList View don't expand
我稍作修改的代码:
public class Main extends Activity {
ExpandableListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list);
lv = (ExpandableListView) this.findViewById(R.id.expandableListView1);
MyExpandableListAdapter expandableAdapter = new MyExpandableListAdapter();
lv.setAdapter(expandableAdapter);
}
class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "People Names", "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(Main.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 hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
这很好用,但为了使其更健壮,我想将在代码中创建的 textViews 分离为 xml 文件(这是个好主意,对吗?)这是我遇到一些 FC 问题的地方。
group_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/row_name"
android:paddingLeft="50px"
android:textSize="20px"
android:textStyle="normal"
android:layout_width="320px"
android:layout_height="wrap_content"/>
</LinearLayout>
我在上面的 java 文件中更改了这个:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView parentRow = (TextView) findViewById(R.id.row_name);
parentRow.setText(getGroup(groupPosition).toString());
return parentRow;
}
我还尝试删除 LinearLayout 包装器,但这并没有解决任何问题。谁能告诉我为什么我的 xml 视图不起作用?谢谢。
编辑: 感谢 Flo,使用本教程的新代码:
group_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/group_name"
android:paddingLeft="50dp"
android:textSize="30sp"
android:textStyle="normal"
android:layout_height="wrap_content"/>
主要:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View parentView = convertView;
if (parentView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
parentView = vi.inflate(R.layout.group_row, null);
}
TextView parentText = (TextView) parentView.findViewById(R.id.group_name);
if (parentText != null) {
parentText.setText(getGroup(groupPosition).toString());
}
return parentText;
}
【问题讨论】:
标签: android xml expandablelistview expandablelistadapter