目录
第一步:在activity_main.xml中添加RecycleView
第一步:在activity_main.xml中添加RecycleView
<?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=".MainActivity3">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_lb"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
第二步:创建子布局 news_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="200dp">
<ImageView
android:id="@+id/iv_pic"
android:layout_width="190dp"
android:scaleType="fitXY"
android:layout_height="190dp"
app:srcCompat="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_pic"
android:textSize="24sp"
android:text="TextView" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="@id/tv_title"
android:layout_alignEnd="@id/tv_title"
android:layout_marginTop="10dp"
android:text="TextView" />
</RelativeLayout>
第三步:创建实体类 News.java
package com.hopu.day41tage01.modl;
public class News {
private String title;
private String desc;
private String pic;
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public String getPic() {
return pic;
}
public News(String title, String desc, String pic) {
this.title = title;
this.desc = desc;
this.pic = pic;
}
}
第四步:创建适配器 NewsAdapter.java
package com.hopu.day41tage01.dataper;
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.hopu.day41tage01.R;
import com.hopu.day41tage01.modl.News;
import java.util.List;
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private List<News> list;
public NewsAdapter (List<News> list){
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
News news = list.get(position);
holder.tv_title.setText(news.getTitle());
holder.tv_desc.setText(news.getDesc());
Glide.with(holder.view).load(news.getPic()).into(holder.iv_pic);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView iv_pic;
TextView tv_title,tv_desc;
View view;
public ViewHolder(@NonNull View itemView) {
super(itemView);
iv_pic = itemView.findViewById(R.id.iv_pic);
tv_title = itemView.findViewById(R.id.tv_title);
tv_desc = itemView.findViewById(R.id.tv_desc);
this.view = itemView;
}
}
}
第五步:声明RecycleView
第六步:开始进行网络编程
第七步:传递数据,数据解析
第八步:当数据完毕后,给RecycleView设置适配器
package com.hopu.day41tage01;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.hopu.day41tage01.dataper.NewsAdapter;
import com.hopu.day41tage01.help.Connection;
import com.hopu.day41tage01.modl.News;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity3 extends AppCompatActivity {
private RecyclerView rv_lb;
private String result;
private List<News> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
rv_lb = findViewById(R.id.rv_lb);
intideat();
}
private void intideat() {
new Thread(new Runnable() {
@Override
public void run() {
Connection connection = new Connection();
result = connection.connection("https://api.vvhan.com/api/hotlist? type=36Ke");
handler.sendEmptyMessage(0x111);
}
}).start();
}
public Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 0x111){
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String title = jsonObject1.getString("title");
String desc = jsonObject1.getString("desc");
String pic = jsonObject1.getString("pic");
News news = new News(title,desc,pic);
list.add(news);
}
NewsAdapter newsAdapter = new NewsAdapter(list);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity3.this);
rv_lb.setLayoutManager(layoutManager);
rv_lb.setAdapter(newsAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
}
网络连接的帮助类
package com.hopu.day41tage01.help;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import com.hopu.day41tage01.modl.News;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Connection {
String mst = "";
public String connection(String url){
try {
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setDoOutput(false);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(true);
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(3000);
connection.connect();
int code = connection.getResponseCode();
if (code == 200){
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
mst += line;
}
bufferedReader.close();
}
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mst;
}
}