RecyclerView(循环复用控件)
优点:提供了一种插拔式的体验,高度的解耦,异常的灵活
用法:
1.通过布局管理器LayoutManager,控制其显示的方式
2.通过ItemDecoration控件Item间的间隔(可绘制)
3.通过ItemAnimator控件Item增删的动画
1.实现垂直方向的滚动
dependencies {
compile fileTree(dir: ‘libs’, include: [’*.jar’])
testCompile ‘junit:junit:4.12’
compile ‘com.android.support:appcompat-v7:24.2.0’
compile ‘com.android.support:recyclerview-v7:24.2.1’
}
添加布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
创建RecyclerView 适配器 BookBaseAdapter ,这个类继
承 RecyclerView.Adapter 并将泛型指定
为 BookBaseAdapter.ViewHolder
其中ViewHolder是我们在 BookBaseAdapter 中定义的一个内部
类:代码如下:
public class BookBaseAdapter extends
RecyclerView.Adapter<BookBaseAdapter.ViewHolder>{
private List<Book> mBookList;
static class ViewHolder extends
RecyclerView.ViewHolder{
ImageView bookImage;
TextView bookname;
public ViewHolder(View view) {
super(view);
bookImage = (ImageView) view.findViewById
(R.id.book_iamge);
bookname = (TextView) view.findViewById
(R.id.book_name);
}
}