Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
buildToolsVersion '25.0.0'

defaultConfig {
applicationId "net.droidlabs.mvvmdemo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
import net.droidlabs.mvvmdemo.viewmodel.SuperUserViewModel;
import net.droidlabs.mvvmdemo.viewmodel.UserViewModel;

public class SuperUserBinder extends ConditionalDataBinder<UserViewModel>
{
public SuperUserBinder(int bindingVariable, int layoutId)
{
super(bindingVariable, layoutId);
public class SuperUserBinder extends ConditionalDataBinder<UserViewModel> {

public SuperUserBinder(int bindingVariable, int layoutId) {
super(bindingVariable, layoutId, null);
}

@Override
public boolean canHandle(UserViewModel model)
{
public boolean canHandle(UserViewModel model) {
return model instanceof SuperUserViewModel;
}
}
11 changes: 4 additions & 7 deletions app/src/main/java/net/droidlabs/mvvmdemo/binder/UserBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import net.droidlabs.mvvm.recyclerview.adapter.binder.ConditionalDataBinder;
import net.droidlabs.mvvmdemo.viewmodel.UserViewModel;

public class UserBinder extends ConditionalDataBinder<UserViewModel>
{
public UserBinder(int bindingVariable, int layoutId)
{
super(bindingVariable, layoutId);
public class UserBinder extends ConditionalDataBinder<UserViewModel> {
public UserBinder(int bindingVariable, int layoutId) {
super(bindingVariable, layoutId, null);
}

@Override
public boolean canHandle(UserViewModel model)
{
public boolean canHandle(UserViewModel model) {
return true;
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/net/droidlabs/mvvmdemo/model/Group.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.droidlabs.mvvmdemo.model;

public class Group {

public String name;
public String id;

public Group(String name, String id) {
this.name = name;
this.id = id;
}
}
65 changes: 35 additions & 30 deletions app/src/main/java/net/droidlabs/mvvmdemo/view/UsersView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.droidlabs.mvvmdemo.view;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
Expand All @@ -9,35 +11,38 @@
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import net.droidlabs.mvvm.recyclerview.adapter.ClickHandler;
import net.droidlabs.mvvm.recyclerview.adapter.LongClickHandler;
import net.droidlabs.mvvm.recyclerview.adapter.binder.CompositeItemBinder;
import net.droidlabs.mvvm.recyclerview.adapter.binder.ItemBinder;
import net.droidlabs.mvvm.recyclerview.adapter.binder.ItemBinderBase;
import net.droidlabs.mvvmdemo.BR;
import net.droidlabs.mvvmdemo.R;
import net.droidlabs.mvvmdemo.binder.SuperUserBinder;
import net.droidlabs.mvvmdemo.binder.UserBinder;
import net.droidlabs.mvvmdemo.databinding.UsersViewBinding;
import net.droidlabs.mvvmdemo.model.Group;
import net.droidlabs.mvvmdemo.model.User;
import net.droidlabs.mvvmdemo.viewmodel.SuperUserViewModel;
import net.droidlabs.mvvmdemo.viewmodel.UserViewModel;
import net.droidlabs.mvvmdemo.viewmodel.UsersViewModel;

public class UsersView extends AppCompatActivity
{
import java.util.HashMap;

public class UsersView extends AppCompatActivity {
private UsersViewModel usersViewModel;
private UsersViewBinding binding;
private boolean useCompositeBinder = false;

@Nullable
private static String getStringFromEditText(EditText editText)
{
private static String getStringFromEditText(EditText editText) {
Editable editable = editText.getText();
return editable == null ? null : editable.toString();
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
usersViewModel = new UsersViewModel();
usersViewModel.users.add(new SuperUserViewModel(new User("Android", "Dev")));
Expand All @@ -48,47 +53,47 @@ protected void onCreate(Bundle savedInstanceState)
binding.activityUsersRecycler.setLayoutManager(new LinearLayoutManager(this));
}

public View.OnClickListener onButtonClick()
{
return new View.OnClickListener()
{
public View.OnClickListener onButtonClick() {
return new View.OnClickListener() {
@Override
public void onClick(View v)
{
public void onClick(View v) {
usersViewModel.addUser(getStringFromEditText(binding.usersViewFirstname), getStringFromEditText(binding.usersViewLastname));
}
};
}

public ClickHandler<UserViewModel> clickHandler()
{
return new ClickHandler<UserViewModel>()
{
public ClickHandler<UserViewModel> clickHandler() {
return new ClickHandler<UserViewModel>() {
@Override
public void onClick(UserViewModel user)
{
public void onClick(UserViewModel user) {
Toast.makeText(UsersView.this, user.getFirstName() + " " + user.getLastName(), Toast.LENGTH_SHORT).show();
}
};
}

public LongClickHandler<UserViewModel> longClickHandler()
{
return new LongClickHandler<UserViewModel>()
{
public LongClickHandler<UserViewModel> longClickHandler() {
return new LongClickHandler<UserViewModel>() {
@Override
public void onLongClick(UserViewModel user)
{
public void onLongClick(UserViewModel user) {
Toast.makeText(UsersView.this, "LONG CLICK: " + user.getFirstName() + " " + user.getLastName(), Toast.LENGTH_SHORT).show();
}
};
}

public ItemBinder<UserViewModel> itemViewBinder()
{
return new CompositeItemBinder<UserViewModel>(
new SuperUserBinder(BR.user, R.layout.item_super_user),
new UserBinder(BR.user, R.layout.item_user)
);

public ItemBinder<UserViewModel> itemViewBinder() {
if (useCompositeBinder) {
return new CompositeItemBinder<>(
new SuperUserBinder(BR.user, R.layout.item_super_user),
new UserBinder(BR.user, R.layout.item_user)
);
} else {

return new ItemBinderBase<>(BR.user, R.layout.item_user, new HashMap<Integer, Object>() {
{
put(BR.group, new Group("TestGroup", "1"));
}
});
}
}
}
24 changes: 12 additions & 12 deletions app/src/main/res/layout/item_super_user.xml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

<variable
name="user"
type="net.droidlabs.mvvmdemo.viewmodel.SuperUserViewModel"/>
type="net.droidlabs.mvvmdemo.viewmodel.SuperUserViewModel" />

</data>

<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#EEE"
android:layout_margin="10dp">
android:orientation="horizontal">



<TextView
android:id="@+id/item_user_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/item_user_name"
android:text='@{String.format("%s &amp; %s", user.firstName, user.group)}'
tools:text="Hello"/>
tools:text="Hello" />

<TextView
android:id="@+id/item_user_surname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/item_user_surname"
android:text="@{user.lastName}"
tools:text="World"
/>
tools:text="World" />
</LinearLayout>
</layout>
11 changes: 11 additions & 0 deletions app/src/main/res/layout/item_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<variable
name="user"
type="net.droidlabs.mvvmdemo.viewmodel.UserViewModel"/>


<variable
name="group"
type="net.droidlabs.mvvmdemo.model.Group" />
</data>

<LinearLayout
Expand All @@ -17,6 +22,12 @@
android:background="#CCC"
android:layout_margin="10dp">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@{group.name}" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:3.0.0-alpha6'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 10 15:27:10 PDT 2013
#Wed Jul 12 13:00:20 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-milestone-1-all.zip
2 changes: 1 addition & 1 deletion recyclerview-binding/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
buildToolsVersion '25.0.0'

defaultConfig {
minSdkVersion 7
Expand Down
Loading