这次使用Pair<F,S>实现一个简单的SimplePairListPopupWindow,还是为了快速迭代,谅解
用法:
import java.util.ArrayList; import android.content.Context; import android.util.Pair; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout.LayoutParams; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import com.mb.bgfitting.App; import com.mb.bgfitting.CommonAdapter; import com.mb.bgfitting.ViewHolder; import com.mb.bgfitting.app.R; /** * * @author pythoner * */ public class SimplePairListPopupWindow<F,S> extends PopupWindow { private Context context; private CommonAdapter<Pair<F,S>> adapter; private ArrayList<Pair<F,S>> beans; public SimplePairListPopupWindow(Context context, View view, int width,ArrayList<Pair<F,S>> b) { super(view, width, LayoutParams.WRAP_CONTENT, true); this.context = context; this.beans = b; this.setBackgroundDrawable(App.res.getDrawable(R.drawable.bg_popupwindow)); this.setOutsideTouchable(true); this.setAnimationStyle(android.R.style.Animation_Dialog); // this.update(); // this.setTouchable(true); // this.setFocusable(false); ListView listView = (ListView) view.findViewById(R.id.listView); listView.setAdapter(adapter = new CommonAdapter<Pair<F,S>>(context, beans,R.layout.item_for_popupwindow_simple) { @Override public void setValues(ViewHolder helper, Pair<F,S> item, int position) { helper.setText(R.id.item_0, item.second.toString()); } }); listView.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> pearnt, View view, int position, long id) { // TODO Auto-generated method stub dismiss(); if (onItemClickListener != null) { onItemClickListener.onItemClick(position,beans.get(position)); } } }); } public interface OnItemClickListener<F,S> { void onItemClick(int position, Pair<F,S> pair); } OnItemClickListener<F,S> onItemClickListener; public void setOnItemClickListener(OnItemClickListener<F,S> onItemClickListener) { this.onItemClickListener = onItemClickListener; } }
用法:
private void showSimplePopupWindow(View v){ ArrayList<Pair<Integer, String>> beans = new ArrayList<Pair<Integer, String>>(); beans.add(new Pair<Integer, String>(0, "当天未完成")); beans.add(new Pair<Integer, String>(1, "当天全部")); beans.add(new Pair<Integer, String>(2, "当天自己")); beans.add(new Pair<Integer, String>(3, "历史查询")); beans.add(new Pair<Integer, String>(4, "交易查询")); View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_simple, null); final SimplePairListPopupWindow<Integer, String> popupWindow = new SimplePairListPopupWindow<Integer, String>(context,view, (int)App.res.getDimension(R.dimen.popupwindow_width), beans); popupWindow.showAsDropDown(v, 0, 0); popupWindow.setOnItemClickListener(new SimplePairListPopupWindow.OnItemClickListener<Integer, String>() { @Override public void onItemClick(int position, Pair<Integer, String> pair) { // TODO Auto-generated method stub } }); }