最近没事做就写了一下PopupWindow,希望对有些人有点帮助。
照常先看一下完成后的结果(界面比较难看就不要吐槽了)
点击地理位置然后弹出的PopupWindow,数据我写死了但是可以根据你们的需求自己改,或者通过网络获取数据。我是通过listView进行展示的你们也可以改成表格布局,具体的实现代码如下:
PopupWindow的弹出框的整体布局(listView)fragment_popup:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
listview要加载的item:pop_list_adapter.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
listview的适配器:PopAdapter
public class PopAdapter extends BaseAdapter { private Listlist; private Context context; public PopAdapter(List list, Context context) { this.list = list; this.cOntext= context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public 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 Listpaths; 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() { @Override public void onItemClick(AdapterView<&#63;> 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()) { // 以下拉方式显示popupwindow this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18); } else { this.dismiss(); } } }
接下来就是调用PopupWindow显示了。actionPath:是你的组件也就是我的地理位置
myPopuWindow= new MyPopuWindow(getActivity()); myPopuWindow.showPopupWindow(actionPath);
好了大概的一个代码就是这样了希望对你们有用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。