0

我需要创建一个带有 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>

我的问题是在绘制彩色矩形时出现的,因为矩形似乎被绘制了多次并且每次都有一个小的位移。第一个位于正确位置的黑色矩形和一个错误的蓝色矩形(在更改矩形颜色后创建的那个 -> jobView.setColor(Color.BLUE))完全移位。

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

4

0 回答 0