最终的效果图,点击+86弹出popup
PupupWindow的布局文件
为一个ListView 作为pupup的主体内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/register_lv_zone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#30DDDDDD"
android:dividerHeight="1dp"
android:scrollbars="none"
android:scrollingCache="false"
/>
</LinearLayout>
ListView的item布局文件
作为每一个选项
<?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"
android:background="@android:color/white"
android:id="@+id/zone_item_parent"
>
<TextView
android:id="@+id/spinner_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textColor="#000"
/>
<TextView
android:id="@+id/spinner_zone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:textColor="#000"
/>
</RelativeLayout>
初始化Pupup
// PopupWindow 的页面布局 包含一个listview
View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_my_spinner, null);
mRegister_lv_zone = (ListView) view.findViewById(R.id.register_lv_zone);
// 初始化 popupW
mPopupWindow = new PopupWindow(RegisterActivity.this);
// 设置高度为 自适应
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setWidth(ScreenUtils.getScreenW(RegisterActivity.this) - UIUtils.getDimen(R.dimen.register_edittext_margin_left) * 2);
// 将view添加到 popup里面
mPopupWindow.setContentView(view);
mPopupWindow.setBackgroundDrawable(new PaintDrawable());
mPopupWindow.setFocusable(true);
设置消失Dismiss的监听
// popupWindow消失的监听
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// 处理你要做的事情
}
});
开启popupWindow
mRegisterZone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置在那个控件下面显示 mPopupWindow
mPopupWindow.showAsDropDown(mRegisterZone);
}
});
为ListView设置适配器
MySpinnerArrayAdapter mySpinnerArrayAdapter = new MySpinnerArrayAdapter();
mRegister_lv_zone.setAdapter(mySpinnerArrayAdapter);
// 设配器
class MySpinnerArrayAdapter extends BaseAdapter {
@Override
public int getCount() {
return mSpinnerZone.length;
}
@Override
public Object getItem(int position) {
return mSpinnerZone[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
convertView = inflater.inflate(R.layout.item_spinner, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mSpinnerAddress.setText(mSpinnerAddress[position]);
viewHolder.mSpinnerZone.setText(mSpinnerZone[position]);
return convertView;
}
class ViewHolder {
TextView mSpinnerAddress;
TextView mSpinnerZone;
private final RelativeLayout mZone_item_parent;
ViewHolder(View view) {
mSpinnerAddress = (TextView) view.findViewById(R.id.spinner_address);
mSpinnerZone = (TextView) view.findViewById(R.id.spinner_zone);
mZone_item_parent = (RelativeLayout) view.findViewById(R.id.zone_item_parent);
}
}
}
可以给ListView设置item点击监听隐藏popup
mRegister_lv_zone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mPopupWindow.dismiss();
}
});