kiwix#2717 added addOnScrollListener to hide keyboard in PageFragment.kt and onlineLibraryFragment.kt

This commit is contained in:
cdhiraj40 2021-11-18 12:43:58 +05:30
parent c5f21b03f8
commit 662fde559c
4 changed files with 62 additions and 0 deletions

View File

@ -49,11 +49,13 @@ import org.kiwix.kiwixmobile.core.downloader.Downloader
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.navigate
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel
import org.kiwix.kiwixmobile.core.extensions.closeKeyboard
import org.kiwix.kiwixmobile.core.extensions.snack
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.utils.BookUtils
import org.kiwix.kiwixmobile.core.utils.NetworkUtils
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.SimpleRecyclerViewScrollListener
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
@ -141,6 +143,13 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
zimManageViewModel.shouldShowWifiOnlyDialog.value = false
}
})
// hides keyboard when scrolled
libraryList.addOnScrollListener(SimpleRecyclerViewScrollListener { _, newState ->
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
libraryList.closeKeyboard()
}
})
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {

View File

@ -19,6 +19,7 @@
package org.kiwix.kiwixmobile.core.extensions
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.fragment.app.Fragment
@ -41,4 +42,9 @@ fun Fragment.closeKeyboard() {
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
fun View.closeKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
val Fragment.coreMainActivity get() = activity as CoreMainActivity

View File

@ -42,6 +42,7 @@ import kotlinx.android.synthetic.main.layout_toolbar.toolbar
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.BaseFragment
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
import org.kiwix.kiwixmobile.core.extensions.closeKeyboard
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener
import org.kiwix.kiwixmobile.core.page.adapter.Page
@ -50,6 +51,7 @@ import org.kiwix.kiwixmobile.core.page.viewmodel.Action
import org.kiwix.kiwixmobile.core.page.viewmodel.PageState
import org.kiwix.kiwixmobile.core.page.viewmodel.PageViewModel
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.SimpleRecyclerViewScrollListener
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
import javax.inject.Inject
@ -135,6 +137,13 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv
pageViewModel.actions.offer(Action.UserClickedShowAllToggle(isChecked))
}
pageViewModel.state.observe(viewLifecycleOwner, Observer(::render))
// hides keyboard when scrolled
recycler_view.addOnScrollListener(SimpleRecyclerViewScrollListener { _, newState ->
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
recycler_view.closeKeyboard()
}
})
}
override fun onCreateView(

View File

@ -0,0 +1,38 @@
/*
* Kiwix Android
* Copyright (c) 2021 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.utils
import androidx.recyclerview.widget.RecyclerView
class SimpleRecyclerViewScrollListener(
private val onLayoutScrollListener: (RecyclerView, Int) -> Unit // here we are calling callback
) :
RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
onLayoutScrollListener(
recyclerView,
newState
) // implement the callback by passing recyclerview and newState
super.onScrollStateChanged(recyclerView, newState)
}
}