Add search button to home fragment (#218)
This commit is contained in:
parent
fa5d73faa3
commit
c712f05dda
3 changed files with 70 additions and 1 deletions
|
@ -7,8 +7,10 @@ import android.view.MenuInflater
|
|||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.WindowManager
|
||||
import android.widget.Toast
|
||||
import android.widget.Toast.LENGTH_LONG
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.view.MenuHost
|
||||
import androidx.core.view.MenuProvider
|
||||
import androidx.core.view.isVisible
|
||||
|
@ -38,6 +40,8 @@ class HomeFragment : Fragment() {
|
|||
private lateinit var binding: FragmentHomeBinding
|
||||
private val viewModel: HomeViewModel by viewModels()
|
||||
|
||||
private var originalSoftInputMode: Int? = null
|
||||
|
||||
private lateinit var errorDialog: ErrorDialogFragment
|
||||
|
||||
override fun onCreateView(
|
||||
|
@ -61,6 +65,38 @@ class HomeFragment : Fragment() {
|
|||
object : MenuProvider {
|
||||
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
|
||||
menuInflater.inflate(R.menu.home_menu, menu)
|
||||
|
||||
val settings = menu.findItem(R.id.action_settings)
|
||||
val search = menu.findItem(R.id.action_search)
|
||||
val searchView = search.actionView as SearchView
|
||||
searchView.queryHint = getString(R.string.search_hint)
|
||||
|
||||
search.setOnActionExpandListener(
|
||||
object : MenuItem.OnActionExpandListener {
|
||||
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
|
||||
settings.isVisible = false
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
|
||||
settings.isVisible = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
||||
override fun onQueryTextSubmit(p0: String?): Boolean {
|
||||
if (p0 != null) {
|
||||
navigateToSearchResultFragment(p0)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onQueryTextChange(p0: String?): Boolean {
|
||||
return false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
|
||||
|
@ -77,12 +113,27 @@ class HomeFragment : Fragment() {
|
|||
)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
requireActivity().window.let {
|
||||
originalSoftInputMode = it.attributes?.softInputMode
|
||||
it.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
viewModel.loadData()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
|
||||
originalSoftInputMode?.let { activity?.window?.setSoftInputMode(it) }
|
||||
}
|
||||
|
||||
private fun setupView() {
|
||||
binding.refreshLayout.setOnRefreshListener {
|
||||
viewModel.loadData()
|
||||
|
@ -194,4 +245,10 @@ class HomeFragment : Fragment() {
|
|||
HomeFragmentDirections.actionHomeFragmentToSettingsFragment()
|
||||
)
|
||||
}
|
||||
|
||||
private fun navigateToSearchResultFragment(query: String) {
|
||||
findNavController().navigate(
|
||||
HomeFragmentDirections.actionHomeFragmentToSearchResultFragment(query)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,16 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<menu xmlns:tools="http://schemas.android.com/tools"
|
||||
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="@drawable/ic_search"
|
||||
android:title="@string/search"
|
||||
app:actionViewClass="androidx.appcompat.widget.SearchView"
|
||||
app:showAsAction="always|collapseActionView"
|
||||
tools:ignore="AlwaysShowAction" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:icon="@drawable/ic_settings"
|
||||
|
|
|
@ -42,6 +42,9 @@
|
|||
<action
|
||||
android:id="@+id/action_homeFragment_to_mediaDetailFragment"
|
||||
app:destination="@id/mediaDetailFragment" />
|
||||
<action
|
||||
android:id="@+id/action_homeFragment_to_searchResultFragment"
|
||||
app:destination="@id/searchResultFragment" />
|
||||
</fragment>
|
||||
|
||||
<fragment
|
||||
|
|
Loading…
Reference in a new issue