最近沒事做就寫了一下PopupWindow,希望對有些人有點協助。
照常先看一下完成後的結果(介面比較難看就不要吐槽了)
點擊地理位置然後彈出的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"> <ListView android: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"> <TextView android: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; } @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 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() { @Override public 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()) { // 以下拉方式顯示popupwindow this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18); } else { this.dismiss(); } }}
接下來就是調用PopupWindow顯示了。actionPath:是你的組件也就是我的地理位置
myPopuWindow= new MyPopuWindow(getActivity());myPopuWindow.showPopupWindow(actionPath);
好了大概的一個代碼就是這樣了希望對你們有用。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。