网络请求列表

This commit is contained in:
nikiii 2024-06-20 11:07:21 +08:00
parent 0f733431ff
commit f90ca73d19
6 changed files with 107 additions and 42 deletions

View File

@ -30,6 +30,7 @@ android {
dependencies {
implementation ("com.google.code.gson:gson:2.10.1")
implementation ("io.github.lucksiege:pictureselector:v3.11.2")
implementation ("io.github.lucksiege:compress:v3.11.2")

View File

@ -38,6 +38,9 @@
android:theme="@style/Theme.MyApplication"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".ApproveActivity"
android:exported="false" />
<activity
android:name=".DealActivity"
android:exported="false" />
@ -47,9 +50,7 @@
<activity
android:name=".PersonActivity"
android:exported="false" />
<activity
android:name=".DrawerFragment"
android:exported="false" />
<activity
android:name=".NewArticleActivity"
android:exported="false" />

View File

@ -1,8 +1,25 @@
package com.example.myapplication;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Chat {
public String name;
public String content;
public String time;
public String imgurl;
public String headimg;
public List<ChatlistBean> chatlist;
public static class ChatlistBean {
public String head;
@SerializedName("name")
public String nameX;
@SerializedName("content")
public String contentX;
@SerializedName("time")
public String timeX;
}
}

View File

@ -1,5 +1,7 @@
package com.example.myapplication;
import static com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread;
import android.os.Bundle;
import androidx.annotation.NonNull;
@ -16,15 +18,24 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Community4Fragment extends Fragment {
RecyclerView recyclerView;
MyAdapter myAdapter;
List<Chat> chatList = new ArrayList<>();
RecyclerView recyclerView1;
MyAdapter myAdapter1;
List<Chat.ChatlistBean> chatList1 = new ArrayList<>();
@Override
@ -32,61 +43,95 @@ public class Community4Fragment extends Fragment {
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_community4, container, false);
for (int i=0;i<=20;i++){
Chat chat=new Chat();
chat.name = "姓名"+i;
chat.content = "聊天内容"+i;
if(i%2==0) {
chat.imgurl = "https://alifei01.cfp.cn/creative/vcg/800/new/VCG41N1499295253.jpg";
}else{
chat.imgurl="https://alifei02.cfp.cn/creative/vcg/800/new/VCG211184546094.jpg";
recyclerView1 = view.findViewById(R.id.recycleview1);
myAdapter1=new MyAdapter();
recyclerView1.setAdapter(myAdapter1);
recyclerView1.setLayoutManager(new LinearLayoutManager(getActivity()));
Request request = new Request.Builder()
.url("https://dev.usemock.com/663d936381caac589ccb3c6b/user1")
.get()
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
//请求失败
}
chatList.add(chat);
}
recyclerView = view.findViewById(R.id.recycleview);
myAdapter=new MyAdapter();
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//请求成功-子线程
String result = response.body().string();
//ui操作必须要在主线程
//切换操作
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
// JSONObject jsonObject =new JSONObject(result);
Gson gson = new Gson();
Chat chatTest = gson.fromJson(result, Chat.class);
chatList1.addAll(chatTest.chatlist);
myAdapter1.notifyDataSetChanged();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
});
return view;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
TextView textView;
TextView contenttextView;
TextView timetextView;
ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
timetextView = itemView.findViewById(R.id.textView33);
imageView = itemView.findViewById(R.id.imageView48);
contenttextView=itemView.findViewById(R.id.textView20);
nameTextView=itemView.findViewById(R.id.textView19);
textView=itemView.findViewById(R.id.textView20);
imageView=itemView.findViewById(R.id.imageView48);
}
}
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(getActivity())
.inflate(R.layout.chat_item_layout,parent,false);
View view = LayoutInflater.from(getActivity())
.inflate(R.layout.chat_item_layout, parent, false);
return new MyViewHolder(view);
}
//item中的控件设置值 点击事件
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Chat chat =chatList.get(position);
holder.nameTextView.setText(chat.name);
holder.textView.setText(chat.content);
Chat.ChatlistBean chat = chatList1.get(position);
holder.nameTextView.setText(chat.nameX);
holder.contenttextView.setText(chat.contentX);
holder.timetextView.setText(chat.timeX);
Glide.with(getActivity())
.load(chat.imgurl)
.load(chat.head)
.apply(RequestOptions.bitmapTransform(new CircleCrop()))
.into(holder.imageView);
}
@Override
public int getItemCount() {
return chatList.size();
return chatList1.size();
}
}
}

View File

@ -48,13 +48,14 @@
app:layout_constraintTop_toTopOf="@+id/textView20"
app:srcCompat="@drawable/like" />
<ImageView
android:id="@+id/imageView50"
android:layout_width="77dp"
android:layout_height="30dp"
app:layout_constraintEnd_toEndOf="@+id/imageView49"
app:layout_constraintStart_toStartOf="@+id/imageView49"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/helpo" />
<TextView
android:id="@+id/textView33"
android:layout_width="85dp"
android:layout_height="27dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -7,7 +7,7 @@
tools:context=".Community4Fragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview"
android:id="@+id/recycleview1"
android:layout_width="match_parent"
android:layout_height="1500dp"
app:layout_constraintEnd_toEndOf="parent"