Android recycleview

构建MVVM模式的基本应用框架笔记 RecyclerView

2016-08-08  本文已影响1281人  低调的喃喃

<h4 id="toc_1">零、废话</h4>

<p>列表基本上是每个应用中都会用到的东西。所以必须率先掌握MVVM中RecyclerView的用法。本来其实挺喜欢用ListView的,后来不知道重何时起,大家都说RecyclerView比ListView好用多了,然后我就人云亦云的觉得RecyclerView确实比ListView好用,然后就再也没用过ListView,所以以后也没打算用ListView了,全部用RecyclerView代替了。</p>

<h4 id="toc_2">一、直接在Activity中添加RecyclerView</h4>

<p>据说使用DataBinding自己写RecyclerView的各种弄个Adapter什么的是非常麻烦的一件事,而且已经有人提供了非常好用的解决方案。那么,在快速学习MVVM设计模式的初级阶段不妨直接使用第三方库来实现些东西,之后再去研究第三方库的源码。

<a href="https://github.com/evant/binding-collection-adapter">BindingCollectionAdapter</a>

这个第三方库使用也非常简单,只需要在build.gradle中的dependencies加上:</p>

<pre><code> compile 'me.tatarka.bindingcollectionadapter:bindingcollectionadapter-recyclerview:1.2.0'

</code></pre>

<p>RecyclerView需要的几个基本元素是<code>LayoutManager</code>,<code>Adapter</code>,<code>item</code>以及item的布局<code>itemView</code>,在MVVM中,首先解决的是ViewModel,这个类里当然就是要包含<code>item</code>和<code>itemView</code></p>

<pre><code>public class ViewModel {
public ObservableList<RecyclerItemViewModel> items = new ObservableArrayList<>();
public ItemView itemView= ItemView.of(BR.item, R.layout.item);

private Context mContext;

public ViewModel(Context context) {
mContext=context;
for(int i=0;i<10;i++){
items.add(new RecyclerItemViewModel(mContext,"item "+i));
}
}
}

</code></pre>

<p>可以看到,ViewModel中包含了俩变量,items用来存放item,itemView则是每一个item对应的布局,如果需要使用多类型布局的话,自定义<code>ItemViewSelector</code>就行啦。我们接下去就是为item创建view和ViewModel。

item.xml</p>

<pre><code><layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="item" type="com.nannan.mvvmtest.viewmodel.RecyclerItemViewModel"/>
</data>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:onClick="@{item.onItemClick}">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{item.title}"/>

&lt;Button
  android:layout_width=&quot;30dp&quot;
  android:layout_height=&quot;40dp&quot;
  android:layout_alignParentRight=&quot;true&quot;/&gt;

</RelativeLayout>

</layout>
</code></pre>

<p>item对应的ViewModel </p>

<pre><code>public class RecyclerItemViewModel extends BaseObservable {

private Context mContext;
@Bindable
private String title;

public RecyclerItemViewModel(Context mContext) {
this.mContext = mContext;
}

public RecyclerItemViewModel(Context mContext, String title) {
this.mContext = mContext;
this.title = title;
}

public void onItemClick(View v){
Toast.makeText(mContext, title, Toast.LENGTH_SHORT).show();
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

</code></pre>

<p>我们可以看到,item的ViewModel中不但包含了View所需的各种Model,而且还有item的点击事件<code>onItemClick(View v)</code> ,不得不感叹一下,MVVM设计模式从某些角度来看,确实挺好用的。然后就是在Activity的布局中添加RecyclerView控件,因为我们引入的第三方库中自带了<code>LayoutManager</code>和<code>Adapter</code>,所以如果没有特别的需求的话,完全可以不用自己自定也。

activity_main.xml中添加的RecyclerView控件</p>

<pre><code>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="@{LayoutManagers.linear()}"
app:items="@{viewModel.items}"
app:itemView="@{viewModel.itemView}"/>
</code></pre>

<p><a href="https://github.com/ljnjiannan/MvvmTest/tree/v0.0.2">完整代码</a></p>

<p>其实大多数情况下我们不需要在Activity中直接使用RecyclerView,所以接下来我们就用第三方库中的RecyclerViewFragment。</p>

上一篇下一篇

猜你喜欢

热点阅读