ListPopupWindow项目绘制不正确

时间:2012-01-16 10:46:55

标签: android listview popupwindow

我有一个从自定义适配器填充的ListPopupWindow。自定义xml由2个textviews和一个LinearLayout组成。自定义适配器填充文本视图并实例化绘制基于缩略图大小的矢量图形的自定义视图。然后,自定义视图将添加到“线性布局”中。

问题是显示的第一行始终显示错误的图形。它有时是来自另一行的图形,有时一半是正确的图形,一半来自错误的行。我还注意到,如果列表中有足够的行进行滚动,则列表底部的项目在滚动时会出现同样的问题。

还有其他人经历过这个吗?

自定义列表适配器:

public class PageSearchListAdapter extends SimpleCursorAdapter
{
    int _height = 130;
  int _width = 130;

  Cursor c;
  Context context;

  // Constructor, here we store any parameters we need in class variables 
  //
    public PageSearchListAdapter(Context context, 
                                 int layout, 
                                 Cursor c,
                                 String[] from, 
                                 int[] to) 
    {
        super(context, layout, c, from, to);

    this.c = c;
    this.context=context;

    }

    // Main view method - the views in query_list_item are populated here
    //
  @Override
  public View getView(int position, View convertView, ViewGroup parent) 
  {
    // Make sure we have a view to work with
    //
    if(convertView == null)
        convertView = View.inflate(context, R.layout.query_list_item, null);

    View row = convertView; 

    // go to the correct row in the cursor
    //
    c.moveToPosition(position);

    // Get the views that need populating
    //
    TextView PageNum = (TextView) convertView.findViewById(R.id.Page_Num);
    LinearLayout pic = (LinearLayout) convertView.findViewById(R.id.Page_Canvas);
    TextView Date = (TextView) convertView.findViewById(R.id.Page_Date);

    // Set the values
    //
    PageNum.setText(c.getString(c.getColumnIndex("PageNum")));
    Date.setText(c.getString(c.getColumnIndex("Timestamp")));

    List<Point> Vectors = new ArrayList<Point>();

    byte[] asBytes = c.getBlob(c.getColumnIndex("Vectors"));

    ..
    .. removed code to Convert Blob to array of 'Vector' records ...
    ..

    // Draw the page
    //

    NotePadPage npPage = new NotePadPage(context, Vectors);

    npPage.setLayoutParams(new LayoutParams(_width, _height));

    pic.addView(npPage);

    return(row);
  }

}

NotePadPage类:

    // Adapter to display a custom list item in a list view
    //
    public class NotePadPage extends View
    {

        int _height = 130;
        int _width = 130;

        Bitmap _bitmap;
        Canvas _canvas;

        Paint _paint;

        List<Point> _Vectors = new ArrayList<Point>();


        public NotePadPage(Context context, List<Point> Vectors) 
        {
            super(context);

            _Vectors = Vectors;

            _paint = new Paint();
            _paint.setColor(Color.WHITE);
            _paint.setStyle(Paint.Style.STROKE);

        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
        {
            _height = View.MeasureSpec.getSize(heightMeasureSpec);
            _width = View.MeasureSpec.getSize(widthMeasureSpec);

            setMeasuredDimension(_width, _height);

            _bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
            _canvas = new Canvas(_bitmap);        
        }

        @Override
        protected void onDraw(Canvas canvas) 
        {
            float yRatio = 1092/ _height;
            float xRatio = 800 / _width;

            Point lastPoint = null;

            for (Point point : _Vectors) 
            {
                switch (point.Type)
                {
                    case Start:
                    {
                        lastPoint = point;
                        break;
                    }
                    case Midpoint:
                    case End:
                    {
                        canvas.drawLine(lastPoint.x / xRatio, lastPoint.y/ yRatio, point.x / xRatio, point.y/ yRatio, _paint);
                        lastPoint = point;                  
                    }
                }
            }

        }    

    }

参考类:

    public class Point 
    {
        public float x, y;

        public PointType Type;      
    }

    public enum PointType 
    { 
        Start, 
        Midpoint , 
        End;

    }

列表行的XML:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:gravity="center_vertical|center_horizontal"    >

        <TextView        
            android:id="@+id/Page_Num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginRight="5dp"        
            android:text="1"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:id="@+id/Page_Canvas"
            android:layout_width="130dip"
            android:layout_height="130dip"
            android:gravity="center_vertical|center_horizontal"
            android:layout_toRightOf="@id/Page_Num">
        </LinearLayout>    

        <TextView
            android:id="@+id/Page_Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="5dp"        
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:layout_toRightOf="@id/Page_Canvas">

        </TextView>

    </RelativeLayout>

0 个答案:

没有答案