Last active
April 26, 2019 08:01
-
-
Save a11n/16ffe69d597ecbf2ef9e to your computer and use it in GitHub Desktop.
Android bidirectional data-binding
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
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
> | |
<data> | |
<import type="your.package.name.Variables"/> | |
<variable | |
name="name" | |
type="String" | |
/> | |
</data> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
> | |
<TextView | |
android:text="@{@string/hello_world(name)}" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
/> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:bind="@{Variables.name}" | |
/> | |
</LinearLayout> | |
</layout> |
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
public class DataBinding { | |
@android.databinding.BindingAdapter("bind") | |
public static void addTextChangedListener(EditText view, final int variable) { | |
final ViewDataBinding binding = DataBindingUtil.findBinding(view); | |
view.addTextChangedListener(new TextWatcher() { | |
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { | |
binding.setVariable(variable, s.toString()); | |
binding.executePendingBindings(); | |
} | |
@Override public void afterTextChanged(Editable s) { | |
} | |
}); | |
} | |
} |
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
<resources> | |
<string name="hello_world">Hello %s!</string> | |
</resources> |
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
/** | |
* This class is used for dynamic binding. | |
* | |
* The generated BR class could not be used in layouts directly. | |
* This class acts as wrapper in order to used binding ids in layouts. | |
*/ | |
public class Variables { | |
public final static int name = BR.name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please post the your mainactivity class of this project...