All Projects → silmood → Bindapter

silmood / Bindapter

You will never build an adapter again

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Bindapter

Multiitem
一个优雅的实现多类型的RecyclerView类库 支持DataBinding Form表单录入 跨多个RecyclerView拖动
Stars: ✭ 381 (+1905.26%)
Mutual labels:  adapter, databinding
Noviewholder
No ViewHolder, No Adapter!!! 基于反射实现 DATA 和 VIEW 的绑定,不知 ViewHolder 为何物。
Stars: ✭ 41 (+115.79%)
Mutual labels:  adapter, databinding
Vrplayer
VRPlayer is a simple but powerful local VR video player
Stars: ✭ 406 (+2036.84%)
Mutual labels:  databinding
Multityperecyclerviewadapter
一个专注于RecyclerView优雅刷新(接管资源和数据源)、高灵活、低耦合、健壮性以及高效性的MVP模式库,支持大多数Adapter
Stars: ✭ 763 (+3915.79%)
Mutual labels:  adapter
Vue Socket.io Extended
✌️⚡️ Socket.io bindings for Vue.js and Vuex (inspired by Vue-Socket.io)
Stars: ✭ 506 (+2563.16%)
Mutual labels:  adapter
Gimx
The GIMX software.
Stars: ✭ 421 (+2115.79%)
Mutual labels:  adapter
Archapp
Simple Android app to show how to design a multi-modules MVVM Android app (fully tested)
Stars: ✭ 551 (+2800%)
Mutual labels:  databinding
Autoinch
优雅的iPhone全尺寸/等比例精准适配工具
Stars: ✭ 395 (+1978.95%)
Mutual labels:  adapter
Loxodon Framework
An MVVM & Databinding framework that can use C# and Lua to develop games
Stars: ✭ 802 (+4121.05%)
Mutual labels:  databinding
Espressif
all espressif stuff will committed here
Stars: ✭ 477 (+2410.53%)
Mutual labels:  adapter
Jetpack Mvvm Best Practice
是 难得一见 的 Jetpack MVVM 最佳实践!在 以简驭繁 的代码中,对 视图控制器 乃至 标准化开发模式 形成正确、深入的理解!
Stars: ✭ 6,950 (+36478.95%)
Mutual labels:  databinding
Instant Weather
An Android weather application implemented using the MVVM pattern, Retrofit2, Dagger2, LiveData, ViewModel, Coroutines, Room, Navigation Components, Data Binding and some other libraries from the Android Jetpack.
Stars: ✭ 473 (+2389.47%)
Mutual labels:  databinding
Mvvmarms
Android MVVM Architecture Components based on MVPArms and Android Architecture Components.
Stars: ✭ 425 (+2136.84%)
Mutual labels:  databinding
Superadapter
[Deprecated]. 🚀 Adapter(BaseAdapter, RecyclerView.Adapter) wrapper for Android. 一个Adapter同时适用RecyclerView、ListView、GridView等。
Stars: ✭ 638 (+3257.89%)
Mutual labels:  adapter
Wanandroid
🏄 基于Architecture Components dependencies (Lifecycles,LiveData,ViewModel,Room)构建的WanAndroid开源项目。 你值得拥有的MVVM快速开发框架:https://github.com/jenly1314/MVVMFrame
Stars: ✭ 410 (+2057.89%)
Mutual labels:  databinding
Lastadapter
Don't write a RecyclerView adapter again. Not even a ViewHolder!
Stars: ✭ 777 (+3989.47%)
Mutual labels:  databinding
Mvvm Juejin
高仿"掘金Android App": databinding + kotlin + rx 的优雅实践。(持续打磨中~)
Stars: ✭ 403 (+2021.05%)
Mutual labels:  databinding
Mongo Php Adapter
🔗 Adapter to provide ext-mongo interface on top of mongo-php-library
Stars: ✭ 453 (+2284.21%)
Mutual labels:  adapter
Markupkit
Declarative UI for iOS and tvOS
Stars: ✭ 508 (+2573.68%)
Mutual labels:  databinding
Marvelheroes
❤️ A sample Marvel heroes application based on MVVM (ViewModel, Coroutines, LiveData, Room, Repository, Koin) architecture.
Stars: ✭ 826 (+4247.37%)
Mutual labels:  databinding

Bindapter

You will never build an adapter again

