RecyclerView实现加载多种条目类型,仿今日头条首页item
在项目开发中会有多种条目的加载需求,今天就来做个RecyclerView加载多条目的类型,仿照今日头条首页列表。先上图看一下
今日头条首页效果。
:
那么今天我们先来做两个条目,一个是带文字图片的,一个是带文字视频的。首先添加recyclerview依赖:
compile 'com.android.support:recyclerview-v7:21.0.0' 然后从activity中传入到adapter中的数据根据一个type字段判断这个item对象需要的视图,然后return出一个标记,在onCreateViewHolder中在引用所对应的item布局. @Override public int getItemViewType(int position) { mNews = mNewsList.get(position); String newsColumn = mNews.getNewsColumn(); if("党政新闻".equals(newsColumn)){ return TYPE_TEXT; }else { return TYPE_VIDEO; } } 一般有多少种类型我们定义多少种常量 public static final int TYPE_TEXT = 0; public static final int TYPE_VIDEO = 1;
有了上面定义的常量之后 在onCreateViewHolder里根据viewtype来判断 所引用的item布局类型 这样每一条item就会不一样了
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; if(viewType == TYPE_TEXT){ view =View.inflate(parent.getContext(),R.layout.item_news_content,null); return new TextViewHolder(view); }else if(viewType == TYPE_VIDEO){ view =View.inflate(parent.getContext(),R.layout.item_video_content,null); return new VideoViewHolder(view); } return null; } 然后在onBindViewHolder中加载处理不同的视图呈现的逻辑,接下来贴上完整的代码,便于大家参考。 activity中的代码: private void initView() { mRecyclerView = (RecyclerView) myNewsView.findViewById(R.id.myNewsListView_id); LinearLayoutManager manager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(manager); //2.当下次再次请求网络的时候,判断本地是否有保存数据,有,加载解析显示缓存数据, String sp_msg = SharePreferenceUtils.getString(getActivity(), "news", ""); if (!TextUtils.isEmpty(sp_msg)) { //加载解析显示数据 processJson(sp_msg); } //获取Web数据新闻列表 initWebData(); } //初始化Web数据 private void initWebData() { // 打开加载数据对话框 String getNewsDataURl = null; progressDialog = ProgressDialog.show(getActivity(), "请稍等...", "正在加载数据...", true); // getNewsDataURl = getString(R.string.getAllNewsByStatus); getNewsDataURl = "https://easydoc.xyz/mock/72378360/82993958/test.video"; MyGetHttpUtil.mySendHttpRequestUtils(getNewsDataURl, new MyHttpCallbackListenerUtil() { @Override public void onFinish(final String response) { //1.请求成功之后,保存数据到本地文件,sp SharePreferenceUtils.saveString(getActivity(), "news", response); //3.加载解析显示最新数据,将缓存数据覆盖 getActivity().runOnUiThread(new Runnable() { @Override public void run() { progressDialog.dismiss(); processJson(response); } }); } @Override public void onError(Exception e) { progressDialog.dismiss(); } }); } /** * 4解析json数据操作 */ private void processJson(String sp_msg) { mNewsList = new Gson().fromJson(sp_msg, new TypeToken<List<News>>() {}.getType()); mNewsAdapter = new NewsAdapter(Fragment_News.this, myNewsView.getContext(), mNewsList); mRecyclerView.setAdapter(mNewsAdapter); } adapter中的代码:
public class NewsAdapter extends RecyclerView.Adapter { private Fragment_News tab_news_Fragment; private Context mContext; private List<News> mNewsList; private News mNews; public static final int TYPE_TEXT = 0; public static final int TYPE_VIDEO = 1; public NewsAdapter(Fragment_News fragment_News, Context mContext, List<News> mNewsList) { super(); this.tab_news_Fragment = fragment_News; this.mContext = mContext; this.mNewsList = mNewsList; } @Override public int getItemCount() { return mNewsList.size(); } @Override public int getItemViewType(int position) { mNews = mNewsList.get(position); String newsColumn = mNews.getNewsColumn(); if("党政新闻".equals(newsColumn)){ return TYPE_TEXT; }else { return TYPE_VIDEO; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; if(viewType == TYPE_TEXT){ view =View.inflate(parent.getContext(),R.layout.item_news_content,null); return new TextViewHolder(view); }else if(viewType == TYPE_VIDEO){ view =View.inflate(parent.getContext(),R.layout.item_video_content,null); return new VideoViewHolder(view); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if(holder instanceof TextViewHolder){ TextViewHolder textViewHolder = (TextViewHolder) holder; textViewHolder.newsNameTextView.setText(mNews.getNewsTitle()); textViewHolder.newsContenTextView.setText(mNews.getNewsContent()); textViewHolder.newsColumnTextView.setText(mNews.getNewsColumn()); textViewHolder.newsDaTextView.setText(mNews.getNewsDate()); if (mNews.getNewsImgSrc() != null) { AsynImageLoader asynImageLoader = new AsynImageLoader(); String imgUrl = mNews.getNewsImgSrc(); asynImageLoader.showImageAsyn(textViewHolder.newsImageView, imgUrl, 0); } else { textViewHolder.newsImageView.setImageResource(R.drawable.errornotes_pressed); } textViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext, NewsDetailActivity.class); //将当前选中的试卷信息进行传到下一个界面 News mNews = mNewsList.get(position); intent.putExtra("currentChoiceNews", mNews); mContext.startActivity(intent); } }); }else { VideoViewHolder videoViewHolder = (VideoViewHolder) holder; videoViewHolder.newsNameTextView.setText(mNews.getNewsTitle()); videoViewHolder.newsContenTextView.setText(mNews.getNewsContent().trim()); videoViewHolder.newsColumnTextView.setText(mNews.getNewsColumn()); videoViewHolder.newsDaTextView.setText(mNews.getNewsDate()); if (mNews.getNewsImgSrc() != null) { AsynImageLoader asynImageLoader = new AsynImageLoader(); String imgUrl = mNews.getNewsImgSrc(); asynImageLoader.showImageAsyn(videoViewHolder.newsImageView, imgUrl, 0); } else { videoViewHolder.newsImageView.setImageResource(R.drawable.errornotes_pressed); } videoViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext, VideoDetailActivity.class); //将当前选中的试卷信息进行传到下一个界面 String newsContent = mNewsList.get(position).getNewsContent(); intent.putExtra("newsContent", newsContent); mContext.startActivity(intent); } }); } } final class TextViewHolder extends RecyclerView.ViewHolder{ TextView newsNameTextView; TextView newsContenTextView; TextView newsColumnTextView; TextView newsDaTextView; ImageView newsImageView; public TextViewHolder(View itemView) { super(itemView); newsNameTextView = (TextView) itemView.findViewById(R.id.item_News_id); newsContenTextView = (TextView) itemView.findViewById(R.id.item_News_desc); newsColumnTextView = (TextView) itemView.findViewById(R.id.item_news_column); newsDaTextView = (TextView) itemView.findViewById(R.id.itemn_news_date); newsImageView = (ImageView) itemView.findViewById(R.id.item_News_img); } } final class VideoViewHolder extends RecyclerView.ViewHolder{ TextView newsNameTextView; TextView newsContenTextView; TextView newsColumnTextView; TextView newsDaTextView; ImageView newsImageView; public VideoViewHolder(View itemView) { super(itemView); newsNameTextView = (TextView) itemView.findViewById(R.id.item_News_id); newsContenTextView = (TextView) itemView.findViewById(R.id.item_News_desc); newsColumnTextView = (TextView) itemView.findViewById(R.id.item_news_column); newsDaTextView = (TextView) itemView.findViewById(R.id.itemn_news_date); newsImageView = (ImageView) itemView.findViewById(R.id.item_News_img); } } } 以上就是实现多条目布局的完整例子,希望对大家会有帮助,欢迎大家关注评论。
CSDN-Ada助手: 哇, 你的文章质量真不错,值得学习!不过这么高质量的文章, 还值得进一步提升, 以下的改进点你可以参考下: (1)提升标题与正文的相关性;(2)起一个更符合文章内容的标题。
hust_edu_csdn_123: 看不明白
亮亮在江湖: 不好意思,找不到完整的代码了
阿桔桃桃: 博主有完整代码吗?
zyhorse2010: 步数应该是直接获取硬件计步器的吧,而非通过加速度数据计算而来的吧