1.先上设计图
2.导入高德地图相应的搜索jar包
可以自行到高德官网去下载相应的jar包 so包,导入工程
3.主页布局
<?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"
android:orientation="vertical"
android:background="#f4f4f4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="@drawable/search_background"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:src="@drawable/home_icon_search" />
<EditText
android:id="@+id/et_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="请输入你的地址"
android:lines="1"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:textColor="#333333"
android:textColorHint="#AAAAAA"
android:textSize="14sp" />
<ImageView
android:id="@+id/iv_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:src="@drawable/search_icon_del"
android:visibility="gone" />
</LinearLayout>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="取消"
android:textColor="#ff5a5a5a"
android:textSize="14sp" />
</LinearLayout>
<com.xxx.library.widget.ExpendRecycleView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
</LinearLayout>
.ExpendRecycleView 是扩展的RecycleView 可直接用v7的RecycleView即可
4.主页代码
package com.xxx.ioammcpv2.view.regist.address
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.amap.api.services.help.Inputtips
import com.amap.api.services.help.InputtipsQuery
import com.chad.library.adapter.base.BaseQuickAdapter
import com.xxx.ioammcpv2.R
import com.xxx.ioammcpv2.adapter.LocalAdapter
import com.xxx.ioammcpv2.base.BaseActivity
import com.xxx.ioammcpv2.common.ConstantStr
import com.xxx.ioammcpv2.mode.user.bean.LocationBean
import kotlinx.android.synthetic.main.activity_search.*
class SearchActivity : BaseActivity() {
private val locaList = ArrayList<LocationBean>()
private lateinit var adapter: LocalAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setDarkStatusIcon(true)
setLayoutAndToolbar(R.layout.activity_search)
recycler_view.layoutManager = LinearLayoutManager(this)
adapter = LocalAdapter(this, R.layout.item_address_data, locaList)
recycler_view.adapter = adapter
adapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
val intent = Intent()
intent.putExtra(ConstantStr.KEY_BUNDLE_STR, locaList[position].title+locaList[position].address)
intent.putExtra(ConstantStr.KEY_BUNDLE_LONG,locaList[position].latitude)
intent.putExtra(ConstantStr.KEY_BUNDLE_LONG_1,locaList[position].longitude)
setResult(Activity.RESULT_OK, intent)
finish()
}
et_search.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
val text = s?.toString()
if (!TextUtils.isEmpty(text)) {
iv_del.visibility = View.VISIBLE
searchData(text!!)
} else {
iv_del.visibility = View.GONE
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
iv_del.setOnClickListener {
et_search.text = null
iv_del.visibility = View.GONE
}
tv_cancel.setOnClickListener {
try {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow((this as Activity)
.currentFocus!!.windowToken,
InputMethodManager.HIDE_NOT_ALWAYS)
} catch (e: Exception) {
}
finish()
}
showSoftInputFromWindow(this, et_search)
}
private fun searchData(text: String) {
val tempList = ArrayList<LocationBean>()
val inputtipsQuery = InputtipsQuery(text, "")//初始化一个输入提示搜索对象,并传入参数
val inputtips = Inputtips(this, inputtipsQuery)//定义一个输入提示对象,传入当前上下文和搜索对象
inputtips.setInputtipsListener { p0, p1 ->
if (p1 == 1000 && p0 != null && p0.isNotEmpty()) {
for (item in p0) {
if(item.point!=null){
val locationBean = LocationBean()
locationBean.latitude = item.point.latitude
locationBean.longitude = item.point.longitude
locationBean.snippet = item.name
locationBean.title = item.district
locationBean.address = item.address
tempList.add(locationBean)
}
}
locaList.clear()
locaList.addAll(tempList)
recycler_view.adapter.notifyDataSetChanged()
}
}
inputtips.requestInputtipsAsyn()//输入查询提示的异步接口实现
}
private fun showSoftInputFromWindow(activity: Activity, editText: EditText) {
editText.isFocusable = true
editText.isFocusableInTouchMode = true
editText.requestFocus()
activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}
}
列表的adapter以及item布局比较简单 这里就不再复制粘贴了 主要是searchData()方法 仅供参考