我是Android新手。我想创建一个可扩展的ListView,其中1行具有必须在manin线程中指定的自定义信息,而不是在布局中进行硬编码。
我想要一张图片和2张textview。我想我需要一个自定义布局文件,但我不知道在我的代码中可以在哪里调用它。请协助。
以下是我的自定义适配器中的GropView fxn。我希望case 0加载自定义布局文件
public View getGroupView(int groupPosition, boolean arg1, View convertView,
ViewGroup arg3) {
String laptopName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.laptop);
item.setTypeface(null, Typeface.BOLD);
item.setText(laptopName);
this.context = (Activity) context;
switch (groupPosition) {
case 0:
convertView.setBackgroundColor(this.context.getResources()
.getColor(R.color.dark_blue));
convertView.inflate(R.layout.first_row_layout, 0, arg3);
break;
case 1:
convertView.setBackgroundColor(this.context.getResources()
.getColor(R.color.purple));
break;
case 2:
convertView.setBackgroundColor(this.context.getResources()
.getColor(R.color.green));
break;
default:
break;
}
return convertView;
}
答案 0 :(得分:0)
代码中定义组项目布局的位置如下:
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item, null);
}
在您的情况下,您使用布局R.layout.group_item作为您的群组项目。如果要为每个组位置加载不同的布局,则需要将这部分代码移到switch-case中。您必须小心ListView中的图像,有关此处的更多信息:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html