1. 项目涉及到的技术点
- 列表控件RecyclerView的使用
- 调用API获取网络数据
- Glide加载图片
- Handler的使用
- okhttp的使用
2. 代码实现过程
在上集中,已经使用TabLayout+ViewPager2把新闻分类滑动实现了,这集具体实现新闻列表
- 新闻布局fragment_tab_news.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".TabNewsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/news_list_item"
android:layout_height="match_parent"/>
</androidx.appcompat.widget.LinearLayoutCompat>
注意实现:app:layoutManager=“xxxxx” 两种设置方式:1. 在xml中直接设置 2. 通过代码设置 ,一定要设置,不然列表显示空白
- RecyclerView需要的item布局news_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/thumbnail_pic_s"
android:layout_width="110dp"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_height="80dp"/>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#222222"
android:text="国家市场监督管理总局拟规定:平台经营者应协助市场监管执法调查工作"/>
<TextView
android:id="@+id/author_name"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新闻来源"/>
<TextView
android:id="@+id/date"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新闻时间"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
- 适配器NewsListAdapter.java
public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.MyHolder> {
private List<NewsInfo.ResultDTO.DataDTO> mDataDTOList = new ArrayList<>();
private Context mContext;
public void setListData(List<NewsInfo.ResultDTO.DataDTO> listData) {
this.mDataDTOList = listData;
//这句话不能少,一定要调用
notifyDataSetChanged();
}
public NewsListAdapter(Context context) {
this.mContext = context;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//加载布局文件
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list_item, null);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
//绑定数据
NewsInfo.ResultDTO.DataDTO dataDTO = mDataDTOList.get(position);
holder.author_name.setText("来源:" + dataDTO.getAuthor_name());
holder.date.setText(dataDTO.getDate());
holder.title.setText(dataDTO.getTitle());
//加载图片
Glide.with(mContext).load(dataDTO.getThumbnail_pic_s()).error(R.mipmap.img_error).into(holder.thumbnail_pic_s);
//点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mOnItemClickListener) {
mOnItemClickListener.onItemClick(dataDTO, position);
}
}
});
}
@Override
public int getItemCount() {
return mDataDTOList.size();
}
static class MyHolder extends RecyclerView.ViewHolder {
ImageView thumbnail_pic_s;
TextView author_name;
TextView date;
TextView title;
public MyHolder(@NonNull View itemView) {
super(itemView);
thumbnail_pic_s = itemView.findViewById(R.id.thumbnail_pic_s);
author_name = itemView.findViewById(R.id.author_name);
date = itemView.findViewById(R.id.date);
title = itemView.findViewById(R.id.title);
}
}
private onItemClickListener mOnItemClickListener;
public void setOnItemClickListener(onItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public interface onItemClickListener {
void onItemClick(NewsInfo.ResultDTO.DataDTO dataDTO, int position);
}
}
- 新闻数据实体NewsInfo.java
public class NewsInfo {
private String reason;
private ResultDTO result;
private Integer error_code;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultDTO getResult() {
return result;
}
public void setResult(ResultDTO result) {
this.result = result;
}
public Integer getError_code() {
return error_code;
}
public void setError_code(Integer error_code) {
this.error_code = error_code;
}
public static class ResultDTO {
private String stat;
private List<DataDTO> data;
private String page;
private String pageSize;
public String getStat() {
return stat;
}
public void setStat(String stat) {
this.stat = stat;
}
public List<DataDTO> getData() {
return data;
}
public void setData(List<DataDTO> data) {
this.data = data;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
public static class DataDTO implements Serializable {
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 is_content;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getIs_content() {
return is_content;
}
public void setIs_content(String is_content) {
this.is_content = is_content;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
}
}
}
- TabNewsFragment.java页面
public class TabNewsFragment extends Fragment {
//key自己去申请
private String url = "http://v.juhe.cn/toutiao/index?key=xxxxxx&type=";
private View rootView;
private RecyclerView recyclerView;
private NewsListAdapter mNewsListAdapter;
private static final String ARG_PARAM = "title";
private String title;
private Handler mHandler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 100) {
String data = (String) msg.obj;
Log.d("------------------", "handleMessage: "+data);
NewsInfo newsInfo = new Gson().fromJson(data, NewsInfo.class);
if (newsInfo != null && newsInfo.getError_code() == 0) {
//逻辑处理
if (null != mNewsListAdapter) {
mNewsListAdapter.setListData(newsInfo.getResult().getData());
}
} else {
Toast.makeText(getActivity(), "获取数据失败,请稍后重试", Toast.LENGTH_SHORT).show();
}
}
}
};
public TabNewsFragment() {
}
public static TabNewsFragment newInstance(String param) {
TabNewsFragment fragment = new TabNewsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM, param);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString(ARG_PARAM);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_tab_news, container, false);
//初始化控件
recyclerView = rootView.findViewById(R.id.recyclerView);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//初始化适配器
mNewsListAdapter = new NewsListAdapter(getActivity());
//设置adapter
recyclerView.setAdapter(mNewsListAdapter);
//recyclerView列表点击事件
mNewsListAdapter.setOnItemClickListener(new NewsListAdapter.onItemClickListener() {
@Override
public void onItemClick(NewsInfo.ResultDTO.DataDTO dataDTO, int position) {
//跳转到详情页
Intent intent = new Intent(getActivity(), NewsDetailsActivity.class);
//传递对象的时候,该类一定要实现Serializable
intent.putExtra("dataDTO", dataDTO);
startActivity(intent);
}
});
//获取数据
getHttpData();
}
private void getHttpData() {
//创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//构构造Request对象
Request request = new Request.Builder()
.url(url + title)
.get()
.build();
//通过OkHttpClient和Request对象来构建Call对象
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.d("-----------------", "onFailure: " + e.toString());
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
String data = response.body().string();
// Log.d("----------------", "onResponse: "+data);
//不能在耗时操作操作里面更新UI,那么需要使用Handler
Message message = new Message();
message.what = 100;
message.obj = data;
//发送
mHandler.sendMessage(message);
}
});
}
}
3. 运行效果图
相关视频教程在某站上面(
标签:pic,return,String,app,分类,private,public,Android,void From: https://blog.csdn.net/jky_yihuangxing/article/details/140769377