点击地理位置然后弹出的PopupWindow,数据我写死了但是可以根据你们的需求自己改,或者通过网络获取数据。我是通过listView进行展示的你们也可以改成表格布局,具体的实现代码如下:
PopupWindow的弹出框的整体布局(listView)fragment_popup:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/pop_path"android:layout_width="match_parent"android:layout_height="wrap_content"></ListView></LinearLayout>listview要加载的item:pop_list_adapter.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/item_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18dp"/></LinearLayout>listview的适配器:PopAdapter
public class PopAdapter extends BaseAdapter {private List<String> list;private Context context;public PopAdapter(List<String> list, Context context) {this.list = list;this.context = context;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView == null) {viewHolder = new ViewHolder();convertView = LayoutInflater.from(context).inflate(R.layout.pop_list_adapter, null);viewHolder.item_content = (TextView) convertView.findViewById(R.id.item_content);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}viewHolder.item_content.setText(list.get(position));return convertView;}private class ViewHolder {private TextView item_content;}}写一个MyPopupWindow类继承PopupWindow:
public class MyPopuWindow extends PopupWindow {private View contentView;private ListView lv_pop;private List<String> paths;private Context context;public MyPopuWindow(final Activity context) {this.context = context;//获得 LayoutInflater 的实例LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);contentView = inflater.inflate(R.layout.fragment_popup, null);//获取屏幕的宽高int h = context.getWindowManager().getDefaultDisplay().getHeight();int w = context.getWindowManager().getDefaultDisplay().getWidth();this.setContentView(contentView);// 设置SelectPicPopupWindow弹出窗体的宽this.setWidth(LayoutParams.MATCH_PARENT);// 设置SelectPicPopupWindow弹出窗体的高this.setHeight(LayoutParams.WRAP_CONTENT);// 设置SelectPicPopupWindow弹出窗体可点击this.setFocusable(true);this.setOutsideTouchable(true);// 刷新状态this.update();// 实例化一个ColorDrawable颜色为半透明ColorDrawable dw = new ColorDrawable(0000000000);// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作this.setBackgroundDrawable(dw);// 设置SelectPicPopupWindow弹出窗体动画效果this.setAnimationStyle(R.style.AnimationPreview);initData();}private void initData() {paths = new ArrayList<>();paths.add("北京");paths.add("上海");paths.add("广州");paths.add("天津");paths.add("大连");paths.add("长春");paths.add("济南");paths.add("青岛");paths.add("无锡");paths.add("郑州");paths.add("宁波");paths.add("厦门");lv_pop = (ListView) contentView.findViewById(R.id.pop_path);lv_pop.setAdapter(new PopAdapter(paths, context));lv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(context, paths.get(position), Toast.LENGTH_SHORT).show();showPopupWindow(view);}});}/** * 显示popupWindow * * @param parent */public void showPopupWindow(View parent) {if (!this.isShowing()) {// 以下拉方式显示popupwindowthis.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);} else {this.dismiss();}}}接下来就是调用PopupWindow显示了。actionPath:是你的组件也就是我的地理位置
myPopuWindow= new MyPopuWindow(getActivity());myPopuWindow.showPopupWindow(actionPath);好了大概的一个代码就是这样了希望对你们有用。