自定义控件-----控件布局随着手指滑动而滑动

依旧是kotlin代码写的

一、主要关键类

class SlideLinearLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs), View.OnTouchListener {

    private var mHeight: Int = 0//布局的高度
    private val mContentView: LinearLayout//整个布局
    private val mSreenHeight: Int//整个屏幕的高度

    init {
        val contentView = LayoutInflater.from(context).inflate(R.layout.slide_linearlayout, null)
        contentView.findViewById<View>(R.id.iv_slide).setOnTouchListener(this)
        mContentView = contentView.findViewById(R.id.ll_slide)

        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        mSreenHeight = windowManager.defaultDisplay.height
        mHeight = (this.mSreenHeight * 967.0 / 1280.0).toInt()
        setViewHeight(mContentView, mHeight)
        addView(contentView)
    }


    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when (event!!.action) {
            MotionEvent.ACTION_MOVE -> {//关键代码,布局随手指滑动而滑动
                mHeight = mSreenHeight - event.rawY.toInt()
                setViewHeight(mContentView, mHeight)
                mContentView.postInvalidate()
            }
            MotionEvent.ACTION_UP -> {//松开以后回到原始高度
                mHeight = (this.mSreenHeight * 967.0 / 1280.0).toInt()
                setViewHeight(mContentView, mHeight)
            }
        }
        return true
    }

    /**
     * 设置子控件的高度
     */
    private fun setViewHeight(view: View, height: Int) {
        val params = view.layoutParams as LinearLayout.LayoutParams
        params.height = height
        view.layoutParams = params
    }

}

二、布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_slide"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="46px"
            android:layout_above="@+id/rv_group"
            android:background="#fff">

            <ImageView
                android:id="@+id/iv_slide"
                android:layout_width="match_parent"
                android:layout_height="46px"
                android:layout_centerHorizontal="true"
                android:background="#f30"
                android:scaleX="0.5"
                android:scaleY="0.25" />

        </RelativeLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="1111111"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="222222"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="3333333"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="4444444"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="5555555"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="66666666"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="4444444"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="5555555"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="66666666"/>

        <RelativeLayout

            android:id="@+id/rl_cross"
            android:layout_width="match_parent"
            android:layout_height="105px"
            android:layout_alignParentBottom="true"
            android:background="#fff">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="#f30" />

            <ImageView
                android:id="@+id/iv_cross"
                android:layout_width="105px"
                android:layout_height="105px"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="#3f0"
                android:scaleX="0.3"
                android:scaleY="0.3" />
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

三、引用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewTestActivity">

    <bai.bai.bai.demo.view.SlideLinearLayout
        android:layout_width="match_parent"
        android:background="#333"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"/>

</RelativeLayout>

ok,完成

猜你喜欢

转载自blog.csdn.net/qq_36968707/article/details/81874508
今日推荐