最終的效果圖,點擊+86彈出popup
PupupWindow的布局文件
為一個ListView 作為pupup的主體內(nèi)容
<?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的監(jiān)聽
// popupWindow消失的監(jiān)聽
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點擊監(jiān)聽隱藏popup
mRegister_lv_zone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mPopupWindow.dismiss();
}
});