在Android上,布局的显示和隐藏往往很生硬,给人体验不好,本文使用 android:animateLayoutChanges属性,使得体验效果瞬间提升一个档次。
展开和收起
package com.zw.demo20230928
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private lateinit var llTitle: LinearLayout
private lateinit var recyclerView: RecyclerView
private lateinit var tvTitle: TextView
private val data =ArrayList<String>()
private var isClick =false;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.recyclerView)
llTitle = findViewById(R.id.ll_title)
tvTitle = findViewById(R.id.tv_title)
llTitle.setOnClickListener {
isClick = !isClick
if (isClick){
tvTitle.text = "收起"
recyclerView.visibility = View.VISIBLE
}else{
tvTitle.text = "展开"
recyclerView.visibility = View.GONE
}
}
var manager=LinearLayoutManager(this)
manager.orientation =LinearLayoutManager.VERTICAL
recyclerView.layoutManager =manager
recyclerView.adapter =object :RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val v = LayoutInflater.from(recyclerView.context).inflate(R.layout.item_test, parent, false)
return object : RecyclerView.ViewHolder(v){
}
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.findViewById<TextView>(R.id.tv_name).text = data[position]
}
}
initData()
}
private fun initData(){
data.add("内容----1")
data.add("内容----2")
data.add("内容----3")
data.add("内容----4")
data.add("内容----5")
data.add("内容----6")
}
}