请注明出处:http://blog.csdn.net/qq_23179075/article/details/78648703
“本文主要针对Android新手,大神请绕道…”
Retrofit2+RxJava2 进行网络请和json数据的解析,注解框架:Butterknife 8.5.1 如果以上的框架还有同学不知道怎么使用可以看看我写的另外两篇文章,主要是介绍了上面框架的简单使用。如果想深入研究请自行百度。还有强大的图片加载框架 : Glide
Retrofit2+RxJava2进行网络请求
最新butterknife 8.5.1注解框架构建步骤
我们需要的数据,因为是新闻头条的数据,所以我就在聚合数据上面申请了一个免费的新闻头条接口
这是头条的API接口:“http://v.juhe.cn/toutiao/index?type=top&key=a1a755458cc22f129942b34904feb820”。
通过网络请求会给我返回这样的一个Json数据:
{ "reason":"成功的返回", "result":{ "stat":"1", "data":[ { "uniquekey":"5f85f80847fc4709cee1ad9390b9ed9d", "title":"高虎城全面阐释中国成就 南开学子交流新时代新内涵", "date":"2017-11-20 21:47", "category":"头条", "author_name":"中国新闻网", "url":"http://mini.eastday.com/mobile/171120214733416.html", "thumbnail_pic_s":"http://07.imgmini.eastday.com/mobile/20171120/20171120214733_cc7ebdd495a11d003fd481317e23c036_3_mwpm_03200403.jpg", "thumbnail_pic_s02":"http://07.imgmini.eastday.com/mobile/20171120/20171120214733_cc7ebdd495a11d003fd481317e23c036_1_mwpm_03200403.jpg", "thumbnail_pic_s03":"http://07.imgmini.eastday.com/mobile/20171120/20171120214733_cc7ebdd495a11d003fd481317e23c036_2_mwpm_03200403.jpg" }, ] }, "error_code":0 }我们需要通过上面请求回来的json数据创建对应的bean类。这里我推荐 AndroidStudio 上的一个插件 GsonFormat 通过该插件可以根据json数据快速生成对应的类。
package com.example.administrator.myapplication.model; import java.util.List; /** * Created by zhengliang on 2017/11/20 0020. */ public class ListNewsVO{ /** * reason : 成功的返回 * result : * error_code : 0 */ private String reason; private ResultEntity result; private int error_code; public static class ResultEntity { /** * stat : 1 * data : [{...}] */ private String stat; private List<DataEntity> data; public static class DataEntity { /** * uniquekey : 5f85f80847fc4709cee1ad9390b9ed9d * title : 高虎城全面阐释中国成就 南开学子交流新时代新内涵 * date : 2017-11-20 21:47 * category : 头条 * author_name : 中国新闻网 * url : * thumbnail_pic_s02 : * thumbnail_pic_s03 : */ private String uniquekey; private String title; private String date; private String category; private String author_name; private String url; private String thumbnail_pic_s; private String thumbnail_pic_s02; private String thumbnail_pic_s03; } } }为了减少篇幅,bean对象的getter和setter方法就没贴出来了!
创建一个现实新闻item的布局文件 item_news.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/ll_item" android:foreground="?android:selectableItemBackgroundBorderless" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" tools:targetApi="lollipop"> <ImageView android:id="@+id/iv_img" android:layout_width="80dp" android:layout_height="80dp" /> <LinearLayout android:layout_marginLeft="8dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textSize="18dp"/> <TextView android:id="@+id/tv_date" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dp"/> </LinearLayout> </LinearLayout> <View android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="1px" android:background="#333333"/> </RelativeLayout>通过 item_news.xml 布局文件创建自定义的Adapter类:NewsItemListAdapter.java 继承 RecyclerView.Adapter<> 类。
/** * Created by zhengliang on 2017/11/20 0020. */ public class NewsItemListAdapter extends RecyclerView.Adapter<NewsItemListAdapter.MyViewHolder> { private Context context; private List<ListNewsVO.ResultEntity.DataEntity> newsList = new ArrayList<>(); private OnItemClickListener listener; public NewsItemListAdapter(Context context, ListNewsVO response) { this.context = context; if (response != null && response.getResult() != null) { this.newsList = response.getResult().getData(); } } public void setListener(OnItemClickListener listener) { this.listener = listener; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.itme_news, parent, false)); } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { ListNewsVO.ResultEntity.DataEntity news = newsList.get(position); holder.tvTitle.setText(news.getTitle()); holder.tvDate.setText(news.getDate()); //加载图片 Glide.with(context) .load(news.getThumbnail_pic_s()) .crossFade() .diskCacheStrategy(DiskCacheStrategy.ALL)//开启缓存 .into(holder.ivImg); //设置Item的点击事件 if (this.listener != null) { holder.LLItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onClick(view,position); } }); } } @Override public int getItemCount() { return this.newsList.size(); } static class MyViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.tv_title) TextView tvTitle; @BindView(R.id.tv_date) TextView tvDate; @BindView(R.id.iv_img) ImageView ivImg; @BindView(R.id.ll_item) LinearLayout LLItem; MyViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } /** * 自定义Item的点击事件接口 */ public interface OnItemClickListener{ void onClick(View view,int position); } }其他的代码源码里面写的很清楚的,这里就不多赘述了! 源码地址:News_AndroidStudio源代码
最后看一下效果图: