Search code examples
androidbaseadapter

viewholder caused the first view item wrongly displaying and some of views splashing


I found an issues of Android BaseAdapter viewHolder. Like this: I use viewholder always cause that the first view item wrongly displaying . Just like the first item view splashing or some of views splash. Most of this issues caused in Low OS Android Device. I don't know why. but I stop using viewHolder fix the problem.
why?

public class TreasureBoxAdapter extends BaseTreasureBoxAdapter {
private static final String TAG = "TreasureBoxAdapter";
private static final boolean DEBUG = FeatureConfig.DEBUG_LOG;

private final AbsListView.LayoutParams param;
private final int mViewHolderHeight;

public TreasureBoxAdapter(Context context, List<?> items, int columnCount, int mScreenWidth) {
    super(context, items, columnCount);
    mViewHolderHeight = mScreenWidth / context.getResources().getInteger(R.integer.column_count);
    param = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            mScreenWidth / context.getResources().getInteger(R.integer.column_count));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
      if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.treasure_box_grid_item, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
      } else {
          holder = (ViewHolder) convertView.getTag();
      }
    holder.build(position);

    holder.item.setLayoutParams(param);
    return convertView;
}

public class ViewHolder extends TreasureViewHolder{

    public ViewHolder(View view) {
        item = (LinearLayout) view.findViewById(R.id.treasure_item);
        title = (TextView) view.findViewById(R.id.item_title);
        image = (ImageView) view.findViewById(R.id.item_img);
        remind = new RemindView(getContext(), image);
    }

    @Override
    public void build(int pos) {
        TreasureBean item = (TreasureBean) getItem(pos);
        item.buildView(getContext(), this, pos);
    }
}

Solution

  • Use RecyclerView instead of ListView and in your adapter extend RecyclerView.Adapter, that will work. eg.

    public class YourAdapterName extends         
    RecyclerView.Adapter<YourAdapterName.MyViewHolder> {
    }