使用recycleview添加微信訊息串列
- 1. 將支持庫添加到 dependencies 部分,
- 2. 將 RecyclerView 添加到布局中
- 2.1 將微信fragment的布局改為recycleView布局
- 2.2 創建顯示資料
- 3. 添加配接器
- 4. 實驗結果
1. 將支持庫添加到 dependencies 部分,
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
2. 將 RecyclerView 添加到布局中
2.1 將微信fragment的布局改為recycleView布局
現在需要將 RecyclerView 添加到上次的微信fragment對應的布局檔案中,使用 RecyclerView 作為整個布局的唯一視圖:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<!-- <TextView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:gravity="center"-->
<!-- android:text="這是微信聊天界面"-->
<!-- android:textSize="30sp"-->
<!-- android:textStyle="bold"/>-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/weixin_recycler_view"
android:layout_width="match_parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在布局中添加了 weixinRecycleViewAdapter 微件之后,需要獲取物件句柄,將其連接到布局管理器,并為要顯示的資料附加配接器,
代碼大致結構如下:
package com.example.myapplication.fragment;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import com.example.myapplication.adapter.weixinRecycleViewAdapter;
import com.example.myapplication.entity.weixin;
import java.util.ArrayList;
public class weixinFragment extends Fragment {
private View view;
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
public weixinFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view=inflater.inflate(R.layout.table01, container, false);
initRecyclerView();
return view;
}
private void initRecyclerView() {
recyclerView = (RecyclerView) view.findViewById(R.id.weixin_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
// specify an adapter (see also next example)
ArrayList<weixin> weixinList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
weixin weixin = new weixin(
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1604377714766&di=047992da111aa3829771424516bd6cdd&imgtype=0&src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F02%2F42%2F32%2F59ba3c2b4d7da_610.jpg",
"小明",
"8:00",
"這是一條訊息"
);
weixinList.add(weixin);
}
mAdapter = new weixinRecycleViewAdapter(weixinList,getActivity());
recyclerView.setAdapter(mAdapter);
}
}
2.2 創建顯示資料
我們要顯示的資料應該如下:

這里有四個部分,頭像,名稱,最后一條訊息,最后一次回復時間
我們先對其進行布局,代碼和效果如下
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/weixin_item"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--頭像-->
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/weixin_avatar"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/roundedCornerStyle"
android:layout_marginLeft="20dp" />
<!--名稱-->
<TextView
android:id="@+id/weixin_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="receiver name"
app:layout_constraintStart_toEndOf="@+id/weixin_avatar"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="20dp" />
<!--最后的聊天內容-->
<TextView
android:id="@+id/weixin_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginStart="20dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="news content"
app:layout_constraintStart_toEndOf="@+id/weixin_avatar"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginLeft="20dp" />
<!--最后的通信時間-->
<TextView
android:id="@+id/weixin_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

創建一個對應的物體類weixin
package com.example.myapplication.entity;
import java.util.Date;
public class weixin {
// 頭像鏈接
public String avatar;
//姓名
private String name;
// 最后通信時間
private String lastTime;
//最后通信內容
private String Content;
public weixin(String avatar, String name, String lastTime, String content) {
this.avatar = avatar;
this.name = name;
this.lastTime = lastTime;
Content = content;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastTime() {
return lastTime;
}
public void setLastTime(String lastTime) {
this.lastTime = lastTime;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
}
然后我們就可以創建資料物件,將其傳入配接器
ArrayList<weixin> weixinList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
weixin weixin = new weixin(
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1604377714766&di=047992da111aa3829771424516bd6cdd&imgtype=0&src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F02%2F42%2F32%2F59ba3c2b4d7da_610.jpg",
"小明",
"8:00",
"這是一條訊息"
);
weixinList.add(weixin);
}
mAdapter = new weixinRecycleViewAdapter(weixinList,getActivity());
3. 添加配接器
要將所有資料輸入串列中,必須擴展 RecyclerView.Adapter 類,此物件會創建項的視圖,并在原始項不再可見時用新資料項替換部分視圖的內容,
這里將weixinList的資料傳入,然后替換視圖中的內容
package com.example.myapplication.adapter;
import android.content.Context;
import android.util.Log;
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.myapplication.R;
import com.example.myapplication.entity.weixin;
import java.util.List;
public class weixinRecycleViewAdapter extends RecyclerView.Adapter<weixinRecycleViewAdapter.MyViewHolder> {
private List<weixin> wexinList;
private Context context;
// 為每個資料項提供對視圖的參考
// 復雜的資料項每個項可能需要多個View,可以提供對ViewHolder中某個資料項的所有View的訪問
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView weixinName;
TextView weixinLastTime;
TextView weixinContent;
ImageView weixinAvatar;
public MyViewHolder(View itemView) {
super(itemView);
weixinName = itemView.findViewById(R.id.weixin_name);
weixinLastTime = itemView.findViewById(R.id.weixin_time);
weixinContent = itemView.findViewById(R.id.weixin_content);
weixinAvatar = itemView.findViewById(R.id.weixin_avatar);
}
}
//提供合適的建構式(取決于資料集的型別)
public weixinRecycleViewAdapter(List<weixin> weixinList,Context context) {
this.wexinList=weixinList;
this.context = context;
}
//創建新視圖(由布局管理器呼叫)
@Override
public weixinRecycleViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// 創建一個新view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tb01viewtext, parent, false);
return new MyViewHolder(v);
}
// 替換視圖的內容(由布局管理器呼叫)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// 從此位置的資料集中獲取元素
// 用該元素替換視圖的內容
weixin weixin = this.wexinList.get(position);
holder.weixinName.setText(weixin.getName());
holder.weixinLastTime.setText(weixin.getLastTime());
holder.weixinContent.setText(weixin.getContent());
Glide.with(context)
.load(weixin.getAvatar())//加載圖片鏈接
.into(holder.weixinAvatar);//將圖片放進imageText
}
// 回傳資料集的大小(由布局管理器呼叫)
@Override
public int getItemCount() {
return this.wexinList.size();
}
}
這里對于圖片的加載用了外部依賴,對于Glide的具體解釋可以看上面代碼注解
dependencies {
implementation 'com.google.android.material:material:1.2.1'
implementation 'com.github.bumptech.glide:glide:4.11.0'
}
另外,在加載圖片時,要使用網路,所以要授予通網權限
可以在manifests中

添加
<uses-permission android:name="android.permission.INTERNET" />

最后寫完配接器,傳入weixinList的資料創建配接器,給recycleViews設定配接器
weixinRCAdapter = new weixinRecycleViewAdapter(weixinList,getActivity());
recyclerView.setAdapter(weixinRCAdapter);
4. 實驗結果
附上代碼倉庫地址:
https://gitee.com/hu_hong_tao/android-development-experiment
運行界面如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/203172.html
標籤:其他
上一篇:只因拎不清 插件化 和 熱修復 的實作原理,我痛失了阿里的offer
下一篇:IPC機制之了解AIDL(一)
