简体   繁体   English

如何构建自定义ListView,其中每行都有矩形和圆形

[英]How to build custom ListView in which each row has rectangles and circles

I have a class DrawView.java which draw a rectangle and I want to make a ListView of rectangle. 我有一个DrawView.java类,它绘制一个矩形,我想制作一个矩形的ListView。 I tried to draw the rectangle in getView() method of List adapter but I failed. 我试图在列表适配器的getView()方法中绘制矩形,但失败了。

Can anyone help me in this issue? 谁能帮助我解决这个问题?

My class which draw the rectangle is: 我绘制矩形的班级是:

    public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);            
    }

    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawRect(30, 30, 180, 180, paint);
        paint.setStrokeWidth(0);
        paint.setColor(Color.CYAN);
        canvas.drawRect(33, 60, 177, 177, paint );
        paint.setColor(Color.YELLOW);
        canvas.drawRect(33, 33, 177, 60, paint );

    }

}

and my getView() method of custom adapter is: 我的自定义适配器的getView()方法是:

               public View getView(final int position,  View convertView, final ViewGroup parent) {

        convertView = mInflater.inflate(R.layout.news_list_item,null);
        DrawView rl = (DrawView)convertView.findViewById(R.id.drawview);
        DrawView draw = new DrawView(c);
        rl=draw;
        return convertView;
}

My XML file is news_list_item: 我的XML文件是news_list_item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="60dp"
    android:id="@+id/mainLayout"
     >

     <com.example.horizontalscrollview.DrawView
        android:id="@+id/drawview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
      </com.example.horizontalscrollview.DrawView>



</LinearLayout>

You need to create a list adapter by extending BaseAdapter. 您需要通过扩展BaseAdapter创建列表适配器。 Your getView method is not properly using the convertView parameter. 您的getView方法未正确使用convertView参数。 I recommend you watch The World of ListView , which covers proper use of ListView. 我建议您观看《 ListView的世界》 ,其中介绍了ListView的正确使用。

UPDATE UPDATE

  1. Do not inflate a layout every time; 不要每次都给布局充气。 only do so when convertView is null . 仅当convertView为null时才这样做。 The whole point of ListView is to recycle its views when they scroll off screen. ListView的全部重点是在屏幕上滚动时回收其视图。
  2. I see no reason for you to create a new instance of DrawView in getView() . 我认为没有理由在getView()创建DrawView的新实例。 It should be in the XML layout you are inflating. 它应该在您要夸大的XML布局中。
  3. The statement r1=draw; 语句r1=draw; will not change the layout of the row in any way. 不会以任何方式更改行的布局。 All you've done is change the reference r1 to point to a different View object (the new DrawView), but that view is not actually attached to the layout in any way. 您要做的就是将参考r1更改为指向另一个View对象(新的DrawView),但是该视图实际上并没有以任何方式附加到布局。 If you want to add it to the layout, you need to call addView() on some other view which you intend to be the container for the new DrawView. 如果要将其添加到布局中,则需要在其他打算作为新DrawView容器的视图上调用addView()

If you haven't done so yet, please watch The World of ListView (link above) and follow the coding patterns presented. 如果您尚未这样做,请观看“ ListView的世界”(上面的链接),并遵循显示的编码模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM