【发布时间】:2015-06-26 02:51:16
【问题描述】:
在我的 Android 应用程序中,我有包含汽车列表的 ListView。每辆车都有(1 到 10)组列表。
在每个列表项中,我都有水平组列表。我为这个水平列表使用 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可能会更好。
标签: android performance android-listview baseadapter android-viewholder