开发者

custom Listview with checkbox, seekbar and textview

I need to make a ListView with this specific attributes:

1) Every row has a CheckBox on the left , seekbar below check box and textview below seekbar ;

2) The seekbar is active and only if the correlated CheckBox is checked.

3) textview displays progress of seekbar

Please 开发者_StackOverflow中文版help me


Create a ListView in your main layout file first

<ListView
    android:id="@+id/colorStockList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/vehicleOption"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp" >
</ListView>

and a separate layout file which will contain your checkbox, seekbar and textview. Create other layout file as if it's a view in itself. Ex:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
    android:id="@+id/colorCode"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<SeekBar
    android:id="@+id/countSeekBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:progress="20"
    android:secondaryProgress="20" />

<EditText
    android:id="@+id/countEditText"
    android:layout_height="50dp"
    android:layout_width="wrap_content"
    android:gravity="end"
    android:inputType="number" />

</RelativeLayout>

Now you can use ViewHolder to populate this list. Sample ViewHolder for above example

public class ColorStockListViewAdapter extends ArrayAdapter<Color> {

Context context;

public ColorStockListViewAdapter(Context context, int resourceId,
        List<Color> items) {
    super(context, resourceId, items);
    this.context = context;
}

/* private view holder class */
private class ViewHolder {
    // ImageView vehicleImageView;
    TextView colorDesc;
    SeekBar seekBar;
    EditText countText;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    Color rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.color_stock_list, null);
        holder = new ViewHolder();

        holder.colorDesc = (TextView) convertView
                .findViewById(R.id.colorCode);
        holder.seekBar = (SeekBar) convertView
                .findViewById(R.id.countSeekBar);
        holder.countText = (EditText) convertView
                .findViewById(R.id.countEditText);

        holder.seekBar.setTag(holder.countText);
        holder.countText.setTag(holder.seekBar);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.colorDesc.setText(rowItem.getDescription());
    holder.seekBar.setMax(100);
    holder.countText.setText("0");

    return convertView;
}
}

And this is how you'll call it in your Activity file

ColorStockListViewAdapteradapter = new ColorStockListViewAdapter(this,
            R.layout.activity_sales_list, colors);

where your activity_sales_list is our layout file you created to populate in ListView. Also to attach checkbox to seekbar and seekbar to checkbox, use setTag(), ex:

checkbox.setTag(seekbar);

Now the seekbar for a checkbox can be fetched like

SeekBar seekbar = checkbox.getTag();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