ExpandableList视图未使用ExpandableListActivity
|
因为我没有足够的声誉积分来评论以前的问题,所以我不得不创建一个新的问题。我需要一个不会占用全部活动的可扩展列表视图,因此我使用此示例在没有ExpandableListActivity的情况下进行了操作:
ExpandableList视图不展开
我稍作修改的代码:
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;
}
没有找到相关结果
已邀请:
1 个回复
括宠
不存在的元素。我猜
行会抛出异常,因为
为空。 带有来自xml文件的自定义行布局的方法的正确实现是膨胀该布局。查看本教程。它适用于普通的ListViews和适配器,但膨胀行布局的概念相同。