双listview实现两级选择的 dialog

1.对话框的布局文件




    

        

        
    

    

        
        

        
        
    


其中的字体的style如下


dialog 类相对

/**
 * 带有两个并列的listview的  dialog
 * @author wtw
 * 2016-1-14下午2:05:49
 */
public class DoubleListDialog extends Dialog{

	private Context context;
	private TextView mTvTitle1;//标题一
	private TextView mTvTitle2;//标题二
	private ListView mLvList1;
	private ListView mLvList2;
	public static View partOne;//listview的item
	
	private ArrayList listData1;//list数据一
	private ArrayList> AlllistData2;//list数据二
	private ArrayList listData2=new ArrayList();//list数据二
	private String data1;//选择的数据一
	private String data2;//选择的数据二
	
	private CallBack callBack;//点击后的数据返回
	
	public DoubleListDialog(Context context,ArrayList listData1,ArrayList> AlllistData2,CallBack callBack) {
		super(context,R.style.publish_dialog);
		this.context=context;
		this.listData1=listData1;
		this.AlllistData2=AlllistData2;
		this.callBack=callBack;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		data1=listData1.get(0);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_double_list, null);
		setContentView(view);
		
		//设置dialog的大小
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.9); // 宽度设置为屏幕宽度的 90%
		dialogWindow.setAttributes(lp);
		
		mTvTitle1=(TextView)view.findViewById(R.id.tv_title1);
		mTvTitle2=(TextView)view.findViewById(R.id.tv_title2);
		
		mLvList1=(ListView)view.findViewById(R.id.lv_list1);
		mLvList2=(ListView)view.findViewById(R.id.lv_list2);
		DoubleListDialogAdapter adapter1 =new DoubleListDialogAdapter(context, listData1);
		mLvList1.setAdapter(adapter1);
		listData2.addAll(AlllistData2.get(0));
		final DoubleListDialogAdapter2 adapter2 =new DoubleListDialogAdapter2(context, listData2);
		mLvList2.setAdapter(adapter2);
		mLvList1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if(partOne!=null){
					partOne.setBackgroundColor(context.getResources().getColor(R.color.white));
				}
				partOne=arg1;
				partOne.setBackgroundColor(context.getResources().getColor(R.color.dialog_list));
				data1=listData1.get(arg2);
				listData2.clear();
				listData2.addAll(AlllistData2.get(arg2));
				adapter2.notifyDataSetChanged();
			}
		});
		mLvList2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				data2=listData2.get(arg2);
				callBack.clickResult(data1,(data2.equals("全部")?"":data2));
			}
		});
	}
	
	public interface CallBack{
		public void clickResult(String data1, String data2);
	}

}

在其他界面的调用如下


ArrayList listData1=new ArrayList();
		listData1.add("蔬菜");
		listData1.add("水果");
		listData1.add("肉");
		ArrayList> AlllistData2=new ArrayList>();
		ArrayList listData2=new ArrayList();
		listData2.add("空心菜");
		listData2.add("菜花");
		listData2.add("大白菜");
		AlllistData2.add(listData2);
		listData2=new ArrayList();
		listData2.add("苹果");
		listData2.add("芒果");
		AlllistData2.add(listData2);
		listData2=new ArrayList();
		listData2.add("鸡肉");
		listData2.add("牛肉");
		AlllistData2.add(listData2);
		dialog=new DoubleListDialog(
				getActivity(), 
				listData1, 
				AlllistData2, 
				new CallBack() {
					
					@Override
					public void clickResult(String data1,String data2) {
						
					}
				});
		dialog.show();


以下是效果图 

双listview实现两级选择的 dialog_第1张图片


以下是下载的地址

http://download.csdn.net/detail/shuang__zi/9624028

你可能感兴趣的:(android自定义控件)