AndroidRecycleView实现不同样式Item样式效果完美解决

更新时间:2023-07-13 14:03:24 阅读: 评论:0

AndroidRecycleView实现不同样式Item样式效果完美解决 在我们开发过程中经常会遇到需要我们展⽰不同样式列表的情况,通常我们都会使⽤ListView展⽰列表,然后实现BaAdapter两个⽅法,即:getViewTypeCount() 和 getItemViewType(int position)即可实现
不过在Android Nougat Google发布了RecycleView之后,⼤家都纷纷弃ListView ,GridView,⽽使⽤RecycleView。所以今天我们写⼀个RecycleView实现不同Item条⽬样式的案例。
RecycleView和RecycleViewAdapter使⽤步骤:
1,初始化RecycleView及相关设置
mRecyclerView = (RecyclerView) findViewById(lerView);
//设置适配器管理器:LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager(瀑布流),
mRecyclerView.tLayoutManager(new LinearLayoutManager(getContext()));
//添加分割线
mRecyclerView.addItemDecoration(new RecycleItemDecoration(context, RecycleItemDecoration.VERTICAL_LIST));
2,设置适配器和添加数据
<span >    </span>mRecyclerView.tAdapter(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.tText((char) i + "");
int type = i % 3;
if (type == 0) {
bean.tType(0);
} el if (type == 1) {
bean.tType(1);
} el if (type == 2) {
bean.tType(2);ba是什么元素
}
mData.add(bean);
migo}
return mData;
}
3,实现RecycleViewAdapter适配器并实现所需⽅法
通常实现常⽤列表样式只需要实现两个⽅法即可:
<span >    </span>public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
⽤来创建ViewHolder并将item布局添加ViewHolder中
<span >    </span>public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position)获取已绑定的ViewHolder对象并进⾏数据填充
<span >    </span>public int getItemViewType(int position)
获取指定苏索引条⽬的样式类型,返回值即为当前条⽬样式的类型(我总感觉应该是tItemViewType⽐较恰当)
通过这三部我们就基本可以使⽤RecycleView和RecycleAdapter实现列表和多种Item样式布局展⽰。下⾯我们展⽰详细代码:
/**
* 作    者 :丁⼴帅
* 创建⽇期 :2016/9/29 10:30
* 描  述 :
*/
public class PerfertectFg extends BaFragment {
@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) {
tContentView(R.layout.child_three);
context = getContext();
mRecyclerView = (RecyclerView) findViewById(lerView);
//设置适配器管理器:LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager(瀑布流),
mRecyclerView.tLayoutManager(new LinearLayoutManager(getContext()));
//添加分割线
mRecyclerView.addItemDecoration(new RecycleItemDecoration(context, RecycleItemDecoration.VERTICAL_LIST));        mRecyclerView.tAdapter(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.tText((char) i + "");网络教育报名
int type = i % 3;
if (type == 0) {
} el if (type == 2) {
bean.tType(2);
}
mData.add(bean);
}
return mData;
}
}
借鉴鸿神的分割线样式:Android RecyclerView 使⽤完全解析 体验艺术般的控件
public class RecycleItemDecoration extends RecyclerView.ItemDecoration {
sheets/**
* 读取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);
tOrientation(orientation);
}
public void tOrientation(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);
} el {
drawHorizontal(c, parent);
}
/**
* 绘制竖直⽅向分割线
*
* @param c
* @param parent
*/
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = PaddingLeft();
final int right = Width() - PaddingRight();
final int childCount = ChildCount();
for (int i = 0; i < childCount; i++) {
final View child = ChildAt(i);
android.support.v7.widget.RecyclerView v = new android.support.v7.widget.Context());            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = Bottom() + params.bottomMargin;
final int bottom = top + IntrinsicHeight();
mDivider.tBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
/**
* 绘制⽔平⽅向分割线
*
* @param c
* @param parent
*/
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = PaddingTop();
四伏
final int bottom = Height() - PaddingBottom();
final int childCount = ChildCount();
for (int i = 0; i < childCount; i++) {
final View child = ChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = Right() + params.rightMargin;
final int right = left + IntrinsicHeight();
mDivider.tBounds(left, top, right, bottom);
冬天里的一把火英文mDivider.draw(c);
}
}
@Override
public void getItemOffts(Rect outRect, int itemPosition, RecyclerView parent) {
//区别类型将分割线设置在下⽅和右⽅
if (mOrientation == VERTICAL_LIST) {
outRect.t(0, 0, 0, IntrinsicHeight());
} el {
outRect.t(0, 0, IntrinsicWidth(), 0);简单的商务英语口语
}
dRecycleView适配器实现,需要我们注意的⼀点是如果仅有⼀种Item样式RecycleAdapter的泛型我们
直接写成⾃定义ViewHolder即可。如果是多种Item样式的话,RecycleAdapter泛型必须为RecycleView.ViewHolder,然后再进⾏强转
((ViewHolderA) holder).text.(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;
}messy
/**
* 创建ViewHolder
*
* @param parent
* @param viewType :不同ItemView的类型
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == Item_Type.RECYCLEVIEW_ITEM_dinal()) {
View mView = LayoutInflater.from(context).inflate(le_item_a, null);
ViewHolderA viewHolder = new ViewHolderA(mView);
return viewHolder;
} el if (viewType == Item_Type.RECYCLEVIEW_ITEM_dinal()) {

本文发布于:2023-07-13 14:03:24,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1094413.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:样式   实现   设置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图