mirror of
https://github.com/jrpie/Launcher.git
synced 2025-02-23 22:41:27 +01:00
Create a working SearchView for the appslist
- Add a `filter` method to `AppsRecyclerView` - Add a `SearchView` to layout
This commit is contained in:
parent
56fb5a063d
commit
2b96b3140d
3 changed files with 69 additions and 12 deletions
|
@ -13,8 +13,10 @@ import android.widget.PopupMenu
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.finnmglas.launcher.*
|
import com.finnmglas.launcher.*
|
||||||
import com.finnmglas.launcher.libraries.*
|
import com.finnmglas.launcher.libraries.FontAwesome
|
||||||
import com.finnmglas.launcher.list.intendedChoosePause
|
import com.finnmglas.launcher.list.intendedChoosePause
|
||||||
|
import java.util.*
|
||||||
|
import kotlin.collections.ArrayList
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A [RecyclerView] (efficient scrollable list) containing all apps on the users device.
|
* A [RecyclerView] (efficient scrollable list) containing all apps on the users device.
|
||||||
|
@ -30,6 +32,7 @@ class AppsRecyclerAdapter(val activity: Activity,
|
||||||
RecyclerView.Adapter<AppsRecyclerAdapter.ViewHolder>() {
|
RecyclerView.Adapter<AppsRecyclerAdapter.ViewHolder>() {
|
||||||
|
|
||||||
private val appsList: MutableList<AppInfo>
|
private val appsList: MutableList<AppInfo>
|
||||||
|
private val appsListDisplayed: MutableList<AppInfo>
|
||||||
|
|
||||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
|
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
|
||||||
View.OnClickListener {
|
View.OnClickListener {
|
||||||
|
@ -40,7 +43,7 @@ class AppsRecyclerAdapter(val activity: Activity,
|
||||||
override fun onClick(v: View) {
|
override fun onClick(v: View) {
|
||||||
val pos = adapterPosition
|
val pos = adapterPosition
|
||||||
val context: Context = v.context
|
val context: Context = v.context
|
||||||
val appPackageName = appsList[pos].packageName.toString()
|
val appPackageName = appsListDisplayed[pos].packageName.toString()
|
||||||
|
|
||||||
when (intention){
|
when (intention){
|
||||||
"view" -> {
|
"view" -> {
|
||||||
|
@ -62,10 +65,10 @@ class AppsRecyclerAdapter(val activity: Activity,
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
|
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
|
||||||
val appLabel = appsList[i].label.toString()
|
val appLabel = appsListDisplayed[i].label.toString()
|
||||||
val appPackageName = appsList[i].packageName.toString()
|
val appPackageName = appsListDisplayed[i].packageName.toString()
|
||||||
val appIcon = appsList[i].icon
|
val appIcon = appsListDisplayed[i].icon
|
||||||
val isSystemApp = appsList[i].isSystemApp
|
val isSystemApp = appsListDisplayed[i].isSystemApp
|
||||||
|
|
||||||
viewHolder.textView.text = appLabel
|
viewHolder.textView.text = appLabel
|
||||||
viewHolder.img.setImageDrawable(appIcon)
|
viewHolder.img.setImageDrawable(appIcon)
|
||||||
|
@ -127,7 +130,7 @@ class AppsRecyclerAdapter(val activity: Activity,
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemCount(): Int { return appsList.size }
|
override fun getItemCount(): Int { return appsListDisplayed.size }
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
val inflater = LayoutInflater.from(parent.context)
|
val inflater = LayoutInflater.from(parent.context)
|
||||||
|
@ -136,8 +139,11 @@ class AppsRecyclerAdapter(val activity: Activity,
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val pm: PackageManager = activity.packageManager
|
|
||||||
appsList = ArrayList()
|
appsList = ArrayList()
|
||||||
|
appsListDisplayed = ArrayList()
|
||||||
|
|
||||||
|
val pm: PackageManager = activity.packageManager
|
||||||
|
|
||||||
val i = Intent(Intent.ACTION_MAIN, null)
|
val i = Intent(Intent.ACTION_MAIN, null)
|
||||||
i.addCategory(Intent.CATEGORY_LAUNCHER)
|
i.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||||
val allApps = pm.queryIntentActivities(i, 0)
|
val allApps = pm.queryIntentActivities(i, 0)
|
||||||
|
@ -149,5 +155,23 @@ class AppsRecyclerAdapter(val activity: Activity,
|
||||||
appsList.add(app)
|
appsList.add(app)
|
||||||
}
|
}
|
||||||
appsList.sortBy { it.label.toString() }
|
appsList.sortBy { it.label.toString() }
|
||||||
|
appsListDisplayed.addAll(appsList)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The function [filter] is used to search elements within this [RecyclerView].
|
||||||
|
*/
|
||||||
|
fun filter(text: String) {
|
||||||
|
appsListDisplayed.clear()
|
||||||
|
if (text.isEmpty()) {
|
||||||
|
appsListDisplayed.addAll(appsList)
|
||||||
|
} else {
|
||||||
|
for (item in appsList) {
|
||||||
|
if (item.label.toString().toLowerCase(Locale.ROOT).contains(text.toLowerCase(Locale.ROOT))) {
|
||||||
|
appsListDisplayed.add(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
package com.finnmglas.launcher.list.apps
|
package com.finnmglas.launcher.list.apps
|
||||||
|
|
||||||
|
import android.graphics.Color
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import android.widget.SearchView
|
||||||
|
import android.widget.TextView
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.finnmglas.launcher.R
|
import com.finnmglas.launcher.R
|
||||||
import com.finnmglas.launcher.UIObject
|
import com.finnmglas.launcher.UIObject
|
||||||
import com.finnmglas.launcher.list.intention
|
|
||||||
import com.finnmglas.launcher.list.forApp
|
|
||||||
import com.finnmglas.launcher.dominantColor
|
import com.finnmglas.launcher.dominantColor
|
||||||
import com.finnmglas.launcher.getSavedTheme
|
import com.finnmglas.launcher.list.forApp
|
||||||
|
import com.finnmglas.launcher.list.intention
|
||||||
import kotlinx.android.synthetic.main.list_apps.*
|
import kotlinx.android.synthetic.main.list_apps.*
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,17 +38,38 @@ class ListFragmentApps : Fragment(), UIObject {
|
||||||
|
|
||||||
override fun applyTheme() {
|
override fun applyTheme() {
|
||||||
list_apps_container.setBackgroundColor(dominantColor)
|
list_apps_container.setBackgroundColor(dominantColor)
|
||||||
|
|
||||||
|
val id: Int = list_apps_searchview.context.resources
|
||||||
|
.getIdentifier("android:id/search_src_text", null, null)
|
||||||
|
list_apps_searchview.findViewById<TextView>(id).setTextColor(Color.WHITE)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setOnClicks() { }
|
override fun setOnClicks() { }
|
||||||
|
|
||||||
override fun adjustLayout() {
|
override fun adjustLayout() {
|
||||||
|
|
||||||
|
val appsRViewAdapter = AppsRecyclerAdapter(activity!!, intention, forApp)
|
||||||
|
|
||||||
// set up the list / recycler
|
// set up the list / recycler
|
||||||
list_apps_rview.apply {
|
list_apps_rview.apply {
|
||||||
// improve performance (since content changes don't change the layout size)
|
// improve performance (since content changes don't change the layout size)
|
||||||
setHasFixedSize(true)
|
setHasFixedSize(true)
|
||||||
layoutManager = LinearLayoutManager(context)
|
layoutManager = LinearLayoutManager(context)
|
||||||
adapter = AppsRecyclerAdapter(activity!!, intention, forApp)
|
adapter = appsRViewAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list_apps_searchview.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
||||||
|
|
||||||
|
override fun onQueryTextSubmit(query: String): Boolean {
|
||||||
|
appsRViewAdapter.filter(query);
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onQueryTextChange(newText: String): Boolean {
|
||||||
|
appsRViewAdapter.filter(newText);
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:id="@+id/list_apps_container"
|
android:id="@+id/list_apps_container"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
@ -12,6 +13,7 @@
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:layout_marginStart="16dp"
|
android:layout_marginStart="16dp"
|
||||||
android:layout_marginLeft="16dp"
|
android:layout_marginLeft="16dp"
|
||||||
|
android:layout_marginTop="32dp"
|
||||||
android:layout_marginEnd="16dp"
|
android:layout_marginEnd="16dp"
|
||||||
android:layout_marginRight="16dp"
|
android:layout_marginRight="16dp"
|
||||||
android:scrollbars="vertical"
|
android:scrollbars="vertical"
|
||||||
|
@ -20,4 +22,12 @@
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<SearchView
|
||||||
|
android:id="@+id/list_apps_searchview"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Add table
Reference in a new issue