Android-Ktx

Offer various extensions to common Android methods to improve development efficiency (welcome Star and Pull Request) GitHub address: github.com/ShowMeThe/A…

How to rely on:

Add what you need in build.gradle dependencies:

implementation 'com. Making. ShowMeThe: binding - KTX: 1.0.1' // One line of code implements ViewBinding and DataBinding
implementation 'com. Making. ShowMeThe: span - KTX: 1.0.0' // SpannableStringBuilder extends fast writing
implementation 'com. Making. ShowMeThe: livedata - KTX: 1.0.0' // The LiveData extension operator
Copy the code

Main contents:

One line of code implements the use of ViewBinding and DataBinding, and adds lifecycle listening (ViewBinding uses reflection, Have been added confusion) 2, 3 to expand quickly write SpannableStringBuilder, LiveData operatorCopy the code

Example:

One line of code implements ViewBinding and DataBinding


class MainActivity : AppCompatActivity() {

    var binding by BindingProperty<ActivityMainBinding>()

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        binding = binding(this,R.layout.activity_main)

        binding.tv1.setOnClickListener {
            Toast.makeText(this."Click Event",Toast.LENGTH_LONG).show()
        }

    }
}

Copy the code

The function of the BindingProperty is to listen for the lifetime of a Lifeowner scope. For example, if the DataBinding writes Livedata directly to the layout, it needs to pass in the Lifeowner or use the following:

class MainActivity : AppCompatActivity() {

    val binding by lazy { binding<ActivityMainBinding>(this,R.layout.activity_main) }

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


        binding.tv1.setOnClickListener {
            Toast.makeText(this."Click Event",Toast.LENGTH_LONG).show()
        }


    }
}
Copy the code

Notes:

1, binding method for ComponentActivity, whether ViewBinding or DataBinding setContentView operation, pay attention to the repeated input problemCopy the code

2. SpannableStringBuilder extension

The template code is as follows:

class MainActivity : AppCompatActivity() {

    val binding by lazy { binding<ActivityMainBinding>(this,R.layout.activity_main) }

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


        val text = "Test content to demonstrate Spannable"binding.apply { tv1.movementMethod = LinkMovementMethod.getInstance() tv1.setHintTextColor(Color.TRANSPARENT) tv1.text =  text.span.apply { foregroundColor(Color.RED,0.7.)
                italic(0.2.)
                backgroundColor(Color.LTGRAY,0.3.)
                underline(0.2.)
                strikeThrough(6.9.)
                bold(2.4.)
                boldItalic(6.9.)
                size(25.0.1.)
                sizeRelative(2f.1.2.)
                val drawable = ContextCompat.getDrawable(this@MainActivity,R.mipmap.ic_launcher_round) drawable!! .setBounds(0.0,drawable.intrinsicHeight,drawable.intrinsicWidth)
                image(drawable,0.1.)
                click(10.12.){
                    Toast.makeText(this@MainActivity, text,Toast.LENGTH_LONG).show()
                }
            }

        }


    }
}
Copy the code

Effect:

3. LiveData extension operator

Use MediatorLiveData to achieve the transformation operation, support filter, map, distinctUntilChanged, debounce

            vallive = MutableLiveData<String? >() live.filter { ! it.isNullOrEmpty() } .map { StringBuilder("Data:").append(it).toString() }
                .debounce(500)
                .observe(this@MainActivity) {
                    Toast.makeText(this@MainActivity, it, Toast.LENGTH_LONG).show()
                    Log.e("MainActivity", it)
                }

Copy the code