Skip to content

Commit

Permalink
✨ Added search menu
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Jan 27, 2020
1 parent 9802aac commit 100401a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 13 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
android:name=".SongListActivity"
android:label="@string/title_song_list"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".SongDetailActivity"
Expand Down
102 changes: 90 additions & 12 deletions app/src/main/java/com/kyhsgeekcode/dereinfo/SongListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package com.kyhsgeekcode.dereinfo

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import android.util.Log
import android.view.*
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import com.kyhsgeekcode.dereinfo.model.CircleType.getColor
import com.kyhsgeekcode.dereinfo.model.CircleType.makeRGB
import com.kyhsgeekcode.dereinfo.model.DereDatabaseHelper
import com.kyhsgeekcode.dereinfo.model.MusicInfo

import com.tingyik90.snackprogressbar.SnackProgressBar
import com.tingyik90.snackprogressbar.SnackProgressBarManager
import kotlinx.android.synthetic.main.activity_song_list.*
Expand All @@ -24,6 +21,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
* An activity representing a list of Pings. This activity
* has different presentations for handset and tablet-size devices. On
Expand All @@ -33,6 +31,7 @@ import kotlinx.coroutines.launch
* item details side-by-side using two vertical panes.
*/
class SongListActivity : AppCompatActivity() {
val TAG = "SongListActivity"
private val snackProgressBarManager by lazy {
SnackProgressBarManager(
mainListLayout,
Expand All @@ -44,6 +43,7 @@ class SongListActivity : AppCompatActivity() {
.setIsIndeterminate(false)
.setAllowUserInput(true)
private lateinit var dereDatabaseHelper: DereDatabaseHelper
private lateinit var adapter: SongRecyclerViewAdapter
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
Expand All @@ -70,7 +70,7 @@ class SongListActivity : AppCompatActivity() {
// activity should be in two-pane mode.
twoPane = true
}
val adapter = setupRecyclerView(song_list)
adapter = setupRecyclerView(song_list)
snackProgressBarManager.show(circularType, SnackProgressBarManager.LENGTH_INDEFINITE)
CoroutineScope(Dispatchers.IO).launch {
dereDatabaseHelper = DereDatabaseHelper(this@SongListActivity)
Expand All @@ -94,20 +94,52 @@ class SongListActivity : AppCompatActivity() {
snackProgressBarManager.disable()
}

override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main_menu, menu)
val search_item = menu.findItem(R.id.app_bar_search)
val searchView: SearchView = search_item.actionView as SearchView
searchView.isFocusable = false
searchView.queryHint = "Search"
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(s: String): Boolean {
adapter.filter?.filter(s)
return false
}

override fun onQueryTextChange(s: String?): Boolean {
adapter.filter?.filter(s)
return false
}
})
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id: Int = item.getItemId()
return super.onOptionsItemSelected(item)
}


private fun setupRecyclerView(recyclerView: RecyclerView): SongRecyclerViewAdapter {
val adapter = SongRecyclerViewAdapter(this, twoPane)
recyclerView.adapter = adapter
return adapter
}

class SongRecyclerViewAdapter(
private val parentActivity: SongListActivity,
private
val parentActivity: SongListActivity,
private val twoPane: Boolean
) :
RecyclerView.Adapter<SongRecyclerViewAdapter.ViewHolder>() {
RecyclerView.Adapter<SongRecyclerViewAdapter.ViewHolder>(), Filterable {

private var listFilter: ListFilter? = null
private val onClickListener: View.OnClickListener
private val values: MutableList<MusicInfo> = ArrayList()
private var filteredItemList: MutableList<MusicInfo> = values

init {
onClickListener = View.OnClickListener { v ->
Expand Down Expand Up @@ -138,8 +170,8 @@ class SongListActivity : AppCompatActivity() {
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = values[position]
holder.idView.text = item.name.replace("\\n", " ")
val item = filteredItemList[position]
holder.idView.text = """${item.name}(${item.id})""".replace("\\n", " ")
holder.contentView.text = item.composer
holder.backgroundLayout.setBackgroundColor(makeRGB(getColor(item.circleType)))
with(holder.itemView) {
Expand All @@ -149,7 +181,7 @@ class SongListActivity : AppCompatActivity() {
}


override fun getItemCount() = values.size
override fun getItemCount() = filteredItemList.size

fun addItem(item: MusicInfo) {
values.add(item)
Expand All @@ -161,5 +193,51 @@ class SongListActivity : AppCompatActivity() {
val contentView: TextView = view.content
val backgroundLayout: LinearLayout = view.listitem_background
}

override fun getFilter(): Filter? {
if (listFilter == null) {
listFilter = ListFilter()
}
return listFilter
}

inner class ListFilter : Filter() {
val TAG="ListFilter"
override fun performFiltering(constraint: CharSequence?): FilterResults {
val TAG = "ListFilter"
Log.d(TAG, "Filter called$constraint")
val results = FilterResults()
if (constraint == null || constraint.isEmpty()) {
results.values = values
results.count = values.size
} else {
val itemList: ArrayList<MusicInfo> = ArrayList()
for (item in values) {
val name = """${item.name}(${item.id})"""
if (name.toUpperCase().contains(constraint.toString().toUpperCase())) {
itemList.add(item)
}
}
results.values = itemList
results.count = itemList.size
}
return results
}

override fun publishResults(
constraint: CharSequence,
results: FilterResults
) { // update listview by filtered data list.
filteredItemList = results.values as ArrayList<MusicInfo>
Log.d(TAG, """filtered:${filteredItemList.size}, original:${values.size}""")

// notify
if (results.count > 0) {
notifyDataSetChanged()
} else {
notifyDataSetChanged()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class MusicInfo(
) {
override fun toString(): String {
val lineSeparator = System.lineSeparator()
return StringBuilder("name:").append(name).append(lineSeparator)
return StringBuilder("id:").append(id).append(lineSeparator)
.append("name:").append(name).append(lineSeparator)
.append("bpm:").append(bpm).append(lineSeparator)
.append("composer:").append(composer).append(lineSeparator)
.append("lyricist:").append(lyricist).append(lineSeparator)
.append("offset:").append(soundOffset).append(lineSeparator)
.append("duration:").append(soundLength).append(lineSeparator)
.append("type:").append(CircleType.getDesc(circleType)).toString()
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_search_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="codes4.com.simplelistview.MainActivity">
<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<string name="tab_text_2">Tab 2</string>
<string name="title_song_list">Songs</string>
<string name="title_song_detail">Song Detail</string>
<string name="search_hint">Search hint</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/xml/searchable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"/>

0 comments on commit 100401a

Please sign in to comment.