我有一個ListView
適配器,根據行項目的類型設置不同的佈局。回收具有不同行佈局的ListView行
如果我使用一個持有人回收我的行,如下面的代碼所示,我想我會得到一些錯誤,因爲對於一個循環的行,我會得到最新的非循環行的佈局,這可能不會屬於同一類型。
我應該避免在這種情況下回收行嗎?我還有什麼其他選擇?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyHolder holder = null;
int type = getItemViewType(position);
if(row == null)
{
holder = new MyHolder();
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
if(type == TYPE_1){
row = inflater.inflate(R.layout.layout_type_1, parent, false);
}
else if(type == TYPE_2){
row = inflater.inflate(R.layout.layout_type_2, parent, false);
}
else {
row = inflater.inflate(R.layout.layout_type_3, parent, false);
}
row.setTag(holder);
}
else
{
holder = (MyHolder)row.getTag();
}
/* ... */
}
你是否重寫getViewTypeCount和getItemViewType? – jimmithy 2013-03-04 16:16:20
@jimmithy:是的,我做 – jul 2013-03-04 16:18:45
回收RecyclerView和多行佈局示例演示http://code2concept.blogspot.in/2015/10/android-multiple-row-layout-using.html回收處理回收站本身 – nitesh 2015-10-15 05:26:59