亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android自定義PopupWindow簡(jiǎn)單小例子

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

最近沒(méi)事做就寫(xiě)了一下PopupWindow,希望對(duì)有些人有點(diǎn)幫助。

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

點(diǎn)擊地理位置然后彈出的PopupWindow,數(shù)據(jù)我寫(xiě)死了但是可以根據(jù)你們的需求自己改,或者通過(guò)網(wǎng)絡(luò)獲取數(shù)據(jù)。我是通過(guò)listView進(jìn)行展示的你們也可以改成表格布局,具體的實(shí)現(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;
  }
}

寫(xiě)一個(gè)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 的實(shí)例
    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彈出窗體可點(diǎn)擊
    this.setFocusable(true);
    this.setOutsideTouchable(true);
    // 刷新?tīng)顟B(tài)
    this.update();
    // 實(shí)例化一個(gè)ColorDrawable顏色為半透明
    ColorDrawable dw = new ColorDrawable(0000000000);
    // 點(diǎn)back鍵和其他地方使其消失,設(shè)置了這個(gè)才能觸發(fā)OnDismisslistener ,設(shè)置其他控件變化等操作
    this.setBackgroundDrawable(dw);
    // 設(shè)置SelectPicPopupWindow彈出窗體動(dòng)畫(huà)效果
    this.setAnimationStyle(R.style.AnimationPreview);
    initData();
  }

  private void initData() {
    paths = new ArrayList<>();
    paths.add("北京");
    paths.add("上海");
    paths.add("廣州");
    paths.add("天津");
    paths.add("大連");
    paths.add("長(zhǎng)春");
    paths.add("濟(jì)南");
    paths.add("青島");
    paths.add("無(wú)錫");
    paths.add("鄭州");
    paths.add("寧波");
    paths.add("廈門(mén)");
    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();
    }
  }

}

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

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

好了大概的一個(gè)代碼就是這樣了希望對(duì)你們有用。

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

相關(guān)文章

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

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

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

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

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

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

    這篇文章主要介紹了android自定義View實(shí)現(xiàn)圓環(huán)顏色選擇器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    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)題欄的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android Studio時(shí)間選擇器的創(chuàng)建方法

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

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

    如何為RecyclerView添加分隔線

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

    Android編程四大組件之Activity用法實(shí)例分析

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

    android實(shí)現(xiàn)自動(dòng)滾動(dòng)的Gallary控件效果

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

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

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

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

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

最新評(píng)論