1. 程式人生 > >自定義控制元件-----控制元件佈局隨著手指滑動而滑動

自定義控制元件-----控制元件佈局隨著手指滑動而滑動

依舊是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,完成