activiti_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
data_list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
res-menu menu_main
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:title="search"/>
</menu>
MainActivity.kt
class MainActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private val dataList = mutableListOf<String>()
private val filteredList = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
addData()
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = DataAdapter(dataList)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
val searchView = menu.findItem(R.id.action_search).actionView as SearchView
searchView.queryHint = getString(R.string.search_view_hint)
searchView.setOnQueryTextListener(this)
return super.onCreateOptionsMenu(menu)
}
override fun onQueryTextChange(entry: String): Boolean {
filteredList.clear()
(0 until dataList.size)
.filter { dataList[it].toLowerCase().contains(entry.toLowerCase()) }
.mapTo(filteredList) { dataList[it] }
recycler_view.adapter = DataAdapter(filteredList)
return true
}
override fun onQueryTextSubmit(query: String?): Boolean {
return true
}
private fun addData() {
dataList.add("100")
dataList.add("200")
dataList.add("300")
dataList.add("400")
dataList.add("500")
dataList.add("600")
dataList.add("700")
dataList.add("800")
}
}
DataAdapter
lass DataAdapter(private var items : MutableList<String>) :
RecyclerView.Adapter<DataAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: ViewHolder, index: Int) {
holder.item.text = items[index]
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.data_list_item, parent, false))
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var item: TextView = itemView.tv_item
}
build.gradle(app)
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'