• Android一个简单带动画的展开收起功能


    Android上,布局的显示和隐藏往往很生硬,给人体验不好,本文使用 android:animateLayoutChanges属性,使得体验效果瞬间提升一个档次。

    1、效果如下:

    展开和收起

    2、具体代码如下:

    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")
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
  • 相关阅读:
    SourceInsight添加对汇编语言文件.s和.S的支持
    jmeter遇到连接数据库的问题
    ubuntu xrdp远程登录一直弹出Authentication required. System policy prevents WiFi scans
    51从零开始用Rust编写nginx,江湖救急,TLS证书快过期了
    Opengl之混合
    金仓数据库KingbaseES客户端应用参考手册--5. dropdb
    更适合电音的蓝牙耳机,设计真的很潮,哈氪零度青春版上手
    Module object(emscripten)
    SQLite Studio软件的安装及基本使用
    阅读LINGO-1: Exploring Natural Language for Autonomous Driving
  • 原文地址:https://blog.csdn.net/zhang106209/article/details/133387122