在我的Android應用程序中,我有包含汽車列表的ListView。每輛車都有(1至10)組的列表。如何在列表適配器中正確使用ViewHolder和自定義視圖
在每個列表項中我都有水平的組列表。我在這個水平列表中使用FlowLayout,爲此添加了「手動」視圖。
林不知道我使用這個ViewHolder概念是完全錯誤的嗎?
至少這比每個項目(FlowLayout)內部的水平列表都要消耗更多的內存。
我應該有自己的列表適配器這個水平列表,或者如何改善這個?
/**
* List adapter (BaseAdapter), getView
*
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Car car = (Car) getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_cars, null);
holder = new ViewHolder();
holder.carName = (TextView)convertView.findViewById(R.id.car_name);
holder.carGroups = (FlowLayout)convertView.findViewById(R.id.car_groups);
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.carName.setText(car.getName());
buildGroupsFlowLayout(holder.carGroups, car.getGroups());
return convertView;
}
/**
* Build FlowLayout
*/
private void buildGroupsFlowLayout(FlowLayout flowLayout, List<CarGroup> groupsList) {
flowLayout.removeAllViews();
int i = 0;
for(CarGroup group : groupsList) {
View groupItemView = mInflater.inflate(R.layout.car_group_item, null);
TextView lineView = (TextView)groupItemView.findViewById(R.id.car_group_item_goup_text);
lineView.setText(group.getName());
lineView.setTextColor(group.getColor());
flowLayout.addView(groupItemView, i, new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT));
i++;
}
}
public static class ViewHolder {
public TextView carName;
public FlowLayout carGroups;
}
對於這樣的任務,'RecyclerView' +'GridLayoutManager'用自己的適配器,而不是'FlowLayout'可能會更好。 – aeracode