如何将自定义视图放入列表片段的自定义列表视图中

时间:2013-09-07 20:04:23

标签: android custom-view

我有一个列表片段的自定义布局。在自定义列表布局中,我想放置另一个自定义视图(绘制内容)。所以我的问题是:如何在方法getView()中为convertView添加另一个视图。 这是我的getView方法:

private class TrainingAdapter extends ArrayAdapter<Product>
{

    public TrainingAdapter(ArrayList<Product> list) {
        super(getActivity(), 0, list);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
        {
            convertView = getActivity().getLayoutInflater().inflate(R.layout.trainings_custom_layer_listfragment, null);
        }

        Product p = getItem(position);
        //references to text view and image view goes here
        //begin to work with custom view
        StarView sv = (StarView) convertView.findViewById(R.id.star_view);
        return convertView; }

这是我的布局:     

<ImageView android:id="@+id/imageview_product_category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="mini photo" />

<TextView android:id="@+id/textview_training_title"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_location"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_title"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_datetime"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_location"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_cost"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_datetime"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_registrationenddate"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_cost"
    android:textColor="@color/text_color_white" />

<jacky.practice.view_custom.StarView 
    android:id="@+id/star_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_registrationenddate"/>

自定义视图代码:

public class StarView extends View {
private Paint myPaint;
private int mLevel = 0;

public StarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public StarView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public StarView(Context context) {
    super(context);
    init();
}

private void init()
{
    myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    myPaint.setColor(Color.GREEN);
}

public void setExpensive(int level)
{
    mLevel = level;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(mLevel <= 3)
    {
        canvas.drawCircle(20, 20, 8, myPaint);
    }
    else if((mLevel > 3) && (mLevel <= 5))
    {
        canvas.drawCircle(20, 20, 8, myPaint);
        canvas.drawCircle(40, 20, 8, myPaint);
    }
    else
    {
        canvas.drawCircle(20, 20, 8, myPaint);
        canvas.drawCircle(40, 20, 8, myPaint);
        canvas.drawCircle(60, 20, 8, myPaint);
    }

    canvas.restore();
}

}

1 个答案:

答案 0 :(得分:0)

您是否忘记实施onCreateView?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.main, null);
    return view;
}