android - Issue drawing custom item in a ListView -
i need create custom listview item textview , colored rectangle.
to have arrayadapter:
public class jobquicklistadapter extends arrayadapter<job>{ private int resource; public jobquicklistadapter(context context, int resource, list<job> items) { super(context, resource, items); this.resource = resource; } @override public view getview(int position, view convertview, viewgroup parent) { jobtagview jobview; job item = getitem(position); string jobtitle = item.gettitle(); if (convertview == null) { jobview = new jobtagview(getcontext()); string inflater = context.layout_inflater_service; layoutinflater vi = (layoutinflater)getcontext().getsystemservice(inflater); vi.inflate(resource, jobview, true); } else { jobview = (jobtagview) convertview; } jobview.setcolor(color.blue); textview jobtitletext = (textview)jobview.findviewbyid(r.id.jobitemtitle); jobtitletext.settext(jobtitle); return jobview; } }
and custom view extending linearlayout:
public class jobtagview extends linearlayout{ private paint paintorig; private rectf originrect; private boolean pathcreated; private int color; public jobtagview(context context) { super(context); initjobtag(); } public jobtagview(context context, attributeset attrs) { super(context, attrs); initjobtag(); } private void initjobtag(){ setwillnotdraw(false); paintorig = new paint(); paintorig.setstyle(paint.style.fill); pathcreated = false; } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); if (!pathcreated){ float hr = h * 14 / 100; float wr = w * 2 / 100; originrect = new rectf(0, hr, wr, h - hr); pathcreated = true; } } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawrect(originrect, paintorig); } public int getcolor() { return color; } public void setcolor(int color) { paintorig.setcolor(color); this.color = color; this.invalidate(); } }
and layout:
<com.sinplanb.jobsniffer.utils.jobtagview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.sinplanb.jobsniffer.utils" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="5dp" android:background="@drawable/job_tab_search"> <textview android:id="@+id/jobitemtitle" android:textisselectable="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="2" android:textsize="16sp"/> </com.sinplanb.jobsniffer.utils.jobtagview>
my problem comes when colored rectangle drawn, because rectangle seems drawn several times , small displacement each time. first black rectangle in right position , wrong blue rectangle (the 1 created after changing rectangle color --> jobview.setcolor(color.blue)) totally displaced.
what doing wrong? there tutorial drawing custom items in list view?
Comments
Post a Comment