From f90ca73d19a977af03005b559277c691444d59eb Mon Sep 17 00:00:00 2001 From: nikiii <2460437170@qq.com> Date: Thu, 20 Jun 2024 11:07:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E8=AF=B7=E6=B1=82=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 + src/main/AndroidManifest.xml | 7 +- .../java/com/example/myapplication/Chat.java | 17 +++ .../myapplication/Community4Fragment.java | 105 +++++++++++++----- src/main/res/layout/chat_item_layout.xml | 17 +-- src/main/res/layout/fragment_community4.xml | 2 +- 6 files changed, 107 insertions(+), 42 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 27a7608..fe97848 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 6207544..9aa7a97 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -38,6 +38,9 @@ android:theme="@style/Theme.MyApplication" android:usesCleartextTraffic="true" tools:targetApi="31"> + @@ -47,9 +50,7 @@ - + diff --git a/src/main/java/com/example/myapplication/Chat.java b/src/main/java/com/example/myapplication/Chat.java index ee420ff..1131eab 100644 --- a/src/main/java/com/example/myapplication/Chat.java +++ b/src/main/java/com/example/myapplication/Chat.java @@ -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 chatlist; + + public static class ChatlistBean { + public String head; + @SerializedName("name") + public String nameX; + @SerializedName("content") + public String contentX; + @SerializedName("time") + public String timeX; + } } diff --git a/src/main/java/com/example/myapplication/Community4Fragment.java b/src/main/java/com/example/myapplication/Community4Fragment.java index 5007c46..fc62750 100644 --- a/src/main/java/com/example/myapplication/Community4Fragment.java +++ b/src/main/java/com/example/myapplication/Community4Fragment.java @@ -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 chatList = new ArrayList<>(); + RecyclerView recyclerView1; + MyAdapter myAdapter1; + List 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{ + + public class MyAdapter extends RecyclerView.Adapter { @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(); } } } \ No newline at end of file diff --git a/src/main/res/layout/chat_item_layout.xml b/src/main/res/layout/chat_item_layout.xml index c4e0851..b8ef8fb 100644 --- a/src/main/res/layout/chat_item_layout.xml +++ b/src/main/res/layout/chat_item_layout.xml @@ -48,13 +48,14 @@ app:layout_constraintTop_toTopOf="@+id/textView20" app:srcCompat="@drawable/like" /> - + + \ No newline at end of file diff --git a/src/main/res/layout/fragment_community4.xml b/src/main/res/layout/fragment_community4.xml index 9031638..5bae9b9 100644 --- a/src/main/res/layout/fragment_community4.xml +++ b/src/main/res/layout/fragment_community4.xml @@ -7,7 +7,7 @@ tools:context=".Community4Fragment">