我有一个自定义列表视图,我想在其中膨胀三种不同的布局。
我见过很多问题,但它们都是奇数和偶数位置,但在我的 listView 中,应该膨胀到 listView 的布局取决于其他条件及其动态
如果 (i == 0) 我得到第一个布局被膨胀,如果 (i==1) 第二个等等,
变量“i”等于我从主活动中获得的值。
public class SocialListAdapter extends ArrayAdapter<Item> {
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
private int i;
public SocialListAdapter(Activity act, int resource, List<Item> arrayList) {
super(act, resource, arrayList);
// TODO Auto-generated constructor stub
this.activity= act;
this.items = arrayList;
this.row = resource;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getCount() {
return items.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
objBean = items.get(position);
i = objBean.getI();
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(i == 0){
view = inflater.inflate(R.layout.list, null);
} else if (i == 1){
view = inflater.inflate(R.layout.row1, null);
}
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
我尝试了这段代码,但效果很好......
当我滚动浏览 listView arrayoutofbondexception 时出现错误