RecyclerView
关于RecyclerView和ListView,不多说什么,简单RecyclerView用法,性能更好但是相对稍微复杂一丢丢:
- layout.xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/exp_data_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- Adapter, with simple callback of click on a item
class SimpleListAdapter(private var listData: Array<String>) : RecyclerView.Adapter<SimpleListAdapter.Companion.ViewHolder>() {
private var clickCallback: ((String) -> Unit)? = null
fun setClickCallback(callback: (String) -> Unit): SimpleListAdapter {
clickCallback = callback
return this
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.simple_list_item, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = listData[position]
holder.textView?.text = item
holder.textView?.setOnClickListener { clickCallback?.invoke(item) }
}
override fun getItemCount(): Int {
return listData.size
}
companion object {
class ViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView) {
val textView: TextView? = rootView.findViewById(R.id.item_text)
}
}
}
- Use it! In Activity or else.
val data = arrayOf("hello", "monday", "happy", "yes", "sounds", "loving", "holiday", "good", "cuty", "wonderful", "andrew", "bell", "custom")
val dataRecyclerView = findViewById(R.id.exp_data_list)
val adapter = SimpleListAdapter(data).setClickCallback { result ->
Toast.makeText(this, "Click page on $result", Toast.LENGTH_SHORT).show()
}
dataRecyclerView?.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
dataRecyclerView?.adapter = adapter
That's it.
SearchView
Usage
- layout.xml, two elements, one for search box one for search result
<androidx.appcompat.widget.SearchView
android:id="@+id/exp_searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textFilter" />
<TextView
android:id="@+id/exp_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/design_default_color_primary"
android:padding="10dp"
android:text="hello"
android:visibility="gone" />
- use it, with SearchAutoComplete
val data = arrayOf("hello", "monday", "happy", "yes", "sounds", "loving", "holiday", "good", "cuty", "wonderful", "andrew", "bell", "custom")
val searchView = findViewById(R.id.exp_searchView)
val searchResultView = findViewById(R.id.exp_content)
val searchAutoComplete = searchView?.findViewById(androidx.appcompat.R.id.search_src_text) as SearchView.SearchAutoComplete
val simpleAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
searchAutoComplete.setAdapter(simpleAdapter)
searchAutoComplete.threshold = 1
searchAutoComplete.setOnItemClickListener { parent, view, position, id ->
val result = simpleAdapter.getItem(position)
Toast.makeText(this, "Click on $result", Toast.LENGTH_SHORT).show()
searchResultView?.visibility = View.VISIBLE
searchResultView?.text = result
}
效果就是,点击搜索按钮,进入搜索页面,有输入就会开始匹配,输入框下会显示列表展示可选的项,点击可选项后会触发callback,弹一个toast同时一个textview展示结果。