在我们开发过程中经常会遇到需要我们展示不同样式列表的情况,通常我们都会使用ListView展示列表,然后实现BaseAdapter两个方法,即:getViewTypeCount() 和 getItemViewType(int position)即可实现
不过在Android Nougat Google发布了RecycleView之后,大家都纷纷弃ListView ,GridView,而使用RecycleView。所以今天我们写一个RecycleView实现不同Item条目样式的案例。
RecycleView和RecycleViewAdapter使用步骤:
1,初始化RecycleView及相关设置
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//设置适配器管理器:LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager(瀑布流),
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//添加分割线
mRecyclerView.addItemDecoration(new RecycleItemDecoration(context, RecycleItemDecoration.VERTICAL_LIST));
2,设置适配器和添加数据
<span style="white-space:pre"> </span>mRecyclerView.setAdapter(new RecycleAdapter(context, initData()));
private List<Bean> initData() {
List<Bean> mData = new ArrayList<>();
for (int i = 'A'; i < 'Z'; i++) {
Bean bean = new Bean();
bean.setText((char) i + "");
int type = i % 3;
if (type == 0) {
bean.setType(0);
} else if (type == 1) {
bean.setType(1);
} else if (type == 2) {
bean.setType(2);
}
mData.add(bean);
}
return mData;
}
3,实现RecycleViewAdapter适配器并实现所需方法
通常实现常用列表样式只需要实现两个方法即可:
<span style="white-space:pre"> </span>public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
用来创建ViewHolder并将item布局添加ViewHolder中
<span style="white-space:pre"> </span>public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position)
获取已绑定的ViewHolder对象并进行数据填充
而我们使用RecycleView实现不同Item样式的展示,同ListVIew一样,RecycleView也给我们提供了类似的方法,
<span style="white-space:pre"> </span>public int getItemViewType(int position)
获取指定苏索引条目的样式类型,返回值即为当前条目样式的类型(我总感觉应该是setItemViewType比较恰当)
通过这三部我们就基本可以使用RecycleView和RecycleAdapter实现列表和多种Item样式布局展示。下面我们展示详细代码:
/**
* 作 者 :丁广帅
* 创建日期 :2016/9/29 10:30
* 描 述 :
*/
public class PerfertectFg extends BaseFragment {
@SuppressLint("ValidFragment")
public PerfertectFg(String tabNames, int tabIcons) {
super(tabNames, tabIcons);
}
public PerfertectFg() {
}
private RecyclerView mRecyclerView;
private Context context;
@Override
protected void onCreateViewLazy(Bundle savedInstanceState) {
super.onCreateViewLazy(savedInstanceState);
setContentView(R.layout.child_three);
context = getContext();
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//设置适配器管理器:LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager(瀑布流),
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//添加分割线
mRecyclerView.addItemDecoration(new RecycleItemDecoration(context, RecycleItemDecoration.VERTICAL_LIST));
mRecyclerView.setAdapter(new RecycleAdapter(context, initData()));
}
private List<Bean> initData() {
List<Bean> mData = new ArrayList<>();
for (int i = 'A'; i < 'Z'; i++) {
Bean bean = new Bean();
bean.setText((char) i + "");
int type = i % 3;
if (type == 0) {
bean.setType(0);
} else if (type == 1) {
bean.setType(1);
} else if (type == 2) {
bean.setType(2);
}
mData.add(bean);
}
return mData;
}
}
借鉴鸿神的分割线样式:Android RecyclerView 使用完全解析 体验艺术般的控件
public class RecycleItemDecoration extends RecyclerView.ItemDecoration {
/**
* 读取attr资源
*/
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
//分割线高度
private int mOrientation;
public RecycleItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
/**
* 绘制竖直方向分割线
*
* @param c
* @param parent
*/
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
/**
* 绘制水平方向分割线
*
* @param c
* @param parent
*/
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
//区别类型将分割线设置在下方和右方
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
d
RecycleView适配器实现,需要我们注意的一点是如果仅有一种Item样式RecycleAdapter的泛型我们直接写成自定义ViewHolder即可。如果是多种Item样式的话,RecycleAdapter泛型必须为RecycleView.ViewHolder,然后再进行强转
((ViewHolderA) holder).text.setText(mData.get(position).getText() + "------样式一");
/**
* 作 者 :丁广帅
* 创建日期 :2016/10/14 14:26
* 描 述 :
*/
public class RecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
/**
* RecycleViewAdapter 要素点:
* <p/>
* 1,ViewHolder必须继承RecyclerView.ViewHolder
* 2,RecycleView.Adapter的泛型为自定义ViewHolder
*/
private List<Bean> mData;
private Context context;
public enum Item_Type {
RECYCLEVIEW_ITEM_TYPE_1,
RECYCLEVIEW_ITEM_TYPE_2,
RECYCLEVIEW_ITEM_TYPE_3
}
public RecycleAdapter(Context context, List<Bean> mData) {
this.mData = mData;
this.context = context;
}
/**
* 创建ViewHolder
*
* @param parent
* @param viewType :不同ItemView的类型
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == Item_Type.RECYCLEVIEW_ITEM_TYPE_1.ordinal()) {
View mView = LayoutInflater.from(context).inflate(R.layout.recycle_item_a, null);
ViewHolderA viewHolder = new ViewHolderA(mView);
return viewHolder;
} else if (viewType == Item_Type.RECYCLEVIEW_ITEM_TYPE_2.ordinal()) {
View mView = LayoutInflater.from(context).inflate(R.layout.recycle_item_b, null);
ViewHolderB viewHolder = new ViewHolderB(mView);
return viewHolder;
} else if (viewType == Item_Type.RECYCLEVIEW_ITEM_TYPE_3.ordinal()) {
View mView = LayoutInflater.from(context).inflate(R.layout.recycle_item_c, null);
ViewHolderC viewHolder = new ViewHolderC(mView);
return viewHolder;
}
return null;
}
/**
* 绑定数据:可以直接拿到已经绑定控件的Viewholder对象
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolderA) {
((ViewHolderA) holder).text.setText(mData.get(position).getText() + "------样式一");
} else if (holder instanceof ViewHolderB) {
((ViewHolderB) holder).text.setText(mData.get(position).getText() + "------样式二");
} else if (holder instanceof ViewHolderC) {
((ViewHolderC) holder).text.setText(mData.get(position).getText() + "------样式三");
}
}
//返回值赋值给onCreateViewHolder的参数 viewType
@Override
public int getItemViewType(int position) {
if (mData.get(position).getType() == 0) {
return Item_Type.RECYCLEVIEW_ITEM_TYPE_1.ordinal();
} else if (mData.get(position).getType() == 1) {
return Item_Type.RECYCLEVIEW_ITEM_TYPE_2.ordinal();
} else if (mData.get(position).getType() == 2) {
return Item_Type.RECYCLEVIEW_ITEM_TYPE_3.ordinal();
}
return -1;
}
@Override
public int getItemCount() {
return mData.size();
}
class ViewHolderA extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolderA(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
}
}
class ViewHolderB extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolderB(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
}
}
class ViewHolderC extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolderC(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
}
}
}
最后还是建议大家以后尽量使用RecycleView来替代ListVIew和GridView!!!
下载链接:http://download.csdn.net/detail/ding_gc/9655977