android – 列表项(视图)顺序在ListView中快速滚动时快速地改变

我是 android的新手,最后(不得不)在这里问一个问题,

让我们简单一点,我只想制作自己的TextView
(MyView扩展了View),

这是我的代码

public class MyView extends View {
    private Paint mPaint;
    private String mText;
    private Bitmap mBitmap1;
    private Bitmap mBitmap2;
    public MyView(Context context) {
        super(context);
        initView();
    }

public MyView(Context context,AttributeSet attrs) {
    super(context,attrs);
    initView();
}

private final void initView() {
    mPaint = new Paint();
}

public void setText(String text) {
    mText = text;
}

@Override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
    int measuredWidth = measureWidth(widthMeasureSpec);
    if (mBitmap1 == null) initBitmap1(measuredWidth);
    int measuredHeight = measureHeight(heightMeasureSpec);
    setMeasuredDimension(measuredWidth,measuredHeight);
}

private void initBitmap1(int measuredWidth) {
    mBitmap1 = Bitmap.createBitmap(measuredWidth,Fonts.getHeight(),Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(mBitmap1 );
    canvas.drawText(mText,mPaint);
}

private void initBitmap2() {
    mBitmap2 = Bitmap.createBitmap(30,Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(mBitmap2);
    canvas.drawText(mText,mPaint);
}

private int measureWidth(int widthMeasureSpec) {
    int measuredWidth = 0;
    int specWidthMode = MeasureSpec.getMode(widthMeasureSpec);
    int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);

    if (specWidthMode == MeasureSpec.EXACTLY) {
        measuredWidth = specWidthSize;
    } else {
        measuredWidth = getWidth();
        if (specWidthMode == MeasureSpec.AT_MOST) {
            measuredWidth = Math.min(measuredWidth,specWidthSize);
        }
    }
    return measuredWidth;
}

private int measureHeight(int heightMeasureSpec) {
    int measuredHeight = 0;
    int specHeightMode = MeasureSpec.getMode(heightMeasureSpec);
    int specHeightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (specHeightMode == MeasureSpec.EXACTLY) {
        measuredHeight = specHeightSize;
    } else {
        measuredHeight = 80;
        if (specHeightMode == MeasureSpec.AT_MOST) {
            measuredHeight = Math.min(measuredHeight,specHeightSize);
        }
    }
    return measuredHeight;
}

@Override
protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
   canvas.drawBitmap(mBitmap1,getLeft(),mPaint);
   initBitmap2();
   canvas.drawBitmap(mBitmap2,30,mPaint);
}

}

在我的代码中,我填充了一些MyView(比方说20)
ListActivity

我的问题是为什么当我滚动时mBitmap1的顺序会随机变化
(快速上下)(如果我慢慢滚动,这个问题不会发生)..?

mBitmap2保留在那些应该是…

解决方法

您必须使用视图持有者类才能克服此问题.这样的事情:
public View getView(final int i,View view,final ViewGroup viewGroup) {
    View vi = view;
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        vi = inflater.inflate(R.layout.listview_row_cart,null);
        holder.yourbutton = (Button) vi.findViewById(R.id.btn);
        vi.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
        holder.yourbutton.setText("Start");
 }
 class ViewHolder {
       Button yourbutton
}

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...