Android底部控件隨軟鍵盤(pán)上移

很多時(shí)候我們會(huì)遇到這個(gè)問(wèn)題翎迁,就是點(diǎn)擊EditText彈出軟鍵盤(pán)的時(shí)候會(huì)遮擋底部的Button,網(wǎng)上的解決方法一般都是設(shè)置WindowSoftInputMode和scrollview嵌套净薛,這個(gè)局限性很多汪榔,而且有時(shí)候達(dá)不到我們想要的效果,下面我自定義了一個(gè)AutoKeyboardConstraintLayout肃拜,可以很小代碼改動(dòng)輕松解決這一問(wèn)題痴腌。

閑話不多說(shuō),先看效果圖:
圖1.gif

圖2.gif
用法很簡(jiǎn)單

第一種模式的用法(圖1)

<!--用自定義的ConstraintLayout-->
<com.cfxc.autokeyboarddemo.autokeyboard.AutoKeyboardConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    <!--底部view上移后最高不能越過(guò)哪個(gè)view id-->
    app:aboveViewId="@+id/tv_register" 
    <!--移動(dòng)模式. 不需要滾動(dòng)的就用這個(gè)模式-->
    app:autoType="auto_translation" 
    <!-- 底部需要上移的view id集燃领,中間用,隔開(kāi)-->
    app:bottomViewIds="btn_login">

      <content
              ....
       />

    <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_login"
            style="@style/largeButtonStyle"
            android:text="login"
            android:layout_marginBottom="30dp"
            android:background="@drawable/shape_white_radius_10"
            app:layout_constraintBottom_toBottomOf="parent" />

第二種模式的用法(圖2)

<com.cfxc.autokeyboarddemo.autokeyboard.AutoKeyboardConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   <!--底部view上移后最高不能越過(guò)哪個(gè)view id-->
    app:aboveViewId="@+id/scroll_view"
   <!--移動(dòng)模式. 需要滾動(dòng)的就用這個(gè)模式-->
    app:autoType="auto_scroll"
   <!-- 底部需要上移的view id集士聪,中間用,隔開(kāi)-->
    app:bottomViewIds="btn_continue,btn_cancel">

    <!--需要滾動(dòng)的內(nèi)容用scroll view-->
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="15dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_continue"
        app:layout_constraintTop_toTopOf="parent">

       <content
        ....
       />

     </androidx.core.widget.NestedScrollView>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn_cancel"
        style="@style/largeButtonStyle"
        android:layout_marginBottom="20dp"
        android:background="@drawable/shape_white_radius_10"
        android:text="Cancel"
        app:layout_constraintBottom_toBottomOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn_continue"
        style="@style/largeButtonStyle"
        android:layout_marginBottom="10dp"
        android:background="@drawable/shape_white_radius_10"
        android:text="Continue"
        app:layout_constraintBottom_toTopOf="@id/btn_cancel" />

用法簡(jiǎn)單吧^?_?^
主要就是封裝自定義了AutoKeyboardConstraintLayout,用的時(shí)候只需要替換原布局的ConstraintLayout猛蔽,xml中設(shè)置幾個(gè)參數(shù)即可剥悟,代碼不需要任何改動(dòng)灵寺,不習(xí)慣用ConstraintLayout的童鞋,可以參照封裝改成LinearLayout

下面是實(shí)現(xiàn)

定義下幾個(gè)屬性

<declare-styleable name="AutoKeyboardConstraintLayout">
        <attr name="bottomViewIds"/>
        <attr name="aboveViewId"/>
        <attr name="autoMargin"/>
        <attr name="autoType"/>
    </declare-styleable>
    <attr name="bottomViewIds" format="string" />
    <attr name="aboveViewId" format="reference" />
    <attr name="autoMargin" format="dimension" />
    <attr name="autoType" format="flags">
        <flag name="normal" value="0" />
        <flag name="auto_translation" value="1" />
        <flag name="auto_scroll" value="2" />
    </attr>

主要是這個(gè)AutoKeyboardConstraintLayout這個(gè)類

class AutoKeyboardConstraintLayout : ConstraintLayout {

    // not need move up
    private val NORMAL = 0

    // bottom view move up, the above view move up as need
    private val AUTO_TRANSLATION = 1

    /** bottom view move up, and change the scrollView height
     * Note: When used this type, that need to have a ScrollView or NestedScrollView or RecyclerView in the layout, and set the above view's id is it's id
     **/
    private val AUTO_SCROLL = 2

    // the id of the views that need move up
    private var bottomViewIds = arrayListOf<Int>()

    //the id of the view above of the bottom view
    private var aboveViewId: Int = NO_ID
    private var onGlobalLayoutListener: ViewTreeObserver.OnGlobalLayoutListener? = null
    private var rootViewVisibleHeight: Int = 0
    private var rootViewHeight: Int = 0

    // This margin is the height from the keyboard when the bottom view moves up
    private var viewMargin: Float = 10f

    //the type of that need move up
    private var autoType = NORMAL
    private val animatorDuration = 100L
    private var toolbarHeight: Int = 0

    constructor(context: Context) : super(context) {
        initViews(null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initViews(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initViews(attrs)
    }

    @SuppressLint("CustomViewStyleable")
    private fun initViews(attrs: AttributeSet?) {
        attrs?.let {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoKeyboardConstraintLayout)
            val idStrings = typedArray.getString(R.styleable.AutoKeyboardConstraintLayout_bottomViewIds)
            setBottomViewID(idStrings)
            aboveViewId = typedArray.getResourceId(R.styleable.AutoKeyboardConstraintLayout_aboveViewId, NO_ID)
            viewMargin = typedArray.getDimension(R.styleable.AutoKeyboardConstraintLayout_autoMargin, 10f)
            autoType = typedArray.getInt(R.styleable.AutoKeyboardConstraintLayout_autoType, NORMAL)
            typedArray.recycle()
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        if (autoType != NORMAL) {
            onGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
                //Gets the size of the current root view displayed on the screen
                val r = Rect()
                getWindowVisibleDisplayFrame(r)

                val visibleHeight = r.height()
                rootViewHeight = rootViewHeight.coerceAtLeast(visibleHeight)
                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight
                    return@OnGlobalLayoutListener
                }
                //The display height of the root view doesn't change
                if (rootViewVisibleHeight == visibleHeight) {
                    return@OnGlobalLayoutListener
                }

                //The display height of the root view has been reduced to over 200 and can be viewed as a soft keyboard display
                if (rootViewHeight - visibleHeight > 200) {
                    val keyboardHeight = rootViewHeight - visibleHeight

                    changeLayout(keyboardHeight)
                    rootViewVisibleHeight = visibleHeight
                    return@OnGlobalLayoutListener
                }

                //The root view shows a larger height than 200, which can be seen as a soft keyboard hidden
                if (visibleHeight - rootViewVisibleHeight > 200) {
                    //keyboard hide
                    restoreView()
                    rootViewVisibleHeight = visibleHeight
                    return@OnGlobalLayoutListener
                }
            }
            viewTreeObserver?.addOnGlobalLayoutListener(onGlobalLayoutListener)
        }
    }

    private fun changeLayout(keyboardHeight: Int) {
        when (autoType) {
            AUTO_TRANSLATION, AUTO_SCROLL -> translationBottomView(keyboardHeight)
        }
    }

    private fun translationBottomView(keyboardHeight: Int) {
        //this is to support toolbar
        val parentView = getRootView(parent)
        // parentView is MainActivity root view
        parentView?.let {drawerLayout->
            // the height of the toolbar is the view in the MainActivity layout
            if (drawerLayout is ViewGroup){
                val toolbarParent = drawerLayout[0]
                if(toolbarParent is ViewGroup && toolbarParent[0] is Toolbar&& toolbarParent[0].isShown){
                    toolbarHeight = toolbarParent[0].height
                }
            }
        }
        var translationHeight = 0f
        var isNeedBottomTranslation = false
        // the lowest view of the bottom views
        var lowestView: View? = null
        // the highest view of the bottom views
        var highestView: View? = null
        var needHeight = 0f
        for (item in bottomViewIds) {
            val bottomView = getViewById(item)
            bottomView?.let {
                if (bottomView.visibility == View.VISIBLE) {
                    lowestView = if (bottomView.bottom > lowestView?.bottom ?: 0) bottomView else lowestView
                    highestView = if (bottomView.top < highestView?.top ?: rootViewHeight) bottomView else highestView
                }
            }
        }

        val aboveView = if (aboveViewId == NO_ID) null else getViewById(aboveViewId)
        var aboveViewBottom = 0
        aboveView?.apply {
            aboveViewBottom = bottom
        }

        lowestView?.let { lowest ->
            if (this.height - lowest.bottom >= keyboardHeight) return
            highestView?.let { highest ->
                val bottomViewsHeight = lowest.bottom - highest.top
                if (aboveViewBottom == 0
                        || keyboardHeight + bottomViewsHeight + dip2px(viewMargin * 2) < this.height - aboveViewBottom
                        || autoType == AUTO_SCROLL) {
                    needHeight = keyboardHeight - (this.height - lowest.bottom) + dip2px(viewMargin) - getNavigationBarHeight()
                    translationHeight = 0f
                    isNeedBottomTranslation = true
                } else {
                    needHeight = keyboardHeight + aboveViewBottom + bottomViewsHeight + dip2px(viewMargin * 2) - getNavigationBarHeight()
                    translationHeight = this.height - needHeight
                    isNeedBottomTranslation = lowest.bottom + translationHeight > this.height - keyboardHeight - dip2px(viewMargin)
                }
            }
        }

        if (isNeedBottomTranslation)
            for (item in bottomViewIds) {
                val bottomView = getViewById(item)
                if (bottomView.visibility == View.VISIBLE)
                    bottomView.animate().setDuration(animatorDuration).translationY(this.height - (lowestView?.bottom
                            ?: 0) - keyboardHeight - dip2px(viewMargin) - translationHeight + getNavigationBarHeight()).start()
            }
        if (autoType == AUTO_TRANSLATION && aboveViewBottom > 0 && needHeight > this.height) {
            this.animate().setDuration(animatorDuration).translationY(translationHeight).start()
        }
        if (autoType == AUTO_SCROLL) {
            aboveView?.let {
                if (aboveView is ScrollView || aboveView is NestedScrollView || aboveView is RecyclerView) {
                    val layoutParams = aboveView.layoutParams
                    (layoutParams as LayoutParams).bottomMargin = (needHeight + dip2px(viewMargin)).toInt()
                    aboveView.layoutParams = layoutParams
                }
            }
        }
    }

    private fun getRootView(viewParent: ViewParent?): ViewParent? {
        if (viewParent != null) {
            if (viewParent is DrawerLayout) {
                return viewParent
            } else {
               return getRootView(viewParent.parent)
            }
        } else {
            return null
        }
    }

    private fun restoreView() {
        when (autoType) {
            AUTO_TRANSLATION, AUTO_SCROLL -> restoreTranslation()
        }
    }

