ListPopupWindow 项目未正确绘制

发布于 2024-12-27 00:55:21 字数 5794 浏览 0 评论 0原文

我有一个 ListPopupWindow,它是从自定义适配器填充的。自定义 xml 由 2 个文本视图和一个 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>

I have a ListPopupWindow that is being populated from a custom adapter. The custom xml consists of 2 textviews and a LinearLayout. The custom adapter populates the textviews and instantiates a custom view that draws a thumbnail sized vector based graphic. The custom view is then added to the Linear Layout.

The problem is that the first row displayed always has the wrong graphic displayed. It is sometimes the graphic from another row, sometimes half of it is the correct graphic, half from the wrong row. I've also noticed that if the list has enough rows in it to scroll, the items at the bottom of the list have the same problem when scrolling.

Has anyone else experienced this?

Custom List Adapter:

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 class:

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

        }    

    }

Referenced Classes:

    public class Point 
    {
        public float x, y;

        public PointType Type;      
    }

    public enum PointType 
    { 
        Start, 
        Midpoint , 
        End;

    }

XML for List row:

    <?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文