在ListView中发出绘制自定义项

时间:2013-04-01 10:42:40

标签: android android-listview customization

我需要使用TextView和彩色矩形创建自定义ListView项。

要做到这一点,我有一个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;
    }
}

我的自定义视图从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();
    }
}

布局:

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

我的问题出现在绘制彩色矩形时,因为矩形似乎被绘制了几次并且每次都有一个小的位移。右侧位置的第一个黑色矩形和错误的蓝色矩形(更改矩形颜色后创建的矩形 - &gt; jobView.setColor(Color.BLUE))完全取代。

我做错了什么?是否有关于在列表视图中绘制自定义项目的教程?

0 个答案:

没有答案