Skip to content

Commit

Permalink
support databinding
Browse files Browse the repository at this point in the history
  • Loading branch information
donniesky committed Jul 31, 2017
1 parent e7083ae commit 8b968df
Show file tree
Hide file tree
Showing 22 changed files with 741 additions and 10 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ android {
preDexLibraries = preDexEnabled && !isCi
}

dataBinding {
enabled = true
}

lintOptions {
abortOnError false
}
Expand Down
37 changes: 31 additions & 6 deletions app/src/main/java/me/donnie/app/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.donnie.app;

import android.databinding.ObservableArrayList;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
Expand All @@ -10,23 +11,30 @@
import java.util.ArrayList;
import java.util.List;

import me.donnie.adapter.BaseAdapter;
import me.donnie.adapter.BaseViewHolder;
import me.donnie.adapter.databinding.BaseBindingAdapter;
import me.donnie.adapter.wrapper.Walle;
import me.donnie.app.databinding.StringViewModel;
import me.donnie.app.databinding.TextMultiDbAdapter;

public class MainActivity extends AppCompatActivity implements Walle.OnLoadMoreListener {

private RecyclerView rv;

private Test2Adapter adapter;
//private Test2Adapter adapter;

//private TestBindingAdapter mAdapter;

private TextMultiDbAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
adapter = new Test2Adapter();
//adapter = new Test2Adapter();
mAdapter = new TextMultiDbAdapter();

Walle walle = Walle.newBuilder()
.enableHeader(true)
Expand All @@ -36,19 +44,36 @@ protected void onCreate(Bundle savedInstanceState) {
.enableLoadMore(true)
.loadmoreRes(R.layout.view_load_more)
.addLoadMoreListener(this)
.wrapperAdapter(adapter).build();
.wrapperAdapter(mAdapter).build();
rv.setAdapter(walle.getWrapperAdapter());
//rv.setAdapter(mAdapter);

adapter.setData(getdatas());
mAdapter.setData(getViewModels());

adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
mAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, BaseViewHolder holder, int position) {
Toast.makeText(MainActivity.this, "点击"+(position - 1), Toast.LENGTH_SHORT).show();
}
});
}

private ObservableArrayList<StringViewModel> getViewModels() {
ObservableArrayList<StringViewModel> models = new ObservableArrayList<>();
for (int i = 0; i < 30; i++) {
String txt = "";
if (i == 2) {
txt = ""+2;
} else if (i == 3) {
txt = ""+3;
} else {
txt = "This is Test -- "+i;
}
models.add(new StringViewModel(txt));
}
return models;
}

