【问题标题】:How to correctly use ViewHolder with custom view in list adapter如何在列表适配器中正确使用 ViewHolder 和自定义视图
【发布时间】: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


【解决方案1】:

尝试为您的 Car(如 CarView)创建包含所有 UI 逻辑的自定义视图,然后将其用作 ViewHolder。

可能是这样的:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    CarView carView;
    if(convertView == null){
        carView = new CarView(context);
    } else {
        carView = (CarView) convertView;
    }
    carView.setData(getItem(position));
    return carView;
}

还有 CarView(你可以把你的 FlowLayout 逻辑放在这里):

public class CarView extends FrameLayout{

  public SeriesTileView(Context context) {
    this(context, null);
  }

  public SeriesTileView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public SeriesTileView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    inflate(context, R.layout.view_car, this);
    findViews();
  }  

  public void setData(Car car) {
    nameTextView.setText(car.getName());
  }

}

【讨论】:

  • 与我原来的实现相比,我想这并没有太大变化?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 2011-12-31
相关资源
最近更新 更多