mirror of
https://github.com/Michatec/michas-droid.git
synced 2026-05-30 18:02:43 +02:00
Manually control SearchView focus
This commit is contained in:
@@ -42,10 +42,12 @@ import nya.kitsunyan.foxydroid.utility.extension.android.*
|
|||||||
import nya.kitsunyan.foxydroid.utility.extension.resources.*
|
import nya.kitsunyan.foxydroid.utility.extension.resources.*
|
||||||
import nya.kitsunyan.foxydroid.utility.extension.text.*
|
import nya.kitsunyan.foxydroid.utility.extension.text.*
|
||||||
import nya.kitsunyan.foxydroid.widget.EnumRecyclerAdapter
|
import nya.kitsunyan.foxydroid.widget.EnumRecyclerAdapter
|
||||||
|
import nya.kitsunyan.foxydroid.widget.FocusSearchView
|
||||||
import kotlin.math.*
|
import kotlin.math.*
|
||||||
|
|
||||||
class TabsFragment: ScreenFragment() {
|
class TabsFragment: ScreenFragment() {
|
||||||
companion object {
|
companion object {
|
||||||
|
private const val STATE_SEARCH_FOCUSED = "searchFocused"
|
||||||
private const val STATE_SEARCH_QUERY = "searchQuery"
|
private const val STATE_SEARCH_QUERY = "searchQuery"
|
||||||
private const val STATE_SHOW_CATEGORIES = "showCategories"
|
private const val STATE_SHOW_CATEGORIES = "showCategories"
|
||||||
private const val STATE_CATEGORIES = "categories"
|
private const val STATE_CATEGORIES = "categories"
|
||||||
@@ -115,8 +117,11 @@ class TabsFragment: ScreenFragment() {
|
|||||||
val toolbar = view.findViewById<Toolbar>(R.id.toolbar)!!
|
val toolbar = view.findViewById<Toolbar>(R.id.toolbar)!!
|
||||||
screenActivity.onToolbarCreated(toolbar)
|
screenActivity.onToolbarCreated(toolbar)
|
||||||
toolbar.setTitle(R.string.app_name)
|
toolbar.setTitle(R.string.app_name)
|
||||||
|
// Move focus from SearchView to Toolbar
|
||||||
|
toolbar.isFocusableInTouchMode = true
|
||||||
|
|
||||||
val searchView = SearchView(toolbar.context)
|
val searchView = FocusSearchView(toolbar.context)
|
||||||
|
searchView.allowFocus = savedInstanceState?.getBoolean(STATE_SEARCH_FOCUSED) == true
|
||||||
searchView.maxWidth = Int.MAX_VALUE
|
searchView.maxWidth = Int.MAX_VALUE
|
||||||
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
|
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
|
||||||
override fun onQueryTextSubmit(query: String?): Boolean {
|
override fun onQueryTextSubmit(query: String?): Boolean {
|
||||||
@@ -307,6 +312,7 @@ class TabsFragment: ScreenFragment() {
|
|||||||
override fun onSaveInstanceState(outState: Bundle) {
|
override fun onSaveInstanceState(outState: Bundle) {
|
||||||
super.onSaveInstanceState(outState)
|
super.onSaveInstanceState(outState)
|
||||||
|
|
||||||
|
outState.putBoolean(STATE_SEARCH_FOCUSED, searchMenuItem?.actionView!!.hasFocus())
|
||||||
outState.putString(STATE_SEARCH_QUERY, searchQuery)
|
outState.putString(STATE_SEARCH_QUERY, searchQuery)
|
||||||
outState.putByte(STATE_SHOW_CATEGORIES, if (showCategories) 1 else 0)
|
outState.putByte(STATE_SHOW_CATEGORIES, if (showCategories) 1 else 0)
|
||||||
outState.putStringArrayList(STATE_CATEGORIES, ArrayList(categories))
|
outState.putStringArrayList(STATE_CATEGORIES, ArrayList(categories))
|
||||||
@@ -317,6 +323,7 @@ class TabsFragment: ScreenFragment() {
|
|||||||
override fun onViewStateRestored(savedInstanceState: Bundle?) {
|
override fun onViewStateRestored(savedInstanceState: Bundle?) {
|
||||||
super.onViewStateRestored(savedInstanceState)
|
super.onViewStateRestored(savedInstanceState)
|
||||||
|
|
||||||
|
(searchMenuItem?.actionView as FocusSearchView).allowFocus = true
|
||||||
if (needSelectUpdates) {
|
if (needSelectUpdates) {
|
||||||
needSelectUpdates = false
|
needSelectUpdates = false
|
||||||
selectUpdatesInternal(false)
|
selectUpdatesInternal(false)
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package nya.kitsunyan.foxydroid.widget
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import android.view.KeyEvent
|
||||||
|
import android.widget.SearchView
|
||||||
|
|
||||||
|
class FocusSearchView: SearchView {
|
||||||
|
constructor(context: Context): super(context)
|
||||||
|
constructor(context: Context, attrs: AttributeSet?): super(context, attrs)
|
||||||
|
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
|
||||||
|
|
||||||
|
var allowFocus = true
|
||||||
|
|
||||||
|
override fun dispatchKeyEventPreIme(event: KeyEvent): Boolean {
|
||||||
|
// Always clear focus on back press
|
||||||
|
return if (hasFocus() && event.keyCode == KeyEvent.KEYCODE_BACK) {
|
||||||
|
if (event.action == KeyEvent.ACTION_UP) {
|
||||||
|
clearFocus()
|
||||||
|
}
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
super.dispatchKeyEventPreIme(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setIconified(iconify: Boolean) {
|
||||||
|
super.setIconified(iconify)
|
||||||
|
|
||||||
|
// Don't focus view and raise keyboard unless allowed
|
||||||
|
if (!iconify && !allowFocus) {
|
||||||
|
clearFocus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user