欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義PopupWindow簡單小例子

 更新時間:2016年11月25日 10:09:14   作者:mr_zdd  
這篇文章主要為大家詳細(xì)介紹了Android自定義PopupWindow簡單小例子,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近沒事做就寫了一下PopupWindow,希望對有些人有點幫助。

照常先看一下完成后的結(jié)果(界面比較難看就不要吐槽了)

點擊地理位置然后彈出的PopupWindow,數(shù)據(jù)我寫死了但是可以根據(jù)你們的需求自己改,或者通過網(wǎng)絡(luò)獲取數(shù)據(jù)。我是通過listView進(jìn)行展示的你們也可以改成表格布局,具體的實現(xiàn)代碼如下:

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);
    // 設(shè)置SelectPicPopupWindow彈出窗體的寬
    this.setWidth(LayoutParams.MATCH_PARENT);
    // 設(shè)置SelectPicPopupWindow彈出窗體的高
    this.setHeight(LayoutParams.WRAP_CONTENT);
    // 設(shè)置SelectPicPopupWindow彈出窗體可點擊
    this.setFocusable(true);
    this.setOutsideTouchable(true);
    // 刷新狀態(tài)
    this.update();
    // 實例化一個ColorDrawable顏色為半透明
    ColorDrawable dw = new ColorDrawable(0000000000);
    // 點back鍵和其他地方使其消失,設(shè)置了這個才能觸發(fā)OnDismisslistener ,設(shè)置其他控件變化等操作
    this.setBackgroundDrawable(dw);
    // 設(shè)置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("濟(jì)南");
    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();
    }
  }

}

接下來就是調(diào)用PopupWindow顯示了。actionPath:是你的組件也就是我的地理位置

myPopuWindow= new MyPopuWindow(getActivity());
myPopuWindow.showPopupWindow(actionPath);

好了大概的一個代碼就是這樣了希望對你們有用。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 多線程的實現(xiàn)方法總結(jié)

    Android 多線程的實現(xiàn)方法總結(jié)

    這篇文章主要介紹了Android 多線程的實現(xiàn)方法總結(jié)的相關(guān)資料,這里提供三種方法,幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • Android基礎(chǔ)之Activity生命周期

    Android基礎(chǔ)之Activity生命周期

    activity類是Android 應(yīng)用生命周期的重要部分。在系統(tǒng)中的Activity被一個Activity棧所管理。當(dāng)一個新的Activity啟動時,將被放置到棧頂,成為運(yùn)行中的Activity,前一個Activity保留在棧中,不再放到前臺,直到新的Activity退出為止。
    2016-05-05
  • android自定義View實現(xiàn)圓環(huán)顏色選擇器

    android自定義View實現(xiàn)圓環(huán)顏色選擇器

    這篇文章主要介紹了android自定義View實現(xiàn)圓環(huán)顏色選擇器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android3.0 ActionBar導(dǎo)航標(biāo)題欄使用解析

    Android3.0 ActionBar導(dǎo)航標(biāo)題欄使用解析

    這篇文章主要為大家詳細(xì)解析了Android3.0 ActionBar導(dǎo)航標(biāo)題欄的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android Studio時間選擇器的創(chuàng)建方法

    Android Studio時間選擇器的創(chuàng)建方法

    這篇文章主要為大家詳細(xì)介紹了Android Studio時間選擇器的創(chuàng)建方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 如何為RecyclerView添加分隔線

    如何為RecyclerView添加分隔線

    這篇文章主要為大家詳細(xì)介紹了如何為RecyclerView添加分隔線,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android編程四大組件之Activity用法實例分析

    Android編程四大組件之Activity用法實例分析

    這篇文章主要介紹了Android編程四大組件之Activity用法,實例分析了Activity的創(chuàng)建,生命周期,內(nèi)存管理及啟動模式等,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-01-01
  • android實現(xiàn)自動滾動的Gallary控件效果

    android實現(xiàn)自動滾動的Gallary控件效果

    這篇文章主要介紹了android實現(xiàn)自動滾動的Gallary控件效果,涉及Android中Gallary控件的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android Material Design 陰影實現(xiàn)示例

    Android Material Design 陰影實現(xiàn)示例

    這篇文章主要介紹了Android Material Design 陰影實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android中Textview超鏈接實現(xiàn)方式

    Android中Textview超鏈接實現(xiàn)方式

    TextView中的超鏈接可以通過幾種方式實現(xiàn):1.Html.fromHtml,2.Spannable,3.Linkify.addLinks。下面分別進(jìn)行測試,包括修改字體樣式,下劃線樣式,點擊事件等,需要的朋友可以參考下
    2016-02-02

最新評論