SkillAgentSearch skills...

ViewBindingEx

ViewBinding with Kotlin Property Delegate and Lifecycle, no memory leak

Install / Use

/learn @guyuuan/ViewBindingEx
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

ViewBindingEx

ViewBinding with Kotlin Property Delegate and Lifecycle, no memory leak

Usage

  1. Add maven central to your project

     repositories {
            ...
            mavenCentral()
            ...
     }
    
  2. Add the dependency in your App module

     dependencies {
          implementation("cn.chitanda:viewbindingex:1.2.0")
     }
    
  3. Activity

    class MainActivity : AppCompatActivity() {
    
        private  val binding by viewBinding(ActivityMainBinding::inflate)
    
        override fun onCreate(savedInstanceState: Bundle?) {
            setContentView(binding.root)
        }
    }
    

    Fragment or DialogFragment

    • Use ViewBinding.inflate()

      class HomeFragment : Fragment() {
      	private val binding by viewBinding { FragmentHomeBinding.inflate(layoutInflater) }
        private val viewModel by viewModels<HomeViewModel>()
       	
        override fun onCreateView(
           inflater: LayoutInflater,
           container: ViewGroup?,
           savedInstanceState: Bundle?): View {
           return binding.root
       	}
      
       	override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
           super.onViewCreated(view, savedInstanceState)
           binding.textHome.text = "hello world"
       	}
        
      }
      
    • Use ViewBinding.bind()

       class HomeFragment : Fragment(R.layout.fragment_home) {
      
      	private val binding by viewBinding (FragmentHomeBinding::bind)
      
      	override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
           super.onViewCreated(view, savedInstanceState)
           binding.button.setOnClickListener {
               HomeDialog().show(childFragmentManager,"dialog")
           }
        }
        
      }
      

Related Skills

View on GitHub
GitHub Stars4
CategoryDevelopment
Updated4y ago
Forks0

Languages

Kotlin

Security Score

70/100

Audited on Apr 3, 2022

No findings