美文网首页
可以拖拽高度的View

可以拖拽高度的View

作者: Android小豆渣 | 来源:发表于2022-02-24 13:00 被阅读0次

示例是LinearLayout布局,这里可灵活变换

/**
 * @CreateDate: 2021/1/11 14:59
 * @Author: ZZJ
 * @Description: 可以拖拽高度的View
 */
class DragHeightView : LinearLayout {

    private var parentHeight = 0
    private var parentWidth = 0
    private var lastX = 0
    private var lastY = 0
    private var button: AppCompatImageView? = null

    constructor(context: Context) : super(context) {
        initView(context)
    }

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

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

    @SuppressLint("ClickableViewAccessibility")
    private fun initView(context: Context) {
        button = AppCompatImageView(context)
        addView(button)
        val layoutParams2 = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)

        button?.layoutParams = layoutParams2
        button?.setImageDrawable(context.getDrawable(R.drawable.ic_practice_top))
        button?.scaleType = ImageView.ScaleType.FIT_XY
        button?.setOnTouchListener { _, event ->
            val rawX = event.rawX.toInt()
            val rawY = event.rawY.toInt()
            when (event.action and MotionEvent.ACTION_MASK) {
                MotionEvent.ACTION_DOWN -> {
                    isPressed = true
                    parent.requestDisallowInterceptTouchEvent(true)
                    lastX = rawX
                    lastY = rawY
                    val parent: ViewGroup
                    if (getParent() != null) {
                        parent = getParent() as ViewGroup
                        parentHeight = parent.height
                        parentWidth = parent.width
                    }
                }
                MotionEvent.ACTION_MOVE -> {

                    //计算手指移动了多少
                    val dx: Int = rawX - lastX
                    val dy: Int = rawY - lastY
                    val mLayoutParams = layoutParams
                    when {
                        mLayoutParams.height - dy <= 300 -> {
                            mLayoutParams.height = 300
                        }
                        mLayoutParams.height - dy >= parentHeight -> {
                            mLayoutParams.height = parentHeight
                        }
                        else -> {
                            mLayoutParams.height = mLayoutParams.height - dy
                        }
                    }
                    layoutParams = mLayoutParams
                    lastX = rawX
                    lastY = rawY
                }
                MotionEvent.ACTION_UP -> {
                    //恢复按压效果
                    isPressed = false
                }
            }
            //如果是拖拽则消s耗事件,否则正常传递即可。
            true
        }
        //监听软键盘 解决材料题评分遮挡问题
        SoftKeyBoardListener.setListener(context as Activity,
            object : SoftKeyBoardListener.OnSoftKeyBoardChangeListener {
                override fun keyBoardShow(height: Int) {
                    //软键盘显示
                    val mLayoutParams = layoutParams
                    if (parent != null) {
                        parentHeight = (parent as ViewGroup).height
                        mLayoutParams.height = parentHeight
                    } else {
                        mLayoutParams.height = height + 300
                    }

                    layoutParams = mLayoutParams
                }

                override fun keyBoardHide(height: Int) {

                }

            })
    }

}

软件盘监听类--这个是项目特殊需求

/**
 * @CreateDate: 2020/4/24 17:47
 * @Author: ZZJ
 * @Description: 监听软键盘
 */
class SoftKeyBoardListener(activity: Activity) {
    private val rootView: View = activity.window.decorView//activity的根视图
    private var rootViewVisibleHeight: Int = 0//纪录根视图的显示高度
    private var onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener? = null

    init {
        //获取activity的根视图
        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.viewTreeObserver.addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener {
            //获取当前根视图在屏幕上显示的大小
            val r = Rect()
            rootView.getWindowVisibleDisplayFrame(r)
            val visibleHeight = r.height()
            println("" + visibleHeight)
            if (rootViewVisibleHeight == 0) {
                rootViewVisibleHeight = visibleHeight
                return@OnGlobalLayoutListener
            }

            //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
            if (rootViewVisibleHeight == visibleHeight) {
                return@OnGlobalLayoutListener
            }

            //根视图显示高度变小超过300,可以看作软键盘显示了,该数值可根据需要自行调整
            if (rootViewVisibleHeight - visibleHeight > 200) {
                if (onSoftKeyBoardChangeListener != null) {
                    onSoftKeyBoardChangeListener?.keyBoardShow(rootViewVisibleHeight - visibleHeight)
                }
                rootViewVisibleHeight = visibleHeight
                return@OnGlobalLayoutListener
            }

            //根视图显示高度变大超过300,可以看作软键盘隐藏了,该数值可根据需要自行调整
            if (visibleHeight - rootViewVisibleHeight > 200) {
                if (onSoftKeyBoardChangeListener != null) {
                    onSoftKeyBoardChangeListener!!.keyBoardHide(visibleHeight - rootViewVisibleHeight)
                }
                rootViewVisibleHeight = visibleHeight
                return@OnGlobalLayoutListener
            }
        })
    }

    private fun setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener
    }

    interface OnSoftKeyBoardChangeListener {
        fun keyBoardShow(height: Int)
        fun keyBoardHide(height: Int)
    }

    companion object {
        @JvmStatic
        fun setListener(activity: Activity, onSoftKeyBoardChangeListener: OnSoftKeyBoardChangeListener) {
            val softKeyBoardListener = SoftKeyBoardListener(activity)
            softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener)
        }
    }
}

相关文章

  • 可以拖拽高度的View

    示例是LinearLayout布局,这里可灵活变换 软件盘监听类--这个是项目特殊需求

  • ViewDragHelper让你轻松让View动起来

    ViewDragHelper 是V4包提供的View拖拽辅助类,用它可以很方便的处理View拖拽,比如探探的卡片功...

  • 自定义拖拽View

    封装简单拖拽视图view,效果如下: 实现思路: 通过箭头的滑动事件坐标动态变化改变容器的高度 上面在箭头Imag...

  • Android OnDragListener

    1、两个概念 1、目标View 不是被拖拽的View,是要拖拽去哪个区域,这个区域就目标View,它要设置OnDr...

  • UIView的Margins

    写在前面 之前使用Storyboard拖拽约束时,可以看到比较的view有margin选项,来支持相对某view的...

  • 2017-05-17-Android拖动view实现

    一、拖动实现的原理 实现拖拽view,在Android中至少可以俩种实现方案:1.可以直接去拖动view2,利用v...

  • 微信小程序拖动组件之movable-view

    movable-view 是一个可移动的视图容器,在页面中可以拖拽滑动。(movable-view必须在 mova...

  • Android View的移动、缩放、旋转组合效果实战

    首先确定功能效果 1.一个View由关闭按钮、文字、拖拽按钮组成2.拖拽文字部分移动这个View3.在拖拽按钮处拖...

  • 拖拽view

    https://github.com/soberZhichao/MoveView

  • View.post()到底做什么

    view.post()什么情况下使用 通过view.post可以获取元素的高度和宽度 为什么可以获取到测量后的高度...

网友评论

      本文标题:可以拖拽高度的View

      本文链接:https://www.haomeiwen.com/subject/zbbblrtx.html