importjava.util.ArrayList;importjava.util.List;importandroid.app.Dialog;importandroid.content.Context;importandroid.os." />

简单实现可以多选的ProductListDialog

系统 1460 0
只是一个范例,是为了代码快速迭代而写的
使用了listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
效果图

简单实现可以多选的ProductListDialog<T>


    
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

简单实现可以多选的ProductListDialog


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论