2010-09-02 45 views
7

我是新來的機器人,並結束了(得)這裏問一個問題,列表項(視圖)爲了unexpextedly變化而(快)在ListView滾動

讓我們把它簡單,我只是想讓我自己般的TextView (MyView的擴展視圖),

這是我的代碼:

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, 0, 0, mPaint); 
} 

private void initBitmap2() { 
    mBitmap2 = Bitmap.createBitmap(30, Fonts.getHeight(), Bitmap.Config.ARGB_4444); 
    Canvas canvas = new Canvas(mBitmap2); 
    canvas.drawText(mText, 0, 0, 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(), 0, mPaint); 
    initBitmap2(); 
    canvas.drawBitmap(mBitmap2, getLeft(), 30, mPaint); 
} 

}

在我的代碼

,我填充MyView的的一些數字(LE我的問題是爲什麼mBitmap1的順序隨機變化,而我快速滾動 (上下)(如果我滾動緩慢,這個問題不會發生)..?????????????

mBitmap2會留在那些應該..

+2

還好,問題在這個線程被sholved:http://groups.google.com/group/android-developers/browse_thread/thread/e508e2d20b1fcda9/0785597e31b51cd3#0785597e31b51cd3 – siriusdely 2010-09-08 03:19:41

回答

0

你必須使用一個視圖holder類爲了克服這個問題。是這樣的:

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 
} 
相關問題