Commit cfb6f42e66afe41c9cb1e10752118da45121c4c9
1 parent
70670522
Add the Adapter Databinding test
Showing
10 changed files
with
294 additions
and
27 deletions
app/build.gradle
| ... | ... | @@ -36,11 +36,11 @@ dependencies { |
| 36 | 36 | exclude group: 'com.android.support', module: 'support-annotations' |
| 37 | 37 | }) |
| 38 | 38 | compile 'com.android.support:appcompat-v7:26.+' |
| 39 | + compile 'com.android.support:recyclerview-v7:26.+' | |
| 39 | 40 | compile 'com.android.support.constraint:constraint-layout:1.0.2' |
| 40 | 41 | testCompile 'junit:junit:4.12' |
| 41 | - compile 'com.android.support.constraint:constraint-layout:1.0.2' | |
| 42 | 42 | // TODO 本地酷使用说明 代码仓库标识 |
| 43 | 43 | // compile 'com.cnlive.libs:base:1.0.0@aar' |
| 44 | -// compile 'com.cnlive:cnlive:0.1.1' | |
| 45 | - compile project(':cnlive') | |
| 44 | + compile 'com.cnlive:cnlive:0.2.0' | |
| 45 | +// compile project(':cnlive') | |
| 46 | 46 | } | ... | ... |
app/src/main/java/com/cnlive/libs/demo/DataBindingRecyclerAdapter.java
0 → 100644
| 1 | +package com.cnlive.libs.demo; | |
| 2 | + | |
| 3 | +import android.databinding.DataBindingUtil; | |
| 4 | +import android.databinding.ViewDataBinding; | |
| 5 | +import android.support.v7.widget.RecyclerView; | |
| 6 | +import android.view.LayoutInflater; | |
| 7 | +import android.view.View; | |
| 8 | +import android.view.ViewGroup; | |
| 9 | + | |
| 10 | +import com.cnlive.libs.base.frame.ReflectUtil; | |
| 11 | + | |
| 12 | +import java.util.List; | |
| 13 | + | |
| 14 | + | |
| 15 | +public abstract class DataBindingRecyclerAdapter<D, T extends ViewDataBinding> extends RecyclerView.Adapter<DataBindingRecyclerAdapter<D, T>.DataBindingViewHolder> { | |
| 16 | + | |
| 17 | + public abstract int getLayoutId(); | |
| 18 | + | |
| 19 | + public abstract List<D> getList(); | |
| 20 | + | |
| 21 | + @Override | |
| 22 | + public DataBindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
| 23 | + T binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), getLayoutId(), parent, false); | |
| 24 | + DataBindingViewHolder viewHolder = new DataBindingViewHolder(binding.getRoot()); | |
| 25 | + viewHolder.setBinding(binding); | |
| 26 | + return viewHolder; | |
| 27 | + } | |
| 28 | + | |
| 29 | + @Override | |
| 30 | + public void onBindViewHolder(DataBindingViewHolder holder, int position) { | |
| 31 | + ReflectUtil.invoke(holder.binding, "setData", getList().get(position)); | |
| 32 | + holder.binding.executePendingBindings(); | |
| 33 | + } | |
| 34 | + | |
| 35 | + @Override | |
| 36 | + public int getItemCount() { | |
| 37 | + return getList().size(); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public class DataBindingViewHolder extends RecyclerView.ViewHolder { | |
| 41 | + | |
| 42 | + private ViewDataBinding binding; | |
| 43 | + | |
| 44 | + public DataBindingViewHolder(View itemView) { | |
| 45 | + super(itemView); | |
| 46 | + } | |
| 47 | + | |
| 48 | + public void setBinding(ViewDataBinding binding) { | |
| 49 | + this.binding = binding; | |
| 50 | + } | |
| 51 | + | |
| 52 | + public ViewDataBinding getBinding() { | |
| 53 | + return binding; | |
| 54 | + } | |
| 55 | + | |
| 56 | + } | |
| 57 | +} | ... | ... |
app/src/main/java/com/cnlive/libs/demo/MainActivity.java
| 1 | 1 | package com.cnlive.libs.demo; |
| 2 | 2 | |
| 3 | +import android.databinding.DataBindingUtil; | |
| 3 | 4 | import android.os.Bundle; |
| 4 | 5 | import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.util.Log; | |
| 7 | +import android.view.View; | |
| 8 | +import android.widget.TextView; | |
| 9 | + | |
| 10 | +import com.cnlive.libs.demo.databinding.ActivityMainBinding; | |
| 5 | 11 | |
| 6 | 12 | public class MainActivity extends AppCompatActivity { |
| 7 | 13 | |
| 14 | + private MainPageData data = new MainPageData(); | |
| 8 | 15 | |
| 9 | 16 | @Override |
| 10 | 17 | protected void onCreate(Bundle savedInstanceState) { |
| 11 | 18 | super.onCreate(savedInstanceState); |
| 12 | - setContentView(R.layout.activity_main); | |
| 13 | 19 | |
| 20 | + ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); | |
| 21 | + binding.setData(data); | |
| 14 | 22 | //Toast 测试 |
| 15 | 23 | ToastTest.test(this); |
| 16 | - | |
| 17 | 24 | Java8Test.test(this); |
| 18 | 25 | } |
| 26 | + | |
| 27 | + protected void onAddItemClick(View view){ | |
| 28 | + int id = data.getList().size(); | |
| 29 | + data.getList().add(new MainListItemData(""+id)); | |
| 30 | + } | |
| 31 | + | |
| 32 | + protected void onMainItemClick(View view) { | |
| 33 | + TextView itemView = (TextView) view; | |
| 34 | + Log.e("DemoView", "view ~" + itemView.getText()); | |
| 35 | + } | |
| 19 | 36 | } | ... | ... |
app/src/main/java/com/cnlive/libs/demo/MainAdapter.java
0 → 100644
| 1 | +package com.cnlive.libs.demo; | |
| 2 | + | |
| 3 | +import com.cnlive.libs.demo.databinding.ListitemMainBinding; | |
| 4 | + | |
| 5 | +import java.util.List; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Created by xiansong on 2017/8/30. | |
| 9 | + */ | |
| 10 | + | |
| 11 | +public class MainAdapter extends DataBindingRecyclerAdapter<MainListItemData, ListitemMainBinding> { | |
| 12 | + | |
| 13 | + private List<MainListItemData> list; | |
| 14 | + | |
| 15 | + public MainAdapter(List<MainListItemData> list){ | |
| 16 | + this.list = list; | |
| 17 | + } | |
| 18 | + | |
| 19 | + @Override | |
| 20 | + public int getLayoutId() { | |
| 21 | + return R.layout.listitem_main; | |
| 22 | + } | |
| 23 | + | |
| 24 | + @Override | |
| 25 | + public List<MainListItemData> getList() { | |
| 26 | + return list; | |
| 27 | + } | |
| 28 | +} | ... | ... |
app/src/main/java/com/cnlive/libs/demo/MainListItemData.java
0 → 100644
| 1 | +package com.cnlive.libs.demo; | |
| 2 | + | |
| 3 | +import android.databinding.BaseObservable; | |
| 4 | +import android.databinding.Bindable; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Created by xiansong on 2017/8/30. | |
| 8 | + */ | |
| 9 | + | |
| 10 | +public class MainListItemData extends BaseObservable { | |
| 11 | + private String name; | |
| 12 | + | |
| 13 | + public MainListItemData(String name ){ | |
| 14 | + this.name = name; | |
| 15 | + } | |
| 16 | + | |
| 17 | + @Bindable | |
| 18 | + public String getName() { | |
| 19 | + return name; | |
| 20 | + } | |
| 21 | + | |
| 22 | + public void setName(String name) { | |
| 23 | + this.name = name; | |
| 24 | + notifyPropertyChanged(BR.name); | |
| 25 | + } | |
| 26 | +} | ... | ... |
app/src/main/java/com/cnlive/libs/demo/MainPageData.java
0 → 100644
| 1 | +package com.cnlive.libs.demo; | |
| 2 | + | |
| 3 | +import android.databinding.BaseObservable; | |
| 4 | +import android.databinding.Bindable; | |
| 5 | +import android.support.v7.widget.LinearLayoutManager; | |
| 6 | + | |
| 7 | +import com.cnlive.libs.demo.databinding.ListitemMainBinding; | |
| 8 | + | |
| 9 | +import java.util.Arrays; | |
| 10 | +import java.util.List; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Created by xiansong on 2017/8/30. | |
| 14 | + */ | |
| 15 | + | |
| 16 | +public class MainPageData extends BaseObservable { | |
| 17 | + | |
| 18 | + private List<MainListItemData> list = Arrays.asList( | |
| 19 | + new MainListItemData("Title 1"), | |
| 20 | + new MainListItemData("Title 2"), | |
| 21 | + new MainListItemData("Title 3"), | |
| 22 | + new MainListItemData("Title 4"), | |
| 23 | + new MainListItemData("Title 5")); | |
| 24 | + | |
| 25 | + private DataBindingRecyclerAdapter adapter = new DataBindingRecyclerAdapter<MainListItemData, ListitemMainBinding>() { | |
| 26 | + @Override | |
| 27 | + public int getLayoutId() { | |
| 28 | + return R.layout.listitem_main; | |
| 29 | + } | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + public List<MainListItemData> getList() { | |
| 33 | + return list; | |
| 34 | + } | |
| 35 | + }; | |
| 36 | + | |
| 37 | + private LinearLayoutManager manager = new LinearLayoutManager(null); | |
| 38 | + | |
| 39 | + public List<MainListItemData> getList() { | |
| 40 | + return list; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public void setList(List<MainListItemData> list) { | |
| 44 | + this.list = list; | |
| 45 | + } | |
| 46 | + | |
| 47 | + @Bindable | |
| 48 | + public DataBindingRecyclerAdapter getAdapter() { | |
| 49 | + return adapter; | |
| 50 | + } | |
| 51 | + | |
| 52 | + public void setAdapter(DataBindingRecyclerAdapter adapter) { | |
| 53 | + this.adapter = adapter; | |
| 54 | + notifyPropertyChanged(BR.adapter); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Bindable | |
| 58 | + public LinearLayoutManager getManager() { | |
| 59 | + return manager; | |
| 60 | + } | |
| 61 | + | |
| 62 | + public void setManager(LinearLayoutManager manager) { | |
| 63 | + this.manager = manager; | |
| 64 | + notifyPropertyChanged(BR.manager); | |
| 65 | + } | |
| 66 | +} | ... | ... |
app/src/main/res/layout/activity_main.xml
| 1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
| 2 | -<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 2 | +<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 3 | 3 | xmlns:app="http://schemas.android.com/apk/res-auto" |
| 4 | - xmlns:tools="http://schemas.android.com/tools" | |
| 5 | - android:layout_width="match_parent" | |
| 6 | - android:layout_height="match_parent" | |
| 7 | - tools:context="com.cnlive.libs.demo.MainActivity"> | |
| 8 | - | |
| 9 | - <TextView android:id="@+id/textView" | |
| 10 | - android:layout_width="wrap_content" | |
| 11 | - android:layout_height="wrap_content" | |
| 12 | - android:text="Hello World!" | |
| 13 | - app:layout_constraintBottom_toBottomOf="parent" | |
| 14 | - app:layout_constraintLeft_toLeftOf="parent" | |
| 15 | - app:layout_constraintRight_toRightOf="parent" | |
| 16 | - app:layout_constraintTop_toTopOf="parent" /> | |
| 17 | - | |
| 18 | -</android.support.constraint.ConstraintLayout> | |
| 4 | + xmlns:tools="http://schemas.android.com/tools"> | |
| 5 | + | |
| 6 | + <data> | |
| 7 | + | |
| 8 | + <variable | |
| 9 | + name="data" | |
| 10 | + type="com.cnlive.libs.demo.MainPageData" /> | |
| 11 | + </data> | |
| 12 | + | |
| 13 | + <android.support.constraint.ConstraintLayout | |
| 14 | + android:layout_width="match_parent" | |
| 15 | + android:layout_height="match_parent" | |
| 16 | + tools:context="com.cnlive.libs.demo.MainActivity"> | |
| 17 | + | |
| 18 | + <Button | |
| 19 | + android:layout_width="368dp" | |
| 20 | + android:layout_height="wrap_content" | |
| 21 | + android:layout_marginBottom="8dp" | |
| 22 | + android:layout_marginLeft="8dp" | |
| 23 | + android:layout_marginRight="8dp" | |
| 24 | + android:layout_marginTop="8dp" | |
| 25 | + android:onClick="onAddItemClick" | |
| 26 | + android:text="Add Item" | |
| 27 | + app:layout_constraintBottom_toBottomOf="parent" | |
| 28 | + app:layout_constraintHorizontal_bias="0.0" | |
| 29 | + app:layout_constraintLeft_toLeftOf="parent" | |
| 30 | + app:layout_constraintRight_toRightOf="parent" | |
| 31 | + app:layout_constraintTop_toTopOf="parent" | |
| 32 | + app:layout_constraintVertical_bias="0.0" /> | |
| 33 | + | |
| 34 | + <TextView | |
| 35 | + android:id="@+id/textView" | |
| 36 | + android:layout_width="wrap_content" | |
| 37 | + android:layout_height="wrap_content" | |
| 38 | + android:text="Hello World!" | |
| 39 | + app:layout_constraintBottom_toBottomOf="parent" | |
| 40 | + app:layout_constraintLeft_toLeftOf="parent" | |
| 41 | + app:layout_constraintRight_toRightOf="parent" | |
| 42 | + app:layout_constraintTop_toTopOf="parent" /> | |
| 43 | + | |
| 44 | + <android.support.v7.widget.RecyclerView | |
| 45 | + android:id="@+id/recyclerView" | |
| 46 | + android:layout_width="368dp" | |
| 47 | + android:layout_height="200dp" | |
| 48 | + android:layout_marginBottom="8dp" | |
| 49 | + android:layout_marginEnd="8dp" | |
| 50 | + android:layout_marginLeft="8dp" | |
| 51 | + | |
| 52 | + android:layout_marginRight="8dp" | |
| 53 | + android:layout_marginStart="8dp" | |
| 54 | + android:layout_marginTop="8dp" | |
| 55 | + app:adapter="@{data.adapter}" | |
| 56 | + app:layoutManager="@{data.manager}" | |
| 57 | + app:layout_constraintBottom_toBottomOf="parent" | |
| 58 | + app:layout_constraintHorizontal_bias="0.0" | |
| 59 | + app:layout_constraintLeft_toLeftOf="parent" | |
| 60 | + app:layout_constraintRight_toRightOf="parent" | |
| 61 | + app:layout_constraintTop_toTopOf="parent" | |
| 62 | + app:layout_constraintVertical_bias="1.0" /> | |
| 63 | + | |
| 64 | + </android.support.constraint.ConstraintLayout> | |
| 65 | + | |
| 66 | +</layout> | ... | ... |
app/src/main/res/layout/listitem_main.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="utf-8"?> | |
| 2 | +<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
| 3 | + | |
| 4 | + <data> | |
| 5 | + | |
| 6 | + <variable | |
| 7 | + name="data" | |
| 8 | + type="com.cnlive.libs.demo.MainListItemData" /> | |
| 9 | + </data> | |
| 10 | + | |
| 11 | + <RelativeLayout | |
| 12 | + android:layout_width="match_parent" | |
| 13 | + android:layout_height="72dp"> | |
| 14 | + | |
| 15 | + <TextView | |
| 16 | + android:layout_width="wrap_content" | |
| 17 | + android:layout_height="wrap_content" | |
| 18 | + android:layout_centerVertical="true" | |
| 19 | + android:onClick="onMainItemClick" | |
| 20 | + android:padding="8dp" | |
| 21 | + android:text="@{data.name}" /> | |
| 22 | + | |
| 23 | + </RelativeLayout> | |
| 24 | + | |
| 25 | +</layout> | ... | ... |
cnlive/build.gradle
| ... | ... | @@ -73,7 +73,7 @@ ext { |
| 73 | 73 | siteUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo' |
| 74 | 74 | gitUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo.git' |
| 75 | 75 | |
| 76 | - libraryVersion = '0.1.4' | |
| 76 | + libraryVersion = '0.2.0' | |
| 77 | 77 | |
| 78 | 78 | developerId = 'CNlive' |
| 79 | 79 | developerName = 'Granzon' | ... | ... |
cnlive/from_bintrayv1.gradle
| ... | ... | @@ -2,11 +2,11 @@ apply plugin: 'com.jfrog.bintray' |
| 2 | 2 | |
| 3 | 3 | version = libraryVersion |
| 4 | 4 | |
| 5 | -tasks.withType(Javadoc) {//防止编码问题 | |
| 6 | - options.addStringOption('Xdoclint:none', '-quiet') | |
| 7 | - options.addStringOption('encoding', 'UTF-8') | |
| 8 | - options.addStringOption('charSet', 'UTF-8') | |
| 9 | -} | |
| 5 | +//tasks.withType(Javadoc) {//防止编码问题 | |
| 6 | +// options.addStringOption('Xdoclint:none', '-quiet') | |
| 7 | +// options.addStringOption('encoding', 'UTF-8') | |
| 8 | +// options.addStringOption('charSet', 'UTF-8') | |
| 9 | +//} | |
| 10 | 10 | |
| 11 | 11 | if (project.hasProperty("android")) { // Android libraries |
| 12 | 12 | task sourcesJar(type: Jar) { | ... | ... |