Git Product home page Git Product logo

view-binding's Introduction

View Binding

This is an Android library. It makes your life easier by reducing boilerplate code and avoiding redundant lines of code when are using the ViewBinding feature on your Android project. This library show up in my head as an very good example on how the property delegation functionality from kotlin can help us.



Usage on Activity

Binding

Yes, the viewBinding() method will bind to the root view for you.

class MainActivity : AppCompatActivity(R.layout.activity_main) {

    private val binding : ActivityMainBinding by viewBinding()
}
Inflating

You don't need to set the layout calling setContentView(). It will be done automatically.

class MainActivity : AppCompatActivity() {

    private val binding : ActivityMainBinding by viewBinding(ViewBindingMethod.INFLATE)
    
}

Usage on Fragment

Binding

The viewBinding() method will track the fragment view lifecycle and destroy the ViewBinding instance for you.

class MyFragment : Fragment(R.layout.fragment_layout) {

    private val binding by viewBinding<FragmentLayoutBinding>()
}
Inflating

Using this way, it gives you the onCreateView

class MyFragment : Fragment() {

    private val binding by viewBinding<FragmentLayoutBinding>(ViewBindingMethod.INFLATE)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = binding.root
}

Installation

Add the following dependency to your build.gradle file:

dependencies {
    implementation 'com.github.jrvansuita:view-binding:1.0.2'
}

Don't forget to add the JitPack maven repository to the list of repositories: maven { url "https://jitpack.io" }

view-binding's People

Contributors

jrvansuita avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.