public class DocumentTypePopwin
extends PopupWindow {
private String[]
typeAry;
private String[]
defaultAry = {
"身份证",
"军官照",
"护照",
"港澳通行证",
"居名户口本",
"其他证件"};
private ListView
lv;
private DocumentTypeAdapter
adapter;
private IListViewItemClickListener
itemClickListener;
private int
index,
num = -
1;
public DocumentTypePopwin(
final Context mContext) {
adapter =
new DocumentTypeAdapter(mContext); View view = View.
inflate(mContext, R.layout.
base_pop,
null); view.findViewById(R.id.
cancel_tv).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) { dismiss(); } }); TextView okTv = (TextView) view.findViewById(R.id.
ok_tv); okTv.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if(
index >=
0 ){
itemClickListener.onClick(
index,
typeAry[
index]); } dismiss(); } });
lv = (ListView) view.findViewById(R.id.
base_pop_lv);
lv.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position,
long id) {
index = position;
adapter.getCurrectItem(
index);
adapter.notifyDataSetChanged(); } });
this.setFocusable(
true);
//设置弹出窗体可点击
this.setOutsideTouchable(
true);
//外部可点击
view.setOnTouchListener(
new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int height = view.findViewById(R.id.
base_pop_lv).getHeight();
int y = (
int) motionEvent.getY();
if (motionEvent.getAction() == MotionEvent.
ACTION_UP) {
if (y < height) { dismiss(); } }
return true; } });
this.setContentView(view);
this.setHeight(RelativeLayout.LayoutParams.
WRAP_CONTENT);
this.setWidth(RelativeLayout.LayoutParams.
MATCH_PARENT);
this.setAnimationStyle(R.style.
morePopStyle); }
/**
* 显示popWindow中的数据,并回调得到当前选择的数据
*
*
@param
view
显示位置参照的view
*
@param
ary
需显示的数据数组
*
@param
clickListener
单击事件回调
*/
public void showPop(View view, String[] ary, IListViewItemClickListener clickListener) {
num = -
1;
if (clickListener ==
null) {
return; }
if (ary ==
null) { ary =
defaultAry; }
this.
itemClickListener = clickListener;
this.
typeAry = ary;
lv.setAdapter(
adapter);
adapter.notifyDataSetChanged();
this.showAtLocation(view, Gravity.
END,
0,
0); }
内部adapter实现
private class DocumentTypeAdapter
extends BaseAdapter { LayoutInflater
inflater; DocumentTypeAdapter(Context mContext) {
inflater = LayoutInflater.
from(mContext); }
void getCurrectItem(
int index) {
num = index; }
@Override
public int getCount() {
return
typeAry.
length; }
@Override
public Object getItem(
int position) {
return
typeAry[position]; }
@Override
public long getItemId(
int position) {
return position; }
@Override
public View getView(
int position, View convertView, ViewGroup parent) { DocumentTypeHolder holder;
if (convertView ==
null) { holder =
new DocumentTypeHolder(); convertView =
inflater.inflate(R.layout.
pop_document_type_item,
null); holder.
itemTv = (TextView) convertView.findViewById(R.id.
type_item_tv); convertView.setTag(holder); }
else { holder = (DocumentTypeHolder) convertView.getTag(); }
if (
num == position) { holder.
itemTv.setBackgroundResource(R.color.
themeColor); }
else { holder.
itemTv.setBackgroundResource(R.color.
white); } holder.
itemTv.setText(
typeAry[position]);
return convertView; } }
private class DocumentTypeHolder { TextView
itemTv; } }
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="@dimen/twoHundred"> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/fifty" android:layout_above="@+id/base_pop_lv" android:layout_gravity="center" android:background="@color/colorBg" android:orientation="horizontal" android:padding="@dimen/sixteen"> <TextView android:id="@+id/cancel_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/cancel" android:textColor="@color/colorBlack" android:textSize="@dimen/sixteen_sp" /> <TextView android:id="@+id/ok_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:text="@string/ok" android:textColor="@color/themeColor" android:textSize="@dimen/sixteen_sp" /> </LinearLayout> <ListView android:id="@+id/base_pop_lv" android:background="@color/white" android:layout_width="match_parent" android:dividerHeight="@dimen/zero_point_three" android:divider="@color/colorShallowAsh" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout>