Giter Club home page Giter Club logo

bindingextension's Introduction

BindingExtension

CircleCI jitpack

ViewBinding is an amazing tool for Android but it's not so fit in Android development as we still have to do some config. BindingExtension is built to provide a simpler usage.

Install

Add Jitpack repository to your root build.grable:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Then add dependency in your module build.gradle:

dependencies {
  implementation 'com.github.jintin:BindingExtension:3.1.0'
}

Usage

First of all, BindingExtension using refelction a lot to link many things internally in order to provide simple usage. To prevent having trouble with proguard, please remember to exclude ViewBinding related method in your proguard-rules file like this:

-keepclassmembers class * implements androidx.viewbinding.ViewBinding {
     public static ** inflate(...);
}

Activity

Extend from BindingActivity with your ViewBinding type then you can use binding directly after calling super.onCreate(savedInstanceState) and you don't have to call setContentView anymore:

class MainActivity : BindingActivity<ActivityMainBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.label.setText(R.string.activity_label)
    }
}

Fragment

Extend from BindingFragment with your ViewBinding type then you can use binding directly after super.onCreateView(inflater, container, savedInstanceState) is called:

class MainFragment : BindingFragment<FragmentMainBinding>() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.button.setOnClickListener {
            binding.button.setText(R.string.fragment_label)
        }
    }
}

ViewHolder

BindingExtension provide an extension function for ViewGroup, you can call ViewGroup.toBinding() in onCreateViewHolder to get your desire type of ViewBinding. And ViewHolder can than access ViewBinding directly without further transformation.

// inside adapter
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return MainViewHolder(parent.toBinding())
}

// ViewHolder
class MainViewHolder(private val binding: AdapterMainBinding) :
    RecyclerView.ViewHolder(binding.root) {

    fun bind(data: String) {
        binding.name.text = data
    }
}

You can go to ./app module for more information.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Jintin/BindingExtension.

Buy Me A Coffee

bindingextension's People

Contributors

jintin avatar

Stargazers

 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.