private List<String> getdatas() {
List<String> data = new ArrayList<>();
for (int i = 0; i < 30 ; i++) {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/me/donnie/app/databinding/ItemDb1Delegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.donnie.app.databinding;

import me.donnie.adapter.databinding.delegate.ItemViewDelegate;
import me.donnie.app.R;

/**
* @author donnieSky
* @created_at 2017/7/31.
* @description
*/

public class ItemDb1Delegate implements ItemViewDelegate<StringViewModel, me.donnie.app.databinding.ViewDbItem1Binding> {
@Override
public int getItemViewLayoutId() {
return R.layout.view_db_item_1;
}

@Override
public boolean isForViewType(StringViewModel item, int position) {
return item.getTxt().equals("2");
}

@Override
public void convert(me.donnie.app.databinding.ViewDbItem1Binding binding, StringViewModel stringViewModel, int position) {
binding.setViewModel(stringViewModel);
binding.executePendingBindings();
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/me/donnie/app/databinding/ItemDb2Delegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.donnie.app.databinding;

import me.donnie.adapter.databinding.delegate.ItemViewDelegate;
import me.donnie.app.R;

/**
* @author donnieSky
* @created_at 2017/7/31.
* @description
*/

public class ItemDb2Delegate implements ItemViewDelegate<StringViewModel, me.donnie.app.databinding.ViewDbItem2Binding> {
@Override
public int getItemViewLayoutId() {
return R.layout.view_db_item_2;
}

@Override
public boolean isForViewType(StringViewModel item, int position) {
return item.getTxt().equals("3");
}

@Override
public void convert(me.donnie.app.databinding.ViewDbItem2Binding binding, StringViewModel stringViewModel, int position) {
binding.setViewModel(stringViewModel);
binding.executePendingBindings();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.donnie.app.databinding;

import me.donnie.adapter.databinding.delegate.ItemViewDelegate;
import me.donnie.app.R;

/**
* @author donnieSky
* @created_at 2017/7/31.
* @description
*/

public class ItemDbDefaultDelegate implements ItemViewDelegate<StringViewModel, ViewDbItemBinding> {
@Override
public int getItemViewLayoutId() {
return R.layout.view_db_item;
}

@Override
public boolean isForViewType(StringViewModel item, int position) {
return !item.getTxt().equals("2") &&
!item.getTxt().equals("3");
}

@Override
public void convert(ViewDbItemBinding binding, StringViewModel stringViewModel, int position) {
binding.setViewModel(stringViewModel);
binding.executePendingBindings();
}
}
24 changes: 24 additions & 0 deletions app/src/main/java/me/donnie/app/databinding/StringViewModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.donnie.app.databinding;

import android.databinding.BaseObservable;
import android.databinding.Bindable;

/**
* @author donnieSky
* @created_at 2017/7/31.
* @description
*/

public class StringViewModel extends BaseObservable {

private String txt;

public StringViewModel(String txt) {
this.txt = txt;
}

@Bindable
public String getTxt() {
return txt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.donnie.app.databinding;

import me.donnie.adapter.BaseViewHolder;
import me.donnie.adapter.databinding.BaseBindingAdapter;
import me.donnie.app.R;

/**
* @author donnieSky
* @created_at 2017/7/31.
* @description
*/

public class TestBindingAdapter extends BaseBindingAdapter<StringViewModel, ViewDbItemBinding, BaseViewHolder> {

public TestBindingAdapter() {
super(R.layout.view_db_item);
}

/*@Override
protected void convert(BaseBindHolder<ViewDbItemBinding> holder, StringViewModel model, int position) {
StringViewModel viewModel = getItem(position);
ViewDbItemBinding itemBinding = holder.binding;
itemBinding.setViewModel(viewModel);
itemBinding.executePendingBindings();
}*/

@Override
protected void convert(ViewDbItemBinding binding, StringViewModel stringViewModel, int position) {
binding.setViewModel(stringViewModel);
binding.executePendingBindings();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.donnie.app.databinding;

import me.donnie.adapter.BaseViewHolder;
import me.donnie.adapter.databinding.MultiItemBindingAdapter;

/**
* @author donnieSky
* @created_at 2017/7/31.
* @description
*/

public class TextMultiDbAdapter extends MultiItemBindingAdapter<StringViewModel, BaseViewHolder> {

public TextMultiDbAdapter() {
super(null);

addItemViewDelegate(new ItemDbDefaultDelegate());
addItemViewDelegate(new ItemDb1Delegate());
addItemViewDelegate(new ItemDb2Delegate());
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.donnie.app.MainActivity"/>
tools:context="me.donnie.app.MainActivity"/>
38 changes: 38 additions & 0 deletions app/src/main/res/layout/view_db_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="viewModel"
type="me.donnie.app.databinding.StringViewModel"/>
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="20dp">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>

<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="@{viewModel.txt}"
android:textColor="@android:color/black"
android:textSize="20sp"/>


</LinearLayout>
</layout>
36 changes: 36 additions & 0 deletions app/src/main/res/layout/view_db_item_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="viewModel"
type="me.donnie.app.databinding.StringViewModel"/>
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="20dp">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"/>

<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="@{viewModel.txt}"
android:textColor="@android:color/black"
android:textSize="20sp"/>

</LinearLayout>
</layout>
32 changes: 32 additions & 0 deletions app/src/main/res/layout/view_db_item_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="viewModel"
type="me.donnie.app.databinding.StringViewModel"/>
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="20dp">

<TextView
android:id="@+id/txt3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="@{viewModel.txt}"
android:textColor="@android:color/white"
android:textSize="30sp"/>

</LinearLayout>
</layout>
4 changes: 4 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ android {
abortOnError false
}

dataBinding {
enabled = true
}

defaultConfig {
minSdkVersion 14
targetSdkVersion 25
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/me/donnie/adapter/BaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public int getItemCount() {
return mData.size();
}

protected abstract void convert(BaseViewHolder holder, T t, int position);
protected abstract void convert(VH holder, T t, int position);

protected void setListener(final ViewGroup parent, final BaseViewHolder viewHolder, int viewType) {
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
Expand Down
Loading

0 comments on commit 8b968df

Please sign in to comment.