add basic search to media view, related to #48

This commit is contained in:
tibbi 2018-01-07 17:48:25 +01:00
parent 4b2d41ec75
commit 38ca5b84d7
4 changed files with 64 additions and 5 deletions

View file

@ -55,7 +55,17 @@
<activity
android:name=".activities.MediaActivity"
android:parentActivityName=".activities.MainActivity"/>
android:parentActivityName=".activities.MainActivity">
<meta-data
android:name="android.app.default_searchable"
android:resource="@xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
</activity>
<activity
android:name=".activities.ViewPagerActivity"

View file

@ -1,13 +1,17 @@
package com.simplemobiletools.gallery.activities
import android.app.Activity
import android.app.SearchManager
import android.app.WallpaperManager
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.support.v4.view.MenuItemCompat
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.SearchView
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
@ -60,6 +64,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private var mLastMediaHandler = Handler()
private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mZoomListener: MyRecyclerView.MyZoomListener? = null
private var mSearchMenuItem: MenuItem? = null
companion object {
var mMedia = ArrayList<Medium>()
@ -130,6 +135,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
}
}
override fun onStop() {
super.onStop()
MenuItemCompat.collapseActionView(mSearchMenuItem)
}
override fun onDestroy() {
super.onDestroy()
if (config.showAll)
@ -150,6 +160,33 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
}
}
private fun setupSearch(menu: Menu) {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
mSearchMenuItem = menu.findItem(R.id.search)
(mSearchMenuItem!!.actionView as SearchView).apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
isSubmitButtonEnabled = false
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String) = false
override fun onQueryTextChange(newText: String): Boolean {
searchQueryChanged(newText)
return true
}
})
}
}
private fun searchQueryChanged(text: String) {
Thread {
val filtered = mMedia.filter { it.name.contains(text, true) } as ArrayList
filtered.sortBy { !it.name.startsWith(text, true) }
runOnUiThread {
(media_grid.adapter as? MediaAdapter)?.updateMedia(filtered)
}
}.start()
}
private fun tryloadGallery() {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
@ -260,6 +297,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
findItem(R.id.toggle_filename).isVisible = config.viewTypeFiles == VIEW_TYPE_GRID
}
setupSearch(menu)
return true
}

View file

@ -2,15 +2,21 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/sort"
android:icon="@drawable/ic_sort"
android:title="@string/sort_by"
app:showAsAction="ifRoom"/>
android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="@+id/toggle_filename"
android:icon="@drawable/ic_label"
android:title="@string/toggle_filename"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/sort"
android:icon="@drawable/ic_sort"
android:title="@string/sort_by"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/filter"
android:icon="@drawable/ic_filter"

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search"
android:label="@string/app_name"/>