一、RecycleView 详细介绍
1、RecycleView 概念
RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相
似的渲染方式。该控件用于在有限的窗口中展有限量数据集,它被作为
ListView和GridView控件的继承者。RecyclerView高度解耦,异常灵活,
通过设置不同LayoutManager,ItemDecoration , ItemAnimator实现令人
想象不到的效果。
2、RecycleView用法
(1)ViewHolder介绍
1.Datas: 要显示的数据
2.Adapter:适配器,绑定数据集
3.ViewHolder:根据当前的数据保存视图
4.LayoutManager:布局管理器。决定item如何摆放
5.ItemDecoration:勉强理解为item装饰器,可以美化item
6. ItemAnimator:动画(当item被增加,删除,重新摆放时动画才有效)。
7. Listener: 事件。RecyclerView 本身不提供OnItemClickListener 等事件
(2)Adapter
1. 继承 RecyclerView.Adapter
2. 要重写的方法
// 创建 ViewHolder ,相当于ListView Adapter 的getView 方法
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return null;
}
// 数据绑定
public void onBindViewHolder(ViewHolder viewHolder, int i) {
}
// 数据的长度
public int getItemCount() {
return 0;
}
(3)ViewHolder
1. 继承 RecyclerView.ViewHolder
2. 要重写的方法
class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
// 绑定控件
}
}
(4)LayoutManager
1. LinearLayoutManager
线性布局,LayoutManager的实现类,类型包括Vertical和Horizontal
2. GridLayoutManager
格子布局,继承自LinearLayoutManager,实现效果类似GridView
3. StaggeredGridLayoutManager
交错的格子布局,同样也是LayoutManager的实现类,类型包括Vertical和
Horizontal,与GridLayoutManager很相似,不过是交错的格子,也就是宽高不等的格子视图,类似瀑布流的效果
(5)ItemDecoration
1.继承 RecyclerView.ItemDecoration
2. 重要方法:
//在itemView绘制完成之前调用,也就是说此方法draw出来的效果将会在itemView的下面
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
}
//与onDraw相反,draw出来的效果将会叠加在itemView的上面
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
//算通过配置outRect来设置itemView的inset边界,相当于设置itemView的margin
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
}
(6)ItemAnimator
1.继承 RecyclerView.ItemAnimator
2. 提供默认的Animator:DefaultItemAnimator
3.github 开源
https://github.com/gabrielemariotti/RecyclerViewItemAnimators
(7)notifyDataChange
notifyDataSetChanged() 弃用
notifyItemChanged(int position)
notifyItemInserted(int position)
notifyItemMoved(int fromPosition, int toPosition)
notifyItemRemoved(int position)
//range系列,多个item的操作
notifyItemRangeChanged(int positionStart, int itemCount)
notifyItemRangeInserted(int positionStart, int itemCount)
notifyItemRangeRemoved(int positionStart, int itemCount)
二、RecycleView的简单封装
1.数据使用泛型
2.数据绑定通过实现抽象方法来实现
3.ViewHolder中的View成员变量转而通过view数组来实现
4.基类里面提供常用的方法
BaseAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public abstract class BaseAdapter<T,H extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder>{
protected static final String TAG = BaseAdapter.class.getSimpleName();
protected final Context context;
protected int layoutResId;
protected List<T> datas;
private OnItemClickListener mOnItemClickListener = null;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public BaseAdapter(Context context, int layoutResId) {
this(context, layoutResId, null);
}
public BaseAdapter(Context context, int layoutResId, List<T> datas) {
this.datas = datas == null ? new ArrayList<T>() : datas;
this.context = context;
this.layoutResId = layoutResId;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false);
BaseViewHolder vh = new BaseViewHolder(view,mOnItemClickListener);
return vh;
}
@Override
public void onBindViewHolder(BaseViewHolder viewHoder, int position) {
T item = getItem(position);
convert((H)viewHoder, item);
}
@Override
public int getItemCount() {
if(datas==null || datas.size()<=0)
return 0;
return datas.size();
}
public T getItem(int position) {
if (position >= datas.size()) return null;
return datas.get(position);
}
public void clear(){
// int itemCount = datas.size();
// datas.clear();
// this.notifyItemRangeRemoved(0,itemCount);
if(datas==null || datas.size()<=0)
return;
for (Iterator it=datas.iterator();it.hasNext();){
T t = (T) it.next();
int position = datas.indexOf(t);
it.remove();
notifyItemRemoved(position);
}
}
/**
* 从列表中删除某项
* @param t
*/
public void removeItem(T t){
int position = datas.indexOf(t);
datas.remove(position);
notifyItemRemoved(position);
}
public List<T> getDatas(){
return datas;
}
public void addData(List<T> datas){
addData(0,datas);
}
public void addData(int position,List<T> list){
if(list !=null && list.size()>0) {
for (T t:list) {
datas.add(position, t);
notifyItemInserted(position);
}
}
}
public void refreshData(List<T> list){
clear();
if(list !=null && list.size()>0){
int size = list.size();
for (int i=0;i<size;i++){
datas.add(i,list.get(i));
notifyItemInserted(i);
}
}
}
public void loadMoreData(List<T> list){
if(list !=null && list.size()>0){
int size = list.size();
int begin = datas.size();
for (int i=0;i<size;i++){
datas.add(list.get(i));
notifyItemInserted(i+begin);
}
}
}
/**
* Implement this method and use the helper to adapt the view to the given item.
* @param viewHoder A fully initialized helper.
* @param item The item that needs to be displayed.
*/
protected abstract void convert(H viewHoder, T item);
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
}
BaseViewHolder.java
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private SparseArray<View> views;
private BaseAdapter.OnItemClickListener mOnItemClickListener ;
public BaseViewHolder(View itemView,BaseAdapter.OnItemClickListener onItemClickListener){
super(itemView);
itemView.setOnClickListener(this);
this.mOnItemClickListener =onItemClickListener;
this.views = new SparseArray<View>();
}
public TextView getTextView(int viewId) {
return retrieveView(viewId);
}
public CheckBox getCheckBox(int viewId) {
return retrieveView(viewId);
}
public Button getButton(int viewId) {
return retrieveView(viewId);
}
public ImageView getImageView(int viewId) {
return retrieveView(viewId);
}
public View getView(int viewId) {
return retrieveView(viewId);
}
protected <T extends View> T retrieveView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v,getLayoutPosition());
}
}
}
该Adapter适用于单布局,提供上下拉刷新方法,不支持多布局。