Bindapter use the power of DataBinding to build a flexible RecyclerView.Adapter which will save you a lot of time.

Bindapter is totally built with Kotlin + Love 💖.

Usage

Only create your view using databinding

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="item"
            type="com.example.app.Item"/>
    </data>

   <TextView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@{item.title}"/>
</layout>

Then create your Bindapter

val adapter = Bindapter<ItemModel>(R.layout.item_view, BR.item)

And then set it to your RecyclerView

recyclerView.adapter = adapter

As simple as that, now you can spend your time on more important things

Setup

  1. Active DataBinding in your project build.gradle
android {
    dataBinding {
        enabled true
    }
}

1.1 If you are using Kotlin - Configure Databinding Compiler with Kapt

apply plugin: 'kotlin-kapt'

dependencies {
  kapt 'com.android.databinding:compiler:3.0.1'
}
  1. Configure maven url in your root build.gradle This one is temporal 😄
allProjects {
    repositories {
        maven { url  "http://dl.bintray.com/silmood/bindapter" }
    }
}
  1. Import Bindapter

Gradle 3.X.X

implementation 'com.silmood.bindapter:bindapter:0.1.1'

Gradle 2.X.X

compile 'com.silmood.bindapter:bindapter:0.1.1'

Listening Events

If you want to add interaction in your views you just have to add a Handler (or whatever you like name it) class when you create a Bindapter:

Kotlin

class Handler {
    fun onClick(view: View) { ... }
    fun onClickImage(view: View, item: ItemModel) { ... }
}

val bindapter = Bindapter<ItemModel>(R.layout.item,
                    itemVariableId = BR.item,
                    handler = Handler(),
                    handlerId = BR.handler)

Java

public class Handler {
    public void onClick(View view) { ... }
    public void onClickImage(View view, ItemModel item) { ... }
}

Bindapter<ItemModel> bindapter = new Bindapter<>(R.layout.item, 
                                        BR.item, 
                                        new Handler(), 
                                        BR.handler);

This way you can use it in your layout.

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="item"
            type="com.example.app.Item"/>
        <variable 
            name="handler" 
            type="com.example.app.Handler"/>
    </data>

   <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="@{handler::onClick}"
        app:imgUrl="@{item.image}"/>
</layout>

If you need to receive the model when an item is clicked, you can implement it this way:

   <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="@{(view) -> handler.onClickImage(view, item)}"
        app:imgUrl="@{item.image}"/>

More info about here

Binding Adapters

A binding adapter is a way to define custom behaviors to set properties in a view. For example, load images with Glide/Picasso or set TextView's color according to a model property. More info

Kotlin

The sample app contains a file named BindingAdapters.kt. This is the way you have to define them; just creating Kotlin file an put the functions there.

@BindingAdapter("imgUrl")
fun loadImage(imageView: ImageView, url: String) {
    Glide.with(imageView.context)
            .load(url)
            .into(imageView)
}

@BindingAdapter("ratingColor")
fun ratingColor(textView: TextView, ratingPercent: String) {
    val percantage = ratingPercent.toInt()
    val context = textView.context

    val color = when {
        percantage > 95 -> ContextCompat.getColor(context, R.color.positive_1)
        percantage > 90 -> ContextCompat.getColor(context, R.color.positive_2)
        else -> ContextCompat.getColor(context, R.color.negative)
    }

    textView.setTextColor(color)
}

Java

If you are using Java you just have to define your binding adapters as static methods.

@BindingAdapter({"bind:imgUrl"})
public static void loadImage(ImageView view, String url) {
   Picasso.with(view.getContext()).load(url).into(view);
}

Once it is declared you can use it in your layout:

   <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:imgUrl="@{item.image}"/>

FAQ

  • My BR. class/variables are missing ¿What can i do?

The BR class isn't correctly generated yet. After define your layout you have to build again your project

  • There is a problem building the project: 'Internal compiler error'

It's possible you have commit a mistake in your layout file defining types with Data Binding. Check your gradle console to know where exactly is the problem.

Code Style

This project is written on Kotlin and uses ktlint. If you find that one of your pull reviews does not pass the CI server check due to a code style conflict, you can easily fix it by running: ./gradlew :bindapter:ktlintFormat, or running IntelliJ/Android Studio's code formatter.

TODO

  • Support multiple view types for different models
  • Support multiple view types for the same model
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].