    private fun restoreTranslation() {
        for (item in bottomViewIds) {
            val bottomView = getViewById(item)
            bottomView?.let {
                this.animate().setDuration(animatorDuration).translationY(0f)
                it.animate().setDuration(animatorDuration).translationY(0f)
            }
        }
        if (autoType == AUTO_SCROLL) {
            val aboveView = if (aboveViewId == NO_ID) null else getViewById(aboveViewId)
            aboveView?.let {
                if (aboveView is ScrollView || aboveView is NestedScrollView || aboveView is RecyclerView) {
                    val layoutParams = aboveView.layoutParams
                    (layoutParams as LayoutParams).bottomMargin = 0
                    aboveView.layoutParams = layoutParams
                }
            }
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        toolbarHeight = 0
        if (autoType != NORMAL)
            viewTreeObserver?.removeOnGlobalLayoutListener(onGlobalLayoutListener)
    }

    private fun setBottomViewID(idStrings: String?) {
        idStrings?.let {
            val idLists = it.split(",")
            for (item in idLists) {
                addID(item)
            }
        }
    }

    private fun addID(idString: String) {
        val idStr = idString.trim()
        var tag = 0
        try {
            val res: Class<*> = id::class.java
            val field = res.getField(idStr)
            tag = field.getInt(null as Any?)
        } catch (var5: Exception) {
        }
        if (tag == 0) {
            tag = context.resources.getIdentifier(idStr, "id", context.packageName)
        }
        if (tag == 0) {
            val constraintLayout = this.parent as ConstraintLayout
            val value = constraintLayout.getDesignInformation(0, idStr)
            if (value != null && value is Int) {
                tag = value
            }
        }
        if (tag != 0) {
            bottomViewIds.add(tag)
        } else {
            Log.w("AutoKeyboardLayout", "Could not find id of \"$idStr\"")
        }
    }

    private fun dip2px(dpValue: Float): Float {
        val scale = context.resources.displayMetrics.density
        return dpValue * scale + 0.5f
    }

    // This is to support the navigation bar show hide
    private fun getNavigationBarHeight(): Int {
        return rootViewHeight - this.height - toolbarHeight
    }
}

注釋是英文的区岗,因?yàn)槭窃谧龉卷?xiàng)目的時(shí)候?qū)懙倪@個(gè)控件略板,公司項(xiàng)目注釋要求是英文的,也懶得改中文了( ̄? ̄)

附上demo地址:https://github.com/fengpeihao/AutoKeyboardDemo

第一次寫(xiě)文章慈缔,寫(xiě)的很簡(jiǎn)單叮称,主要是為了記錄下方便以后用到查看

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市藐鹤,隨后出現(xiàn)的幾起案子瓤檐,更是在濱河造成了極大的恐慌,老刑警劉巖娱节,帶你破解...
    沈念sama閱讀 210,978評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件距帅,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡括堤,警方通過(guò)查閱死者的電腦和手機(jī)碌秸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)悄窃,“玉大人讥电,你說(shuō)我怎么就攤上這事≡梗” “怎么了恩敌?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,623評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)横媚。 經(jīng)常有香客問(wèn)我纠炮,道長(zhǎng),這世上最難降的妖魔是什么灯蝴? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,324評(píng)論 1 282
  • 正文 為了忘掉前任恢口,我火速辦了婚禮,結(jié)果婚禮上穷躁,老公的妹妹穿的比我還像新娘耕肩。我一直安慰自己,他們只是感情好问潭,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布猿诸。 她就那樣靜靜地躺著,像睡著了一般狡忙。 火紅的嫁衣襯著肌膚如雪梳虽。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,741評(píng)論 1 289
  • 那天灾茁,我揣著相機(jī)與錄音窜觉,去河邊找鬼是复。 笑死,一個(gè)胖子當(dāng)著我的面吹牛竖螃,可吹牛的內(nèi)容都是我干的淑廊。 我是一名探鬼主播,決...
    沈念sama閱讀 38,892評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼特咆,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼季惩!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起腻格,我...
    開(kāi)封第一講書(shū)人閱讀 37,655評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤画拾,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后菜职,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體青抛,經(jīng)...
    沈念sama閱讀 44,104評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評(píng)論 2 325
  • 正文 我和宋清朗相戀三年酬核,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蜜另。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,569評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嫡意,死狀恐怖橘券,靈堂內(nèi)的尸體忽然破棺而出蝗锥,到底是詐尸還是另有隱情欠气,我是刑警寧澤铁孵,帶...
    沈念sama閱讀 34,254評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站旧巾,受9級(jí)特大地震影響耸序,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鲁猩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評(píng)論 3 312
  • 文/蒙蒙 一坎怪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绳匀,春花似錦芋忿、人聲如沸炸客。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,725評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)痹仙。三九已至是尔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間开仰,已是汗流浹背拟枚。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,950評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工薪铜, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人恩溅。 一個(gè)月前我還...
    沈念sama閱讀 46,260評(píng)論 2 360
  • 正文 我出身青樓隔箍,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親脚乡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蜒滩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容