我正在制作一个 listView,我想为三种不同的布局充气。每行示例都有一个条件:如果 x=="F" R.layout.row1 应该膨胀,如果 x=="t" R.layout.row2 应该膨胀,我有三行.. I尝试使用
@Override
public int getViewTypeCount() {
return 3;
}
//
@Override
public int getItemViewType(int position) {
objBean = items.get(position);
if (objBean.gettype() == "I") {
return layout2;
} else if (objBean.gettype() == "F") {
return layout1;
} else {
return layout3;
}
return i;
}
并在 getview 方法中
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
objBean = items.get(position);
int type = getItemViewType(position);
String t = objBean.gettype();
// if(i == 0){
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (type == layout2) {
view = inflater.inflate(R.layout.row1, null);
} else if (type == layout1) {
view = inflater.inflate(R.layout.list, null);
} else{
view = inflater.inflate(R.layout.twitter, null);
}
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
在哪里
int layout1 = 0;
int layout2 = 1;
int layout3 = 2;