Skip to content

Commit

Permalink
Hide keyboard in closing search
Browse files Browse the repository at this point in the history
  • Loading branch information
Faltenreich committed Oct 4, 2020
1 parent 7662b28 commit 2e28627
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.faltenreich.release.domain.release.discover
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.viewModels
Expand All @@ -18,6 +19,7 @@ import com.faltenreich.release.domain.release.list.ReleaseDateItem
import com.faltenreich.release.domain.release.list.ReleaseProvider
import com.faltenreich.release.domain.release.search.SearchListAdapter
import com.faltenreich.release.framework.android.fragment.BaseFragment
import com.faltenreich.release.framework.android.view.hideKeyboard
import com.faltenreich.release.framework.skeleton.SkeletonFactory
import kotlinx.android.synthetic.main.fragment_discover.*
import org.threeten.bp.LocalDate
Expand Down Expand Up @@ -68,6 +70,26 @@ class DiscoverFragment : BaseFragment(R.layout.fragment_discover, R.menu.main),
searchListAdapter = SearchListAdapter(requireContext())
}

private fun initList() {
val context = context ?: return

listLayoutManager = DiscoverLayoutManager(context, listAdapter)
listItemDecoration = DiscoverItemDecoration(context)

listView.layoutManager = listLayoutManager
listView.addItemDecoration(listItemDecoration)
listView.adapter = listAdapter

listView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (isAdded) {
invalidateListHeader()
}
}
})
}

private fun initSearch() {
val context = context ?: return

Expand All @@ -84,27 +106,26 @@ class DiscoverFragment : BaseFragment(R.layout.fragment_discover, R.menu.main),
// TODO: Toggle depending on state
motionLayout.transitionToStart()
}
clearButton.setOnClickListener { searchView.text = null }
}

private fun initList() {
val context = context ?: return
motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {

listLayoutManager = DiscoverLayoutManager(context, listAdapter)
listItemDecoration = DiscoverItemDecoration(context)
override fun onTransitionStarted(motionLayout: MotionLayout?, startId: Int, endId: Int) = Unit

listView.layoutManager = listLayoutManager
listView.addItemDecoration(listItemDecoration)
listView.adapter = listAdapter
override fun onTransitionChange(motionLayout: MotionLayout?, startId: Int, endId: Int, progress: Float) = Unit

listView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (isAdded) {
invalidateListHeader()
override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
when (currentId) {
R.id.start -> {
searchView.hideKeyboard()
view?.requestFocus()
}
}
}

override fun onTransitionTrigger(motionLayout: MotionLayout?, triggerId: Int, positive: Boolean, progress: Float) = Unit
})

clearButton.setOnClickListener { searchView.text = null }
}

private fun setDate(date: LocalDate) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.faltenreich.release.framework.android.view

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.res.ColorStateList
import android.view.View
import android.view.inputmethod.InputMethodManager

var View.backgroundTint: Int?
get() = backgroundTintList?.defaultColor
Expand All @@ -23,4 +25,14 @@ val View.activity: Activity?
context = context.baseContext
}
return null
}
}

fun View.showKeyboard() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

0 comments on commit 2e28627

Please sign in to comment.