//这里我们写的复杂的RecyclerView对条目的抽取,我们只设置了三个类型,头布局(轮播图),身体布局,脚布局(上拉刷新)的复杂条目的抽取框架
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.oschina21.bean.FootBean;
import com.example.oschina21.bean.HeadBean;
import com.example.oschina21.interfaces.BodyType;
import com.example.oschina21.interfaces.FootType;
import com.example.oschina21.interfaces.HeadType;
import com.example.oschina21.interfaces.ItemType;
import com.example.oschina21.ui.views.FootViewLayout;
import com.example.oschina21.viewholder.FinalViewholder;
import java.util.ArrayList;
import java.util.List;
public class FinalListAdapter extends RecyclerView.Adapter<FinalViewholder> {
private List<ItemType> mShowItems = new ArrayList();
private int mBodyLayout = 0;
//监听
private FinalAdapterListener mFinalAdapterListener;
public FinalListAdapter(List<ItemType> showItems, int bodyLayout, FinalAdapterListener finalAdapterListener) {
mShowItems = showItems;
mBodyLayout = bodyLayout;
this.mFinalAdapterListener = finalAdapterListener;
}
//初始化
//viewType:这是我们条目的类型
@Override
public FinalViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
switch (viewType) {
case HEADTYPE:
//布局从外部传入???
//这个是不是必须的
//不是必须的
try {
view = LayoutInflater.from(parent.getContext()).inflate(mHeadLyaout, parent, false);
} catch (Exception e) {
throw new RuntimeException("小sb给个头部");
}
break;
case BODYTYPE:
//是不是必须的,
//必须的
//构造,抽象
//要不要子类
//
try {
view = LayoutInflater.from(parent.getContext()).inflate(mBodyLayout, parent, false);
} catch (Exception e) {
throw new RuntimeException("小sb给个身体");
}
break;
case FOOTTYPE:
//这里要考虑一下,我们的脚有很多状态,正在加载,失败,没有更多,或者.....
view = new FootViewLayout(parent.getContext());
break;
default:
break;
}
return new FinalViewholder(view);
}
//数据更新
@Override
public void onBindViewHolder(FinalViewholder holder, int position) {
//谁用谁处理
int itemViewType = getItemViewType(position);//得到条目类型
switch (itemViewType) {
case HEADTYPE:
mFinalAdapterListener.bindHeadView(holder, position, mShowItems);
break;
case BODYTYPE:
mFinalAdapterListener.bindBodyView(holder, position, mShowItems);
break;
case FOOTTYPE:
mFinalAdapterListener.bindFootView(holder, position, mShowItems);
break;
default:
break;
}
}
//把数据爆露出去
//接口回调
public interface FinalAdapterListener {
void bindHeadView(FinalViewholder holder, int position, List<ItemType> showItems);
void bindBodyView(FinalViewholder holder, int position, List<ItemType> showItems);
void bindFootView(FinalViewholder holder, int position, List<ItemType> showItems);
}
//接口更新是必须的,那么我们通过构造
@Override
public int getItemCount() {
return mShowItems.size();
}
//定义三个类型
public static final int HEADTYPE = 101;//头部类型
public static final int BODYTYPE = 102;//身体类型
public static final int FOOTTYPE = 103;//身体类型
//根据我们的数据返回不同的类型
@Override
public int getItemViewType(int position) {
if (mShowItems.get(position) instanceof HeadType) {
return HEADTYPE;
}
if (mShowItems.get(position) instanceof BodyType) {
return BODYTYPE;
}
if (mShowItems.get(position) instanceof FootType) {
return FOOTTYPE;
}
//如果上面都不是,那么走到下面,但是不可能
return BODYTYPE;
}
//刷新数据
//用来控件头部或者脚部显示的方法
public void updateData() {
//删除所有的头
for (int i = 0; i < mShowItems.size(); i++) {
if (mShowItems.get(i) instanceof HeadType) {
mShowItems.remove(i);
}
}
//添加头
//如果用户传入了头部布局,那么我们就显示头部,如果没有传入我们就不显示
if (mHeadLyaout > 0) {
//加头,加个假头,用来显示头部的位置的
mShowItems.add(0, new HeadBean());
}
//删除脚部
for (int i = 0; i < mShowItems.size(); i++) {
if (mShowItems.get(i) instanceof FootType) {
mShowItems.remove(i);
}
}
//显示的
if (mIsShowFoot) {
mShowItems.add(new FootBean());
}
//刷新数据
notifyDataSetChanged();
}
private int mHeadLyaout = 0;//头部布局
//钩子方法
//脚部传入的方法
public void setHeadLayout(int headLayout) {
this.mHeadLyaout = headLayout;
}
//是否显示脚部
private boolean mIsShowFoot = false;//默认是false,框架设计的时候只需要满足基本需求
public void isShowFoot(boolean isShowFoot) {
this.mIsShowFoot = isShowFoot;
}
}
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.oschina21.bean.FootBean;
import com.example.oschina21.bean.HeadBean;
import com.example.oschina21.interfaces.BodyType;
import com.example.oschina21.interfaces.FootType;
import com.example.oschina21.interfaces.HeadType;
import com.example.oschina21.interfaces.ItemType;
import com.example.oschina21.ui.views.FootViewLayout;
import com.example.oschina21.viewholder.FinalViewholder;
import java.util.ArrayList;
import java.util.List;
public class FinalListAdapter extends RecyclerView.Adapter<FinalViewholder> {
private List<ItemType> mShowItems = new ArrayList();
private int mBodyLayout = 0;
//监听
private FinalAdapterListener mFinalAdapterListener;
public FinalListAdapter(List<ItemType> showItems, int bodyLayout, FinalAdapterListener finalAdapterListener) {
mShowItems = showItems;
mBodyLayout = bodyLayout;
this.mFinalAdapterListener = finalAdapterListener;
}
//初始化
//viewType:这是我们条目的类型
@Override
public FinalViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
switch (viewType) {
case HEADTYPE:
//布局从外部传入???
//这个是不是必须的
//不是必须的
try {
view = LayoutInflater.from(parent.getContext()).inflate(mHeadLyaout, parent, false);
} catch (Exception e) {
throw new RuntimeException("小sb给个头部");
}
break;
case BODYTYPE:
//是不是必须的,
//必须的
//构造,抽象
//要不要子类
//
try {
view = LayoutInflater.from(parent.getContext()).inflate(mBodyLayout, parent, false);
} catch (Exception e) {
throw new RuntimeException("小sb给个身体");
}
break;
case FOOTTYPE:
//这里要考虑一下,我们的脚有很多状态,正在加载,失败,没有更多,或者.....
view = new FootViewLayout(parent.getContext());
break;
default:
break;
}
return new FinalViewholder(view);
}
//数据更新
@Override
public void onBindViewHolder(FinalViewholder holder, int position) {
//谁用谁处理
int itemViewType = getItemViewType(position);//得到条目类型
switch (itemViewType) {
case HEADTYPE:
mFinalAdapterListener.bindHeadView(holder, position, mShowItems);
break;
case BODYTYPE:
mFinalAdapterListener.bindBodyView(holder, position, mShowItems);
break;
case FOOTTYPE:
mFinalAdapterListener.bindFootView(holder, position, mShowItems);
break;
default:
break;
}
}
//把数据爆露出去
//接口回调
public interface FinalAdapterListener {
void bindHeadView(FinalViewholder holder, int position, List<ItemType> showItems);
void bindBodyView(FinalViewholder holder, int position, List<ItemType> showItems);
void bindFootView(FinalViewholder holder, int position, List<ItemType> showItems);
}
//接口更新是必须的,那么我们通过构造
@Override
public int getItemCount() {
return mShowItems.size();
}
//定义三个类型
public static final int HEADTYPE = 101;//头部类型
public static final int BODYTYPE = 102;//身体类型
public static final int FOOTTYPE = 103;//身体类型
//根据我们的数据返回不同的类型
@Override
public int getItemViewType(int position) {
if (mShowItems.get(position) instanceof HeadType) {
return HEADTYPE;
}
if (mShowItems.get(position) instanceof BodyType) {
return BODYTYPE;
}
if (mShowItems.get(position) instanceof FootType) {
return FOOTTYPE;
}
//如果上面都不是,那么走到下面,但是不可能
return BODYTYPE;
}
//刷新数据
//用来控件头部或者脚部显示的方法
public void updateData() {
//删除所有的头
for (int i = 0; i < mShowItems.size(); i++) {
if (mShowItems.get(i) instanceof HeadType) {
mShowItems.remove(i);
}
}
//添加头
//如果用户传入了头部布局,那么我们就显示头部,如果没有传入我们就不显示
if (mHeadLyaout > 0) {
//加头,加个假头,用来显示头部的位置的
mShowItems.add(0, new HeadBean());
}
//删除脚部
for (int i = 0; i < mShowItems.size(); i++) {
if (mShowItems.get(i) instanceof FootType) {
mShowItems.remove(i);
}
}
//显示的
if (mIsShowFoot) {
mShowItems.add(new FootBean());
}
//刷新数据
notifyDataSetChanged();
}
private int mHeadLyaout = 0;//头部布局
//钩子方法
//脚部传入的方法
public void setHeadLayout(int headLayout) {
this.mHeadLyaout = headLayout;
}
//是否显示脚部
private boolean mIsShowFoot = false;//默认是false,框架设计的时候只需要满足基本需求
public void isShowFoot(boolean isShowFoot) {
this.mIsShowFoot = isShowFoot;
}
}