一.思维导图
一、添加依赖
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
二、让viewbinding为true
在build.gradle中添加
buildFeatures {
viewBinding true
}
三,写Bean类
package com.example.city_pro;
import java.util.List;
public class daohang {
private List<DataDTO> data;
private int errorCode;
private String errorMsg;
public List<DataDTO> getData() {
return data;
}
public void setData(List<DataDTO> data) {
this.data = data;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
@Override
public String toString() {
return "daohang{" +
"data=" + data +
", errorCode=" + errorCode +
", errorMsg='" + errorMsg + '\'' +
'}';
}
public static class DataDTO {
private List<?> articleList;
private String author;
private List<?> children;
private int courseId;
private String cover;
private String desc;
private int id;
private String lisense;
private String lisenseLink;
private String name;
private int order;
private int parentChapterId;
private int type;
private boolean userControlSetTop;
private int visible;
public List<?> getArticleList() {
return articleList;
}
public void setArticleList(List<?> articleList) {
this.articleList = articleList;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public List<?> getChildren() {
return children;
}
public void setChildren(List<?> children) {
this.children = children;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLisense() {
return lisense;
}
public void setLisense(String lisense) {
this.lisense = lisense;
}
public String getLisenseLink() {
return lisenseLink;
}
public void setLisenseLink(String lisenseLink) {
this.lisenseLink = lisenseLink;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public int getParentChapterId() {
return parentChapterId;
}
public void setParentChapterId(int parentChapterId) {
this.parentChapterId = parentChapterId;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public boolean isUserControlSetTop() {
return userControlSetTop;
}
public void setUserControlSetTop(boolean userControlSetTop) {
this.userControlSetTop = userControlSetTop;
}
public int getVisible() {
return visible;
}
public void setVisible(int visible) {
this.visible = visible;
}
@Override
public String toString() {
return "DataDTO{" +
"articleList=" + articleList +
", author='" + author + '\'' +
", children=" + children +
", courseId=" + courseId +
", cover='" + cover + '\'' +
", desc='" + desc + '\'' +
", id=" + id +
", lisense='" + lisense + '\'' +
", lisenseLink='" + lisenseLink + '\'' +
", name='" + name + '\'' +
", order=" + order +
", parentChapterId=" + parentChapterId +
", type=" + type +
", userControlSetTop=" + userControlSetTop +
", visible=" + visible +
'}';
}
}
}
四,写api接口
public interface Api {
@GET("/project/tree/json")
Call<daohang> dao();
}
五,创建视图
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/dao_re"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
六,绑定创建的视图
在Fragment中绑定需要使用ViewBinding进行绑定
FragmentABinding fragmentABinding =FragmentABinding.inflate(inflater,container,false);
View view= fragmentABinding.getRoot();
review = view.findViewById(R.id.dao_re);
七,给视图写适配器
package com.example.city_pro;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class adpter extends RecyclerView.Adapter<adpter.imhoder> {
public List<daohang.DataDTO> data=new ArrayList<>();
public void updata(daohang dao) {
data.clear();
data.addAll(dao.getData());
notifyDataSetChanged();
}
@NonNull
@Override
public adpter.imhoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
return new imhoder(view);
}
@Override
public void onBindViewHolder(@NonNull adpter.imhoder holder, int position) {
TextView te=holder.itemView.findViewById(R.id.te);
daohang.DataDTO D= data.get(position);
te.setText(D.getName().toString());
}
@Override
public int getItemCount() {
return data.size();
}
public class imhoder extends RecyclerView.ViewHolder {
public imhoder(@NonNull View itemView) {
super(itemView);
}
}
}
最后在aFragment中进行绑定适配器
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(null);
linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
adpter = new adpter();
review.setLayoutManager(linearLayoutManager);
review.setAdapter(adpter);
initjson();
return view;
八,写RetrofitManager管理请求的数据
public class RetrofitManager {
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.wanandroid.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
public static Retrofit getRetrofit(){
return retrofit;
}
}
九,申请json数据
private void initjson() {
Api api= RetrofitManager.getRetrofit().create(Api.class);
Call<daohang> task= api.dao();
task.enqueue(new Callback<daohang>() {
@Override
public void onResponse(Call<daohang> call, Response<daohang> response) {
if (response.code()== HttpURLConnection.HTTP_OK) {
daohang dao=response.body();
updata(dao);
}
}
@Override
public void onFailure(Call<daohang> call, Throwable t) {
}
});
}
private void updata(daohang dao) {
adpter.updata(dao);
}
}
十,在AndroidManifest.xml申请权限
切记不要忘了
在AndroidManifest.xml写
<uses-permission android:name="android.permission.INTERNET" />
总结
1,通过思维导图对基本的步骤进行了解
2,用到的组件RecyclerView
3,用到了申请网络的Retrofit框架
4.不要忘了添加权限