BottomSheetBehavior 底部抽屉

826 阅读1分钟

Screenshot_2022_0531_142034.gif

布局文件 activity_main

<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">

    <include layout="@layout/layout_answer_main" />

    <include layout="@layout/layout_answer_bottom" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

布局文件 layout_answer_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/answerMainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#99ffffff"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/answerTitle"
        android:layout_width="match_parent"
        android:layout_height="44dp">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="试卷"
            android:textSize="14sp"
            android:textStyle="bold" />

    </RelativeLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/answerViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/answerTitle" />

    <View
        android:id="@+id/backView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:visibility="gone" />


</RelativeLayout>

布局文件 layout_answer_bottom

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/answerBottomContainer"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@color/teal_200"
    android:clickable="true"
    android:orientation="vertical"
    app:behavior_hideable="false"
    app:behavior_peekHeight="50dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <RelativeLayout
        android:id="@+id/answerBottomTitleRL"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/purple_700">

    </RelativeLayout>


</LinearLayout>

代码 mainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var mAdapter: AnswerAdapter
    val myFormat = DecimalFormat("0.0")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val viewPager = findViewById<ViewPager2>(R.id.answerViewPager)
        val mainContainer = findViewById<RelativeLayout>(R.id.answerMainContainer)
        val bottomContainer = findViewById<LinearLayout>(R.id.answerBottomContainer)
        val behaivor = BottomSheetBehavior.from(bottomContainer)
        val answerBottomTitleRL = findViewById<RelativeLayout>(R.id.answerBottomTitleRL)
        val backView = findViewById<View>(R.id.backView)

        mAdapter = AnswerAdapter(this, R.layout.adapter_answer)
        mAdapter.add()
        viewPager.adapter = mAdapter
        viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL


        behaivor.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {

                if (newState < 4) {
                    backView.visibility = View.VISIBLE
                } else {
                    backView.visibility = View.GONE
                }


            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {


                //向上滑动,设置头部背景
                setViewBackGround(slideOffset, backView)


            }
        })

        answerBottomTitleRL.setOnClickListener {

            if (behaivor.state == BottomSheetBehavior.STATE_COLLAPSED) {
                behaivor.state = BottomSheetBehavior.STATE_EXPANDED
            } else if (behaivor.state == BottomSheetBehavior.STATE_EXPANDED) {
                behaivor.state = BottomSheetBehavior.STATE_COLLAPSED
            }
        }

    }

    //弹出后给主View加背景色
    private fun setViewBackGround(slideOffset: Float, backView: View) {

        //1.先保留一位小数
        var doubouSlide = myFormat.format(slideOffset).toDouble()
        if (doubouSlide > 0.5) {
            doubouSlide = 0.5
        }
        //2.将十进制转换成十六进制
        var sixteenStr = Integer.toHexString((doubouSlide * 255).toInt())
        //3.如果是百分之百透明
        if (sixteenStr == "0") {
            sixteenStr = "00"
        }
        //4.设置背景色
        backView.setBackgroundColor(Color.parseColor("#${sixteenStr}000000"))

    }


}

代码 adapter

class AnswerAdapter(val context: Context, val layoutId: Int) :
    RecyclerView.Adapter<AnswerAdapter.AnswerHolder>() {


    private val mDatas = mutableListOf<String>()


    class AnswerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val coontent = itemView.findViewById<TextView>(R.id.adapterAnswerContent)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnswerHolder {
        return AnswerHolder(LayoutInflater.from(context).inflate(layoutId, parent, false))
    }

    override fun onBindViewHolder(holder: AnswerHolder, position: Int) {
        holder.coontent.text = mDatas[position]
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }


    fun add() {

        for (i in 0..20) {

            mDatas.add("题目-->$i")

        }


    }
}