RecyclerView的不同position加载不同View实现
2016-02-19 14:36
369 查看
在android开发中,有很多页面,可能一个页面上面需要2种不同的recyclerView,一般的,我们会使用一个scrollview,然后嵌套2个recyclerView,但是嵌套总会有点问题,除非你重写方法,listView是重写listview的onMeasure方法,RecyclerView重写LayoutManager方法,具体可以参考之前的一篇博客
/article/9188614.html
但是,这么做可能还是会有这样那样的限制。
下面介绍一种新的方法,全部用一个RecyclerView或者listview显示,用getItemViewType方法中不同的position,设置不同的ViewType
核心思想:
1.重写RecyclerView.Adapter的getItemViewType(int position),在此方法中根据不同的position,设置不同的ViewType
2.编写具体的RecyclerView.ViewHolder子类(不同子类对应不同View或Layout)
3.重写RecyclerView.Adapter的onCreateViewHolder(ViewGroup parent,int viewType) 在此方法中根据我们之前设置的ViewType来返回不同的RecyclerView.ViewHolder的子类
直接上代码
效果可以是这样的
如上面2张图,从用户信息,到图片列表,到评论列表都是可以写在一个RecyclerView里面的
/article/9188614.html
但是,这么做可能还是会有这样那样的限制。
下面介绍一种新的方法,全部用一个RecyclerView或者listview显示,用getItemViewType方法中不同的position,设置不同的ViewType
核心思想:
1.重写RecyclerView.Adapter的getItemViewType(int position),在此方法中根据不同的position,设置不同的ViewType
2.编写具体的RecyclerView.ViewHolder子类(不同子类对应不同View或Layout)
3.重写RecyclerView.Adapter的onCreateViewHolder(ViewGroup parent,int viewType) 在此方法中根据我们之前设置的ViewType来返回不同的RecyclerView.ViewHolder的子类
直接上代码
public class FunItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<FunItemModel.ImageUrlEntity> mImgList; private List<CommentsEntity> mCommentList; private Context mContext; private String author, time, msg, authorUrl, location; private static final int TYPE_INFO = 0; private static final int TYPE_IMGS = 1; private static final int TYPE_MSG = 2; private static final int TYPE_CT = 3; private static final int TYPE_COMS = 4; public FunItemAdapter(List<FunItemModel.ImageUrlEntity> imageUrlList, List<CommentsEntity> commentsList, String author, String authorUrl, String time, String location, String msg, Activity context) { LogCat.i("FunItemAdapter"); mImgList = imageUrlList; mCommentList = commentsList; this.author = author; this.time = time; this.msg = msg; this.authorUrl = authorUrl; this.location = location; mContext = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { RecyclerView.ViewHolder holder = getViewHolderByViewType(viewType); return holder; } private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) { View view_info = View.inflate(mContext, R.layout.fun_item_info, null); View view_imgs = View.inflate(mContext, R.layout.fun_detail_pic_item_view, null); RecyclerView.ViewHolder holder = null; switch (viewType) { case TYPE_INFO: holder = new ViewHolder_Info(view_info); break; case TYPE_IMGS: holder = new ViewHolder_Imgs(view_imgs); break; } // if(i==(mImgList.size()+1)){//msg // // } // // if(i>(mImgList.size()+1)){//pinglun // // } return holder; } @Override public int getItemViewType(int position) { if (position == 0) { return TYPE_INFO; } if (position > 0 && position <= mImgList.size()) {//mImgList){ return TYPE_IMGS; } return 0; } @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { if (i == 0) { Picasso .with(mContext) .load(authorUrl) .into(((ViewHolder_Info)viewHolder).detail_author_img); ((ViewHolder_Info)viewHolder).detail_author_text.setText(author); ((ViewHolder_Info)viewHolder).detail_location.setText(location); ((ViewHolder_Info)viewHolder).detail_time.setText(time); // mMsgText.setText(msg); } if (i > 0) { String url = mImgList.get(i - 1).getValue(); LogCat.i("帖子详情图片地址url=" + url); Transformation transformation = new PicassoTransformation(((ViewHolder_Imgs)viewHolder).detail_pic_item); if (url != null && !url.equals("")) { url = Constant.QINIU_YUN + url; Picasso .with(mContext) .load(url) .placeholder(R.mipmap.zhanwei) .error(R.mipmap.zhanwei) .transform(transformation) .into(((ViewHolder_Imgs)viewHolder).detail_pic_item); } } } @Override public int getItemCount() { return 1 + mImgList.size()/*+1+1+mCommentList.size()*/; } class ViewHolder_Info extends RecyclerView.ViewHolder { public ImageView detail_author_img; public TextView detail_author_text,detail_time,detail_location; public ViewHolder_Info(View itemView) { super(itemView); detail_author_img = (ImageView)itemView.findViewById(R.id.detail_author_img); detail_author_text = (TextView)itemView.findViewById(R.id.detail_author_text); detail_time = (TextView)itemView.findViewById(R.id.detail_time); detail_location = (TextView)itemView.findViewById(R.id.detail_location); } } class ViewHolder_Imgs extends RecyclerView.ViewHolder { public ImageView detail_pic_item; public ViewHolder_Imgs(View itemView) { super(itemView); detail_pic_item = (ImageView)itemView.findViewById(R.id.detail_pic_item); } } }
效果可以是这样的
如上面2张图,从用户信息,到图片列表,到评论列表都是可以写在一个RecyclerView里面的
相关文章推荐
- 自己写USB 操作系统
- java项目中常见jar包的说明
- Tensorflow MNIST 数据集测试代码入门
- 迁云架构实践
- leetcode笔记--Delete Node in a Linked List
- 328. Odd Even Linked List
- 我的2016年计划
- 区分微信内置浏览器
- iOS学习之Socket使用简明教程- AsyncSocket
- collection.toArray(new String[0])中new String[0]的作用
- Android数据库高手秘籍(七)——体验LitePal的查询艺术
- unity 内存优化
- php 引用详解
- java中transient关键字
- WinCE 下 RETAILMSG 如何能在串口工具(SSCOM) 中输出汉字
- 【转】俞军给淘宝产品经理的分享
- Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程
- 简单爬虫模拟登陆phpmyadmin
- CentOS常用的文件操作命令总结
- spring初步学习---注入配置文件变量