2010-09-02 22 views
7

Tôi mới đến android và đã kết thúc (phải) đặt câu hỏi tại đây,Mục Danh sách (xem) thay đổi thứ tự không được giải phóng trong khi cuộn (nhanh) trong ListView

Hãy đơn giản, tôi chỉ muốn để làm của riêng tôi TextView giống như (MyView mở rộng View),

đây là mã của tôi:

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); 
} 

}

trong mã của tôi, tôi điền một số số lượng MyView (le t nói rằng 20) trong một ListActivity

câu hỏi của tôi là lý do để mBitmap1 của thay đổi một cách ngẫu nhiên trong khi tôi di chuyển (lên xuống) nhanh chóng tiện lợi (nếu tôi di chuyển chậm, vấn đề này không xảy ra) ..?

mBitmap2 vẫn ở vị trí cần có.

+2

ổn, vấn đề đã được sholved trong luồng này: http://groups.google.com/group/android-developers/browse_thread/thread/e508e2d20b1fcda9/0785597e31b51cd3#0785597e31b51cd3 – siriusdely

Trả lời

0

bạn phải sử dụng lớp chế độ xem để khắc phục sự cố này. một cái gì đó như thế này:

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 
} 
Các vấn đề liên quan