viewmodel

作者: 闫鹏飞写字的地方 | 来源:发表于2022-06-15 12:56 被阅读0次

    官方地址:https://developer.android.google.cn/topic/libraries/architecture/viewmodel

    B站视频:https://www.bilibili.com/video/BV1fY4y1i7BG?p=7&vd_source=c9e619eb6c2ba53337eccc49eb025732

    执行add方法,+1+1+1+1,i为4,屏幕旋转i会变回0

    class MainActivity : AppCompatActivity() {
        private lateinit var btnAdd:Button
        private lateinit var tvCount: TextView
        private var i = 0
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            btnAdd = findViewById(R.id.btnAdd)
            tvCount = findViewById(R.id.tvCount)
            btnAdd.setOnClickListener(View.OnClickListener {
                add()
            })
        }
        fun add(){
            i++
            tvCount.text = i.toString()
        }
    }
    

    使用viewmodel,横竖屏数据是不会变为0的

    class MyViewModel :ViewModel() {
    
        private var testNum = MutableLiveData<Int>()
    
        private var i = 0
    
        fun getTestNum():MutableLiveData<Int>{
            return testNum
        }
    
        fun add(){
            i ++
            testNum.value = i
        }
    
    }
    
    class MainActivity : AppCompatActivity() {
        private lateinit var btnAdd:Button
        private lateinit var tvCount: TextView
        val model: MyViewModel by viewModels()
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            btnAdd = findViewById(R.id.btnAdd)
            tvCount = findViewById(R.id.tvCount)
            val model: MyViewModel by viewModels()
            model.getTestNum().observe(this, Observer {
                tvCount.text = it.toString()
            })
            btnAdd.setOnClickListener(View.OnClickListener {
                add()
            })
        }
        fun add(){
            model.add()
            val testNum = model.getTestNum().value
            tvCount.text = testNum.toString()
        }
    }
    

    在 Fragment 之间共享数据

    class MainActivity : AppCompatActivity() {
    
        private val viewModel by lazy {
            ViewModelProvider(this).get(LeftViewModel::class.java)
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main2)
            val btnAdd = findViewById<Button>(R.id.btnAdd)
            btnAdd.setOnClickListener(View.OnClickListener {
                viewModel.add()
            })
        }
    }
    
    class LeftFragment : Fragment() {
    
        private lateinit var tvTest:TextView
    
        private val viewModel by lazy {
            ViewModelProvider(requireActivity()).get(LeftViewModel::class.java)
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.left_fragment, container, false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            tvTest = view.findViewById(R.id.tvTest)
            viewModel.getLiveData().observe(requireActivity()){
                tvTest.text = it.toString()
            }
        }
    
    }
    
    class LeftViewModel : ViewModel() {
    
        private var testLiveData = MutableLiveData<Int>()
    
        private var i = 0
    
        fun getLiveData(): MutableLiveData<Int> {
            return testLiveData
        }
    
        fun add(){
            i ++
            testLiveData.value = i
        }
    
    }
    
    class RightFragment : Fragment() {
    
        private lateinit var tvTest: TextView
    
        private val viewModel by lazy {
            ViewModelProvider(requireActivity()).get(LeftViewModel::class.java)
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_right, container, false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            tvTest = view.findViewById(R.id.tvTest)
            viewModel.getLiveData().observe(requireActivity()){
                tvTest.text = it.toString()
            }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:viewmodel

          本文链接:https://www.haomeiwen.com/subject/cmtxvrtx.html