天天看点

软键盘顶起滑动view的技术分析

软键盘顶起滑动view的技术分析
主要工具类如下:

/**
 * 解决输入框遮挡问题
 */
class AndroidBug5497WorkaroundUpdate {
    private var mContentView: View? = null
    private var usableHeightPrevious = 0
    private var frameLayoutParams: ViewGroup.LayoutParams? = null
    private var height = 0
    private var scrollView: View? = null//要滑动的view
    private var offView:View?=null  //需要便宜的view
    private var mOffDistance=0//偏移距离
    private var callBack:OnEditChangeCallBack? = null


    fun setCallBack(callBack: OnEditChangeCallBack) {
        this.callBack=callBack
    }

    private constructor(
        content: View?,
        srollView: View
    ) {
        if (content != null) {
            mContentView = content
            scrollView = srollView
            mContentView?.viewTreeObserver?.addOnGlobalLayoutListener {
                possiblyResizeChildOfContentOther()
            }
            frameLayoutParams = mContentView!!.layoutParams

        }
    }

    private constructor(
        content: View?,
        srollView: View,offDistance: Int
    ) {
        if (content != null) {
            mContentView = content
            scrollView = srollView
            mOffDistance=offDistance
            mContentView?.viewTreeObserver?.addOnGlobalLayoutListener {
                possiblyResizeChildOfContentOther()
            }
            frameLayoutParams = mContentView!!.layoutParams

        }
    }

    private constructor(content: View?) {
        if (content != null) {
            mContentView = content
            mContentView?.viewTreeObserver?.addOnGlobalLayoutListener {
                possiblyResizeChildOfContentOther()
            }
            frameLayoutParams = mContentView!!.layoutParams

        }
    }


    private fun computeUsableHeight(): Int {
        //计算视图可视高度
        val r = Rect()
        mContentView!!.getWindowVisibleDisplayFrame(r)
        val top = r.top
        return r.bottom
    }

    // 其他方式处理
    private fun possiblyResizeChildOfContentOther() {
        val usableHeightNow = computeUsableHeight() // 可视高度
         val screenHeight =mContentView?.rootView?.height//屏幕高度
        screenHeight?.let {
            height=it-usableHeightNow-mOffDistance
        }


        if (usableHeightNow != usableHeightPrevious) {
            val isEdit = mContentView!!.measuredHeight - usableHeightNow > 0
            synchronized(this) {
                mContentView!!.postDelayed({
                    callBack?.let {
                        if (!it.callBack(isEdit, height)) {
                            offView=it.getEditOffView()
                            setPaddingBottom(isEdit, scrollView)
                            if (isEdit) {
                                dealRecycleEditSoftkeyboard(

                                )
                            }
                        }
                    }
                }, 0)
            }
        }
    }




    fun setPaddingBottom(isEdit: Boolean, bottomView: View?) {
        if (isEdit) {
            bottomView?.setPadding(
                bottomView.paddingLeft,
                bottomView.paddingTop,
                bottomView.paddingRight,
                height
            )


        } else {
            bottomView?.setPadding(
                bottomView.paddingLeft,
                bottomView.paddingTop,
                bottomView.paddingRight,
                0
            )


        }
    }

    //处理recycleview嵌套editView软键盘隐藏输入框的问题
    fun dealRecycleEditSoftkeyboard() {

        //计算除了软键盘的可见高度
        val usableHeightNow = computeUsableHeight() // 可视高度


        offView?.let {

            val focusViewTop = it.top
            val itemHeight = it.height
            val position = IntArray(2)
            it.getLocationInWindow(position)
            LogUtil.e("BBB","距离屏幕的距离"+it.y)
            var d=position[1]-usableHeightNow+itemHeight
            if (d>0){
                scrollView!!.scrollBy(0, d)
            }
        }
    }

    companion object {
        fun assistActivity(content: View?): AndroidBug5497WorkaroundUpdate {
            return AndroidBug5497WorkaroundUpdate(content)
        }


        fun assistActivity(
            content: View?,
            scrollView: View
        ): AndroidBug5497WorkaroundUpdate {
            return AndroidBug5497WorkaroundUpdate(content,scrollView)
        }

        fun assistActivity(
            content: View?,
            scrollView: View,offDistance:Int
        ): AndroidBug5497WorkaroundUpdate {
            return AndroidBug5497WorkaroundUpdate(content,scrollView,offDistance)
        }
    }
}