Last active
October 21, 2022 09:16
-
-
Save addeeandra/128faf3c986cfc30ddc8f7a6e3c55b90 to your computer and use it in GitHub Desktop.
Binding Adapter of Spinner, DatePicker, View, TextView, RecyclerView and SwipeRefreshLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.widget.DatePicker | |
import android.widget.Spinner | |
import androidx.databinding.BindingAdapter | |
import androidx.databinding.InverseBindingAdapter | |
import androidx.databinding.InverseBindingListener | |
import java.util.* | |
@BindingAdapter(value = ["selectedDate", "selectedDateAttrChanged"], requireAll = false) | |
fun DatePicker.selectedDate( | |
newSelectedDate: Date?, | |
newDateAttrChanged: InverseBindingListener | |
) { | |
val calendar = newSelectedDate?.let { newDate -> | |
Calendar.getInstance().apply { time = newDate } | |
} ?: Calendar.getInstance() | |
init( | |
calendar.get(Calendar.YEAR), | |
calendar.get(Calendar.MONTH), | |
calendar.get(Calendar.DAY_OF_MONTH) | |
) { _, _, _, _ -> newDateAttrChanged.onChange() } | |
} | |
@InverseBindingAdapter(attribute = "selectedDate", event = "selectedDateAttrChanged") | |
fun DatePicker.selectedDateAttrChanged(): Date { | |
return Calendar.getInstance().apply { | |
set(year, month, dayOfMonth) | |
}.time | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.widget.ImageView | |
import androidx.databinding.BindingAdapter | |
import com.squareup.picasso.Picasso | |
@BindingAdapter("srcLink") | |
fun ImageView.appSrcLink(url: String? = null) { | |
val loadInstance = if (url != null && url.isNotBlank()) { | |
Picasso.get().load(url).error(R.drawable.img_no) // R.drawable.img_no is a placeholder image | |
} else { | |
Picasso.get().load(R.drawable.img_no) | |
} | |
when { | |
this.scaleType == ImageView.ScaleType.FIT_CENTER -> loadInstance.centerInside().fit() | |
else -> loadInstance.centerCrop().fit() | |
} | |
loadInstance.into(this) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.databinding.BindingAdapter | |
import androidx.recyclerview.widget.RecyclerView | |
interface BindableModel | |
interface BindableAdapter<T : BindableModel> { | |
fun submitList(data: List<T>) | |
} | |
@BindingAdapter("items") | |
fun <T : BindableModel> RecyclerView.setItemAdapter(items: List<T>?) { | |
if (adapter != null && adapter is BindableAdapter<*>) { | |
(adapter as BindableAdapter<BindableModel>).submitList(items ?: listOf()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View | |
import android.widget.AdapterView | |
import android.widget.ArrayAdapter | |
import android.widget.Spinner | |
import androidx.databinding.BindingAdapter | |
import androidx.databinding.InverseBindingAdapter | |
import androidx.databinding.InverseBindingListener | |
@BindingAdapter("entries") | |
fun Spinner.entries(values: List<String>) { | |
adapter = ArrayAdapter<String>( | |
context, | |
android.R.layout.simple_spinner_dropdown_item, | |
android.R.id.text1, | |
values | |
) | |
} | |
@BindingAdapter(value = ["selectedValue", "selectedValueAttrChanged"], requireAll = false) | |
fun Spinner.selectedValue( | |
newSelectedValue: String?, | |
newTextAttrChanged: InverseBindingListener | |
) { | |
onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | |
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { | |
newTextAttrChanged.onChange() | |
} | |
override fun onNothingSelected(parent: AdapterView<*>) {} | |
} | |
if (newSelectedValue != null) { | |
val pos = (adapter as ArrayAdapter<String>).getPosition(newSelectedValue) | |
setSelection(pos, true) | |
} | |
} | |
@InverseBindingAdapter(attribute = "selectedValue", event = "selectedValueAttrChanged") | |
fun Spinner.selectedValueAttrChanged(): String { | |
return this.selectedItem as String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.databinding.BindingAdapter | |
import androidx.databinding.InverseBindingAdapter | |
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout | |
@BindingAdapter("onRefresh") | |
fun SwipeRefreshLayout.swipeRefresh(callback: Runnable) { | |
setOnRefreshListener { | |
callback.run() | |
} | |
} | |
@BindingAdapter("onRefresh") | |
fun SwipeRefreshLayout.swipeRefresh(callback: Runnable) { | |
setOnRefreshListener { | |
callback.run() | |
} | |
} | |
@BindingAdapter(value = ["loading", "loadingAttrChanged"], requireAll = false) | |
fun SwipeRefreshLayout.loadingState( | |
isLoading: Boolean, | |
loadingAttrChanged: InverseBindingListener | |
) { | |
if (isRefreshing != isLoading) { | |
isRefreshing = isLoading | |
loadingAttrChanged.onChange() | |
} | |
} | |
@InverseBindingAdapter(attribute = "loading", event = "loadingAttrChanged") | |
fun SwipeRefreshLayout.onLoadingStateChange(): Boolean = isRefreshing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.widget.TextView | |
import androidx.annotation.ColorRes | |
import androidx.annotation.DrawableRes | |
import androidx.core.content.ContextCompat | |
import androidx.databinding.BindingAdapter | |
@BindingAdapter("customBackground") | |
fun TextView.background(@DrawableRes resource: Int) { | |
background = ContextCompat.getDrawable(context, resource) | |
} | |
@BindingAdapter("customTextColor") | |
fun TextView.textColor(@ColorRes resource: Int) { | |
setTextColor(ContextCompat.getColor(context, resource)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View | |
import androidx.databinding.BindingAdapter | |
@BindingAdapter("visible") | |
fun View.visible(visible: Boolean) { | |
visibility = if (visible) View.VISIBLE else View.GONE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment