实现了自定义的dialog中布局listview
1、在com.sl.example.control包下创建listDialog类,代码如下:
public class listDialog extends Dialog {
int layoutRes;// 布局文件
Context context;
public listDialog(Context context) {
super(context);
this.context = context;
}
/**
* * 自定义主题及布局的构造方法 * @param context * @param theme * @param resLayout
*/
public listDialog(Context context, int theme) {
super(context, theme);
}
// Builder 方法创建dialog控件
public static class Builder {
private Context context;
// Dialog 标题名称
private String title;
// Dialog 消息内容
private String message;
// listview中的数据
private List<Map<String, String>> list;
// 控件listview
private ListView lv;
// positiveButton Button的文字
private String positiveButtonText;
// lisetview的onitemclick事件
private DialogInterface.OnClickListener listClickListener;
// positiveButton 单击事件
private DialogInterface.OnClickListener positiveButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* Set the Dialog message from String
*
* @param title
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
*
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
*
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the lisetview datas and it's onitemclicklistener
*
* @param List<Map<String, String>> data
* @param listener
* @return
*/
public Builder setList(List<Map<String, String>> l,
DialogInterface.OnClickListener listener) {
this.list = l;
this.listClickListener = listener;
return this;
}
/**
* Create the custom dialog
* lisetview 的样式文件为 :res/layout/ctl_list_dialog.xml
* R.style.customDialog在: res/values/syles.xml 文件中
* listdialogAdapter 应在com.sl.example.adapter包中,此设计写在了本文件中
*/
public listDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
// R.style.customDialog在 res/values/syles.xml 文件中
final listDialog dialog = new listDialog(context,
R.style.customDialog);
View layout = inflater.inflate(R.layout.ctl_list_dialog, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.ctl_dialog_title))
.setText(title);
lv = (ListView) layout.findViewById(R.id.ctl_dialog_lv);
lv.setAdapter(new listdialogAdapter());
// set list
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView v = (TextView) arg1;
//选中item 文字变色,背景变色,并触发自定义listClick事件
v.setTextColor(context.getResources().getColor(
R.color.white));
v.setBackgroundColor(context.getResources().getColor(
R.color.app_default));
listClickListener.onClick(dialog, arg2);
}
});
dialog.setContentView(layout);
return dialog;
}
private class listdialogAdapter extends BaseAdapter {
public listdialogAdapter() {
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
TextView t = new TextView(context);
t.setText(list.get(arg0).get("text"));
t.setPadding(100, 20, 10, 20);
t.setTextSize(14);
t.setTextColor(context.getResources().getColor(R.color.gray));
t.setGravity(Gravity.CENTER_VERTICAL);
return t;
}
}
}
}
2、listDialog布局文件:res/layout/ctl_list_dialog.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:background="@color/white"
android:minWidth="300dp"
android:minHeight="500dp"
android:orientation="vertical" >
<TextView
android:id="@+id/ctl_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/selector_btn_default"
android:padding="15dp"
android:text="请选择城市"
android:textColor="@color/gray"
android:textSize="14sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/app_default" />
<ListView
android:id="@+id/ctl_dialog_lv"
android:layout_width="fill_parent"
android:listSelector="@android:color/transparent"
android:cacheColorHint="#00000000"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
3、listDialog样式设计:res/values/syles.xml 文件中
<style name="customDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
4、activity页面调用listDialog控件
private Dialog dialog;
private List<Map<String, String>> province;//获取数据
province = helper_city.getProvince(MainActivity.this);
listDialog.Builder dialogBuilder = new listDialog.Builder(
MainActivity.this).setTitle("城市选择").setList(province,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
//((TextView) findViewById(R.id.main_tv_1)).setText(province.get(arg1).get("text"));
//reloadCitys(String.valueOf(arg1 + 1));
dialog.dismiss();
}
});
dialog = dialogBuilder.create();
dialog.show();