// 在我们的这个位置的话动态适配我们的网络相关数据:
第一步的话就是编写我们的布局文件:
// 在这个位置的话就是我们的详细的代码的编写:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.NewsActivity.NewsActivity4">
<!--在我们的这个位置的话设置我们的热门主题-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/hotService"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
然后的话就是我们的java部分的代码的编写:
package com.example.okhttpproject.ui.NewsActivity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.example.okhttpproject.R;
import com.example.okhttpproject.ui.Bean.HotServiceBean;
import com.example.okhttpproject.ui.https.HttpUtil;
import com.example.okhttpproject.ui.https.MyApplication;
import com.example.okhttpproject.ui.https.OkhttpCallBack;
import java.io.IOException;
public class NewsActivity4 extends AppCompatActivity {
private RecyclerView hotService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news4);
initData();
}
private void initData() {
hotService = (RecyclerView) findViewById(R.id.hotService);
initRecylerMiddle();
}
private void initRecylerMiddle() {
HttpUtil.getInstance().doGet("/press/press/list?pageNum=1&pageSize=10&pressCategory=48", new OkhttpCallBack() {
@Override
public void success(String successString) throws IOException {
// 然后的话就是将我们的东西设置进去
MyApplication.getMainHandler().post(()->{
// 然后的话使用我们的gson来解析我们的数据
HotServiceBean hotServiceBean = MyApplication.getGson().fromJson(successString,HotServiceBean.class);
hotService.setLayoutManager(new StaggeredGridLayoutManager(4, LinearLayoutManager.VERTICAL));
hotService.setAdapter(new HotServiceAdapter(hotServiceBean.getRows()));
});
}
@Override
public void failure(String errorString) {
}
});
}
}
然后的话就是再我们的这个位置的话设置我们的相关的适配器的代码:
package com.example.okhttpproject.ui.NewsActivity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.okhttpproject.R;
import com.example.okhttpproject.ui.Bean.HotServiceBean;
import com.example.okhttpproject.ui.https.HttpUtil;
import java.util.List;
public class HotServiceAdapter extends RecyclerView.Adapter<HotServiceAdapter.HotViewHolder> {
// todo 在我们的这个位置的创建了一个我们的列表
private List<HotServiceBean.RowsBean> list;
// todo 创建我们的构造方法
public HotServiceAdapter(List<HotServiceBean.RowsBean> list) {
this.list = list;
}
@NonNull
@Override
public HotServiceAdapter.HotViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_hoy_service,parent,false);
return new HotViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull HotServiceAdapter.HotViewHolder holder, int position) {
// 在我们的这个位置的话就是绑定我们的数据
HotServiceBean.RowsBean rowsBean = list.get(position);
Glide.with(holder.itemView.getContext()).load(HttpUtil.BASE_URL + rowsBean.getImgUrl()).into(holder.ivNews);
holder.tvHotServiceTitle.setText(rowsBean.getTitle());
// todo 在我们的这个位置的话应该是设置我们的跳转页面的复用
holder.itemView.setOnClickListener(v->{
Intent intent = new Intent(holder.itemView.getContext(),NewsActivity5.class);
intent.putExtra("imageurl", HttpUtil.BASE_URL + rowsBean.getImgUrl());
intent.putExtra("title", rowsBean.getTitle());
intent.putExtra("content", rowsBean.getContent());
holder.itemView.getContext().startActivity(intent);
});
}
@Override
public int getItemCount() {
return list.size();
}
public class HotViewHolder extends RecyclerView.ViewHolder {
private ImageView ivNews;
private TextView tvHotServiceTitle;
public HotViewHolder(@NonNull View itemView) {
super(itemView);
ivNews = (ImageView) itemView.findViewById(R.id.iv_news);
tvHotServiceTitle = (TextView) itemView.findViewById(R.id.tv_hot_service_title);
}
}
}
// 然后的话就是我们的相关模板文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="8dp"
android:layout_height="wrap_content">
<!--然后的话在我们的这个位置的话就是设置我们的相关的东西-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_margin="4dp"
android:singleLine="true"
android:ellipsize="end"
android:id="@+id/tv_hot_service_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
在我们的这个位置的话然后的话就是的话就是将我们的东西效果编写完成了