Skip to content

Commit

Permalink
fix keyboard cover the search list
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Apr 17, 2019
1 parent 862494f commit 926f34f
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'org.jsoup:jsoup:1.11.2'
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:retrofit-converters:2.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CalendarPagerAdapter(val fragment: CalendarFragment, private val pager: Vi
return items.getOrPut(position){
val swipeRefreshLayout = SwipeRefreshLayout(pager.context)
val recyclerView = RecyclerView(pager.context)
recyclerView.overScrollMode = View.OVER_SCROLL_NEVER
recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
scrollTrigger((items[CalendarAdapter.getCalendarInt(getPostDate(pager.currentItem))]?.second?.tag as? RecyclerView)?.canScrollVertically(-1) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CollectionPagerAdapter(private val context: Context, val fragment: Collect
val item = items.getOrPut(position){
val swipeRefreshLayout = SwipeRefreshLayout(container.context)
val recyclerView = RecyclerView(container.context)
recyclerView.overScrollMode = View.OVER_SCROLL_NEVER
recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
scrollTrigger((items[pager.currentItem]?.second?.tag as? RecyclerView)?.canScrollVertically(-1) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class RakuenPagerAdapter(context: Context, val fragment: RakuenFragment, private
val item = items.getOrPut(position){
val swipeRefreshLayout = SwipeRefreshLayout(container.context)
val recyclerView = RecyclerView(container.context)
recyclerView.overScrollMode = View.OVER_SCROLL_NEVER
recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
scrollTrigger((items[pager.currentItem]?.second?.tag as? RecyclerView)?.canScrollVertically(-1) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TimeLinePagerAdapter(context: Context, val fragment: TimeLineFragment, pri
val item = items.getOrPut(position){
val swipeRefreshLayout = SwipeRefreshLayout(container.context)
val recyclerView = RecyclerView(container.context)
recyclerView.overScrollMode = View.OVER_SCROLL_NEVER
recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
scrollTrigger((items[pager.currentItem]?.second?.tag as? RecyclerView)?.canScrollVertically(-1) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.support.v7.widget.RecyclerView
import android.util.Log
import android.util.SparseIntArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.content_index.*
import retrofit2.Call
Expand Down Expand Up @@ -80,6 +81,7 @@ class IndexPagerAdapter(private val fragment: IndexFragment, private val pager:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IndexPagerViewHolder {
val swipeRefreshLayout = SwipeRefreshLayout(parent.context)
val recyclerView = RecyclerView(parent.context)
recyclerView.overScrollMode = View.OVER_SCROLL_NEVER
val adapter = SubjectAdapter()
val viewHolder = IndexPagerViewHolder(swipeRefreshLayout, adapter)
recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package soko.ekibun.bangumi.ui.search

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_search.*
import soko.ekibun.bangumi.R
import soko.ekibun.bangumi.ui.view.SwipeBackActivity
import soko.ekibun.bangumi.util.KeyboardUtil

class SearchActivity : SwipeBackActivity() {

Expand All @@ -21,6 +21,8 @@ class SearchActivity : SwipeBackActivity() {
it.setDisplayShowTitleEnabled(false)
}

KeyboardUtil(this, root_layout)

SearchPresenter(this)
}

Expand Down
82 changes: 82 additions & 0 deletions app/src/main/java/soko/ekibun/bangumi/util/KeyboardUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package soko.ekibun.bangumi.util

import android.app.Activity
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager

/**
* Created by mikepenz on 14.03.15.
* This class implements a hack to change the layout padding on bottom if the keyboard is shown
* to allow long lists with editTextViews
* Basic idea for this solution found here: http://stackoverflow.com/a/9108219/325479
*/
class KeyboardUtil(act: Activity, private val contentView: View) {

private val decorView: View = act.window.decorView


//a small helper to allow showing the editText focus
var onGlobalLayoutListener: ViewTreeObserver.OnGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
val r = Rect()
//r will be populated with the coordinates of your view that area still visible.
decorView.getWindowVisibleDisplayFrame(r)

//get screen height and calculate the difference with the useable area from the r
val height = decorView.context.resources.displayMetrics.heightPixels
val diff = height - r.bottom

//if it could be a keyboard add the padding to the view
if (diff != 0) {
// if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
//check if the padding is 0 (if yes set the padding for the keyboard)
if (contentView.paddingBottom != diff) {
//set the padding of the contentView for the keyboard
contentView.setPadding(0, 0, 0, diff)
}
} else {
//check if the padding is != 0 (if yes reset the padding)
if (contentView.paddingBottom != 0) {
//reset the padding of the contentView
contentView.setPadding(0, 0, 0, 0)
}
}
}

init {
//only required on newer android versions. it was working on API level 19
if (Build.VERSION.SDK_INT >= 19) {
decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
}
}

fun enable() {
if (Build.VERSION.SDK_INT >= 19) {
decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
}
}

fun disable() {
if (Build.VERSION.SDK_INT >= 19) {
decorView.viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener)
}
}

companion object {


/**
* Helper to hide the keyboard
*
* @param act
*/
fun hideKeyboard(act: Activity?) {
if (act != null && act.currentFocus != null) {
val inputMethodManager = act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(act.currentFocus!!.windowToken, 0)
}
}
}
}

0 comments on commit 926f34f

Please sign in to comment.