Please indicate the source: http://blog.csdn.net/qq_23179075/article/details/78648703
Implementing a simple news list in Android
When I first started writing CSDN, I wrote a simple news list in Android through ListView. Recently, I saw that many students who are just beginning to learn Android need source code. Unfortunately, the source code for that example is no longer available. So here's a rewrite with the source code attached.
Preface address: http://blog.csdn.net/qq_23179075/article/details/52201778
The IDE used in the previous example is Eclipse, and some libraries are older. The IDE used in this article is Android Studio 3.0 and some newer third-party libraries such as Retrofit 2, RxJava 2 and so on. Then the previous ListView is also replaced by Recycler View, which is currently recommended.
"This article is mainly aimed at novice Android, God please bypass..."
Third-party libraries used
Retrofit2+RxJava2 for network request and json data parsing, annotation framework: Butterknife 8.5.1 If the above framework and students do not know how to use can see the other two articles I wrote, mainly introduced the simple use of the above framework. If you want to do further research, please do Baidu by yourself. There's also a powerful picture loading framework: Glide
Retrofit2+RxJava2 for network requests
Latest Buerknife 8.5.1 Annotation Framework Construction Steps
Prepare data sources
We need data, because it's headline data, so I applied for a free headline interface on aggregated data.
This is the headline API interface:" http://v.juhe.cn/toutiao/index?type=top&key=a1a755458cc22f129942b34904feb820".
A Json data will be returned to me through a network request:
{
"reason":"Successful return",
"result":{
"stat":"1",
"data":[
{
"uniquekey":"5f85f80847fc4709cee1ad9390b9ed9d",
"title":"Gao Hucheng comprehensively explains the new connotation of the new era of Nankai student exchange",
"date":"2017-11-20 21:47",
"category":"Headlines",
"author_name":"China News Network",
"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
}
We need to create the corresponding bean class from the json data returned from the above request. Here I recommend GsonFormat, a plug-in on Android Studio, which can quickly generate corresponding classes based on json data.
package com.example.administrator.myapplication.model;
import java.util.List;
/**
* Created by zhengliang on 2017/11/20 0020.
*/
public class ListNewsVO{
/**
* reason : Successful return
* 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 : Gao Hucheng comprehensively explains the new connotation of the new era of Nankai student exchange
* date : 2017-11-20 21:47
* category : Headlines
* author_name : China News Network
* 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;
}
}
}
To reduce space, the getter and setter methods of the bean object are not posted!
XML layout file
Create a layout file item_news.xml for real news item
<?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>
Java file
Create a custom adapter class through item_news.xml layout file: NewsItemListAdapter.java inherits RecyclerView. Adapter<> class.
/**
* 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());
//Loading pictures
Glide.with(context)
.load(news.getThumbnail_pic_s())
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)//Open cache
.into(holder.ivImg);
//Setting Item Click Events
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);
}
}
/**
* Customize Item Click Event Interface
*/
public interface OnItemClickListener{
void onClick(View view,int position);
}
}
The other source codes are written very clearly, so I won't go into any more details here! uuuuuuuuuuu
Source address: News_Android Studio source code
Finally, take a look at the effect map: