我想创建列表视图,其中我希望所有不同的行都有不同的布局。
那么如何创建自定义适配器来为不同的行设置不同的布局。
任何帮助将不胜感激。
先感谢您。
你需要延长你的Adapter
,并覆盖其getView
method.
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resource;
// Here you set ‘resource’ with the correct layout, for the row
// given by the parameter ‘position.’
//
// E.g.:
//
// switch (someArray[position].type) {
// case SOME_TYPE_A: resource = R.layout.a; break;
// case SOME_TYPE_B: resource = R.layout.b; break;
// ...
// }
View rowView = inflater.inflate(resource, parent, false);
// Here you initialize the contents of the newly created view.
//
// E.g.:
// switch (resource) {
// case R.layout.a:
// TextView aA = (TextView) rowView.findViewById(R.id.aa);
// aA.setText("View 1");
// ...
// break;
// case R.layout.b:
// TextView bB = (TextView) rowView.findViewById(R.id.bb);
// bB.setText("View 2");
// ...
// break;
// ...
// }
return rowView;
}
有关适配器以及如何扩展它们的更多示例,请参阅下面的链接。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)