国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 高德地圖之poi搜索功能的實現代碼

2019-10-22 18:28:55
字體:
來源:轉載
供稿:網友

廢話不多說,先看效果,如果大家感覺不錯,請參考實現代碼

android,高德,poi搜索,高德地圖poi

這個功能我是用Fragmentdialog里面做的,也遇到不少坑

第一,就是設置背景的drawable為純白色導致鍵盤彈出的時候,recyclerview的布局被頂上去導致出現白色布局,有點扎眼;最后改成了設置為和背景色一個顏色就和好了

  Window window = getDialog().getWindow();    WindowManager.LayoutParams lp = window.getAttributes();    lp.gravity = Gravity.CENTER;    window.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.color_gray_f2)));    window.setAttributes(lp);

布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android/57606.html">android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  xmlns:tools="http://schemas.android.com/tools"  android:background="@color/color_gray_f2"  android:orientation="vertical">  <RelativeLayout    android:id="@+id/search_maps_bar"    android:layout_width="match_parent"    android:layout_height="50dp"    android:layout_centerHorizontal="true"    android:layout_marginLeft="15dp"    android:layout_marginRight="15dp"    android:layout_marginTop="10dp"    android:background="@drawable/new_card">    <ImageButton      android:id="@+id/dialog_search_back"      android:layout_width="50dp"      android:layout_height="match_parent"      android:layout_centerVertical="true"      android:layout_margin="2dp"      android:background="@drawable/button_background_selector"      android:src="@drawable/ic_qu_appbar_back"/>    <ImageButton      android:id="@+id/dialog_serach_btn_search"      android:layout_width="50dp"      android:layout_height="match_parent"      android:layout_alignParentRight="true"      android:layout_centerVertical="true"      android:layout_margin="2dp"      android:background="@drawable/button_background_selector"      android:src="@drawable/ic_qu_search"      tools:ignore="ContentDescription,RtlHardcoded"/>    <EditText      android:id="@+id/dialog_search_et"      android:layout_width="wrap_content"      android:layout_height="match_parent"      android:layout_centerInParent="true"      android:layout_marginLeft="5.0dip"      android:layout_marginRight="5.0dip"      android:layout_toLeftOf="@+id/dialog_serach_btn_search"      android:layout_toRightOf="@+id/dialog_search_back"      android:background="@android:color/transparent"      android:completionThreshold="1"      android:dropDownVerticalOffset="1.0dip"      android:hint="請輸入關鍵字"      android:imeOptions="actionSearch|flagNoExtractUi"      android:inputType="text|textAutoComplete"      android:maxHeight="50dp"      android:maxLength="20"      android:minHeight="50dp"      android:singleLine="true"      android:textColor="#000000"      android:textSize="16.0sp"/>  </RelativeLayout>  <android.support.v7.widget.RecyclerView    android:id="@+id/dialog_search_recyclerview"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginLeft="15dp"    android:layout_marginRight="15dp"    android:layout_marginTop="@dimen/dp_10" /></LinearLayout>

第二個問題是鍵盤彈出的時候,會出現dialog布局整體被頂上去

最后通過設置 style來解決

  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //解決dialogfragment布局不被頂上去的方法    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);  }

最后就是實現搜索功能了

第一個點擊搜索時,鍵盤和搜索按鈕兩個都是同樣的效果

/**   * 搜索功能   */  private void searchLocationPoi() {    //關閉鍵盤    KeyBoardUtils.closeKeybord(poiSearchInMaps, BaseApplication.mContext);    if (TextUtils.isEmpty(poiSearchInMaps.getText().toString().trim())) {      ToastUtils.showToastCenter("內容為空!");    } else {      query = new PoiSearch.Query(poiSearchInMaps.getText().toString().trim(), "", "");// 第一個參數表示搜索字符串,第二個參數表示poi搜索類型,第三個參數表示poi搜索區域(空字符串代表全國)      query.setPageSize(20);// 設置每頁最多返回多少條poiitem      query.setPageNum(0);// 設置查第一頁      poiSearch = new PoiSearch(getActivity(), query);      poiSearch.setOnPoiSearchListener(this);      poiSearch.searchPOIAsyn();    }  }

然后回調中進行處理

@Override  public void onPoiSearched(PoiResult poiResult, int errcode) {    Logger.e(poiResult.getPois().toString() + "" + errcode);    if (errcode == 1000) {      datas = new ArrayList<>();      ArrayList<PoiItem> pois = poiResult.getPois();      for (int i = 0; i < pois.size(); i++) {        LocationBean locationBean = new LocationBean();        locationBean.title = pois.get(i).getTitle();        locationBean.snippet = pois.get(i).getSnippet();        datas.add(locationBean);      }      searchCarAdapter.setNewData(datas);    }  }

    還有就是監聽EditText里面內容的變化來搜索,其實也很簡單

 poiSearchInMaps.addTextChangedListener(new TextWatcher() {      @Override      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {      }      @Override      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {        textChangeSearch(charSequence);      }      @Override      public void afterTextChanged(Editable editable) {      }    });  /**   * 監聽edittext內容的變化,去搜索   */  private void textChangeSearch(CharSequence charSequence) {    String content = charSequence.toString().trim();//獲取自動提示輸入框的內容    Logger.e(content);    InputtipsQuery inputtipsQuery = new InputtipsQuery(content, "");//初始化一個輸入提示搜索對象,并傳入參數    Inputtips inputtips = new Inputtips(getActivity(), inputtipsQuery);//定義一個輸入提示對象,傳入當前上下文和搜索對象    inputtips.setInputtipsListener(new Inputtips.InputtipsListener() {      @Override      public void onGetInputtips(List<Tip> list, int errcode) {        Logger.e(list.toString() + errcode);        if (errcode == 1000 && list != null) {          datas = new ArrayList<>();          for (int i = 0; i < list.size(); i++) {            LocationBean locationBean = new LocationBean();            Tip tip = list.get(i);            locationBean.latitude = tip.getPoint().getLatitude();            locationBean.longitude = tip.getPoint().getLongitude();            locationBean.snippet = tip.getName();            locationBean.title = tip.getDistrict();            datas.add(locationBean);          }          searchCarAdapter.setNewData(datas);        }      }    });//設置輸入提示查詢的監聽,實現輸入提示的監聽方法onGetInputtips()    inputtips.requestInputtipsAsyn();//輸入查詢提示的異步接口實現  }

ok,搞定,最后只需要搞個回調,把Search后點擊的item傳回去就好了.希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 茂名市| 宁海县| 白城市| 磐安县| 舒城县| 大石桥市| 杂多县| 曲周县| 高阳县| 东山县| 康乐县| 崇信县| 行唐县| 巨鹿县| 平和县| 兴安县| 福鼎市| 色达县| 罗定市| 汨罗市| 嘉禾县| 鸡西市| 剑河县| 霍林郭勒市| 临洮县| 兴和县| 开封县| 岳阳市| 铅山县| 丹江口市| 留坝县| 隆安县| 伊春市| 营山县| 应城市| 安泽县| 会同县| 壶关县| 保亭| 娄烦县| 玛多县|