我写这样的文章都是为了快速迭代,所以都写的比较简单,谅解
此类支持任意Model对象,该对象需要重写toString()
简单的一个Item布局item_simple_list.xml
附件是需要的工具类,快速生成一个ListView的代码,可能需要Volley,如报错,可以注释掉不必要的代码,不影响本例。
使用:
KeyValue是个Model对象,重写了toString(),可以是任何对象
此类支持任意Model对象,该对象需要重写toString()
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 com.mb.bgfitting.CommonAdapter; import com.mb.bgfitting.ViewHolder; import com.mb.bgfitting.app.R; /** * * @author pythoner * */ public class SimpleListDialog<T> extends Dialog { private Context context; private ListView listView; private List<T> beans; private CommonAdapter<T> adapter; public SimpleListDialog(Context context,List<T> beans) { this(context, R.style.Theme_Dialog_NoTitle,beans); // TODO Auto-generated constructor stub } public SimpleListDialog(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_simple_list); initViews(); // initValues(); } private void initViews() { if(beans==null){ return; } listView=(ListView)findViewById(R.id.listView); listView.setAdapter(adapter = new CommonAdapter<T>( context, beans, R.layout.item_simple_list){ @Override public void setValues(ViewHolder helper, T item, int position) { // TODO Auto-generated method stub helper.setText(R.id.item_0, item.toString()); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { // TODO Auto-generated method stub T instance=beans.get(position); if(onItemClickListener!=null){ onItemClickListener.onItemClick(v, instance,position); } dismiss(); } }); } 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 OnItemClickListener<T> onItemClickListener; public interface OnItemClickListener<T> { public void onItemClick(View v,T item,int position); } public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) { this.onItemClickListener = onItemClickListener; } }
简单的一个Item布局item_simple_list.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/item_0" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="16dp" android:paddingBottom="16dp" android:gravity="center" android:textColor="@color/base_text_black" android:textSize="@dimen/font_xbig" android:singleLine="true" /> </RelativeLayout>
附件是需要的工具类,快速生成一个ListView的代码,可能需要Volley,如报错,可以注释掉不必要的代码,不影响本例。
使用:
List<KeyValue> list=new ArrayList<KeyValue>(); list.add(new KeyValue("0","继续挑选")); list.add(new KeyValue("1","立即购买")); list.add(new KeyValue("2","配货投递")); list.add(new KeyValue("3","立即试衣")); SimpleListDialog<KeyValue> d=new SimpleListDialog<KeyValue>(context, list); d.setOnItemClickListener(new SimpleListDialog.OnItemClickListener<KeyValue>() { @Override public void onItemClick(View v, KeyValue item, int position) { // TODO Auto-generated method stub } }); d.show();
KeyValue是个Model对象,重写了toString(),可以是任何对象