只是一个范例,是为了代码快速迭代而写的
使用了listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
效果图
布局:
单个Item布局:item_product_list.xml
选中的背景:bg_item.xml
用法:
Android开发技巧——自定义单选或多选的ListView
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0619/3089.html
使用了listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
效果图
import java.util.ArrayList; import java.util.List; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.mb.bgfitting.CommonAdapter; import com.mb.bgfitting.ViewHolder; import com.mb.bgfitting.app.R; import com.mb.bgfitting.model.BillSaleDetailInfo; /** * * @author pythoner * */ public class ProductListDialog<T> extends Dialog { private Context context; private ListView listView; private List<T> beans; private List<T> checkedBeans = new ArrayList<T>(); private CommonAdapter<T> adapter; public ProductListDialog(Context context, List<T> beans) { this(context, R.style.Theme_Dialog_NoTitle, beans); // TODO Auto-generated constructor stub } public ProductListDialog(Context context, int theme, List<T> beans) { super(context, theme); // TODO Auto-generated constructor stub this.context = context; this.beans = beans; init(); } private void init() { this.setCanceledOnTouchOutside(true); this.setCancelable(true); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.dialog_product_list); initViews(); // initValues(); } private void initViews() { if (beans == null) { return; } findViewById(R.id.btn_ok).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (checkedBeans.size() == 0) { Toast.makeText(context, "请选择至少一件商品", Toast.LENGTH_LONG).show(); return; } if (onOkClickListener != null) { onOkClickListener.onOkClick(v, checkedBeans); } dismiss(); } }); listView = (ListView) findViewById(R.id.listView); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(adapter = new CommonAdapter<T>(context, beans, R.layout.item_product_list) { @Override public void setValues(ViewHolder helper, T item, final int position) { // TODO Auto-generated method stub if (item instanceof BillSaleDetailInfo) { BillSaleDetailInfo bean = (BillSaleDetailInfo) item; helper.setText(R.id.item_0, bean.getProdName()); helper.setText(R.id.item_1, bean.getColorName()); helper.setText(R.id.item_2, bean.getSizeName()); } } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { // TODO Auto-generated method stub T bean = beans.get(position); if (listView.isItemChecked(position)) { checkedBeans.add(bean); } else { if (checkedBeans.contains(bean)) { checkedBeans.remove(bean); } } } }); } private void initValues() { // 不能写在init()中 Window window = getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); DisplayMetrics dm = context.getResources().getDisplayMetrics(); lp.width = dm.widthPixels; lp.gravity = Gravity.BOTTOM; window.setAttributes(lp); } private OnOkClickListener<T> onOkClickListener; public interface OnOkClickListener<T> { public void onOkClick(View v, List<T> list); } public void setOnOkClickListener(OnOkClickListener<T> onOkClickListener) { this.onOkClickListener = onOkClickListener; } }
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/dialog_width" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:cacheColorHint="@android:color/transparent" /> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="确定" android:textSize="@dimen/font_big" /> </LinearLayout>
单个Item布局:item_product_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/bg_item" > <TextView android:id="@+id/item_0" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:textColor="@color/base_text_black" android:textSize="@dimen/font_middle" android:layout_weight="1" /> <TextView android:id="@+id/item_1" android:layout_width="40dp" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:textColor="@color/base_text_black" android:textSize="@dimen/font_middle" /> <TextView android:id="@+id/item_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:layout_weight="1" android:textColor="@color/base_text_black" android:textSize="@dimen/font_middle" /> </LinearLayout>
选中的背景:bg_item.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/base_blue" android:state_activated="true"></item> <item android:drawable="@android:color/transparent"/> </selector>
用法:
ProductListDialog<BillSaleDetailInfo> d=new ProductListDialog<BillSaleDetailInfo>(context,billSaleDetailInfos); d.setOnOkClickListener(new ProductListDialog.OnOkClickListener<BillSaleDetailInfo>() { @Override public void onOkClick(View v, List<BillSaleDetailInfo> list) { // TODO Auto-generated method stub addToShoppingCartIfNecessary(list); } }); d.show();
Android开发技巧——自定义单选或多选的ListView
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0619/3089.html