一、Recyclerview简介
RecyclerView 可以让您轻松高效地显示大量数据。您提供数据并定义每个列表项的外观,而 RecyclerView 库会根据需要动态创建元素。
顾名思义,RecyclerView 会回收这些单个的元素。当列表项滚动出屏幕时,RecyclerView 不会销毁其视图。相反,RecyclerView 会对屏幕上滚动的新列表项重用该视图。这种重用可以显著提高性能,改善应用响应能力并降低功耗。
使用Recyclerview
下面用仿微信布局来举例子
引入Recyclerview
implementation 'androidx.recyclerview:recyclerview:1.1.0'
在布局文件中添加Recyclerview控件
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
定义一个Wechat类,并且将相关资源放入drawable文件夹
package com.example.layout;
public class Wechat {
private String name;
private int imageId;
public Wechat(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
创建子布局wechat_item
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#191919"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.example.layout.CustomRoundAngleImageView
android:id="@+id/we_image"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="12dp"
android:orientation="vertical"
android:layout_weight="3">
<TextView
android:id="@+id/nick_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="阿巴阿巴"
android:textColor="#fafafa"
android:textSize="16sp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="4dp"
android:text="阿巴阿巴"
android:textColor="#484848"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="12dp"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="right">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="晚上9:43"
android:textColor="#484848"
android:textSize="12sp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right"
android:layout_marginTop="6dp"
android:src="@drawable/dnd"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
创建一个适配器类WechatAdapter继承自RecyclerView.Adapter<WechatAdapter.ViewHolder>ViewHolder为匿名类
定义 Adapter 时,您需要替换三个关键方法:
- onCreateViewHolder():每当 RecyclerView 需要创建新的 ViewHolder 时,它都会调用此方法。此方法会创建并初始化 ViewHolder 及其关联的 View,但不会填充视图的内容,因为 ViewHolder 此时尚未绑定到具体数据。
- onBindViewHolder():RecyclerView 调用此方法将 ViewHolder 与数据相关联。此方法会提取适当的数据,并使用该数据填充 ViewHolder 的布局。例如,如果 RecyclerView 显示的是一个名称列表,该方法可能会在列表中查找适当的名称,并填充 ViewHolder 的 TextView 微件。
- getItemCount():RecyclerView 调用此方法来获取数据集的大小。例如,在通讯簿应用中,这可能是地址总数。RecyclerView 使用此方法来确定什么时候没有更多的列表项可以显示。
package com.example.layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class WechatAdapter extends RecyclerView.Adapter<WechatAdapter.ViewHolder> {
//定义列表来获取wechat数据
private List<Wechat> mWechatList;
static class ViewHolder extends RecyclerView.ViewHolder {
View wechatView;
ImageView wechatImage;
TextView wechatName;
public ViewHolder(View view) {
super(view);
wechatView = view;
//找到对应元素,方便后面做修改
wechatImage = view.findViewById(R.id.we_image);
wechatName = view.findViewById(R.id.nick_name);
}
}
//
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//使用子布局填充布局,因为子布局没有进行加载使用要用LayoutInflater来获取子布局的view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.wechat_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
//为view添加点击事件
holder.wechatView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Wechat wechat = mWechatList.get(position);
//弹窗提醒你点击了哪一个view
Toast.makeText(view.getContext(), "你点击了View"+ wechat.getName(), Toast.LENGTH_SHORT).show();
}
});
//为图片添加点击事件
holder.wechatImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Wechat wechat = mWechatList.get(position);
//弹窗提醒你点击了哪一个图片
Toast.makeText(view.getContext(), "你点击了图片"+ wechat.getName(), Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//获取具体的子布局
Wechat wechat = mWechatList.get(position);
//瀑布流布局
// if(position%3!=0){
// holder.wechatImage.setImageResource(R.drawable.ad);
// }
// else{
// holder.wechatImage.setImageResource(R.drawable.ganyu);
// }
//设置子布局的名字和图片
holder.wechatImage.setImageResource(wechat.getImageId());
holder.wechatName.setText(wechat.getName());
}
//获取总数
@Override
public int getItemCount() {
return mWechatList.size();
}
public WechatAdapter(List<Wechat> wechatList) {
mWechatList = wechatList;
}
}
修改Activity代码,设置RecyclerView管理器,并且与WechatAdapter进行绑定
package com.example.layout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Wechat> wechatList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化数据
initList();
//新建recyclerView 管理器,与子布局进行绑定
RecyclerView recyclerView = findViewById(R.id.recycler_view);
//设置布局的方式
LinearLayoutManager layoutManager= new LinearLayoutManager(this);//线性布局
//layoutManager.setOrientation(RecyclerView.HORIZONTAL);//线性布局水平呈现
//recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
//recyclerView.setLayoutManager(new GridLayoutManager(this,1));//网格布局
recyclerView.setLayoutManager(layoutManager);
//新建适配器实例
WechatAdapter adapter = new WechatAdapter(wechatList);
//将管理器和布局进行绑定
recyclerView.setAdapter(adapter);
//底部界面的切换逻辑
//点击第一个
View we = findViewById(R.id.message);
we.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView text_we = findViewById(R.id.text_we);
TextView text_ad = findViewById(R.id.text_ad);
TextView text_find = findViewById(R.id.text_find);
TextView text_me = findViewById(R.id.text_me);
ImageView wechat = findViewById(R.id.img_we);
ImageView ad = findViewById(R.id.img_ad);
ImageView find = findViewById(R.id.img_find);
ImageView me = findViewById(R.id.img_me);
text_we.setTextColor(0xff06c05f);
text_ad.setTextColor(0xfffafafa);
text_find.setTextColor(0xfffafafa);
text_me.setTextColor(0xfffafafa);
wechat.setImageResource(R.drawable.ic_message_focus);
ad.setImageResource(R.drawable.ic_ad);
find.setImageResource(R.drawable.ic_find);
me.setImageResource(R.drawable.ic_me);
}
});
//点击第二个
View ad = findViewById(R.id.address_list);
ad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView wechat = findViewById(R.id.img_we);
ImageView ad = findViewById(R.id.img_ad);
ImageView find = findViewById(R.id.img_find);
ImageView me = findViewById(R.id.img_me);
TextView text_we = findViewById(R.id.text_we);
TextView text_ad = findViewById(R.id.text_ad);
TextView text_find = findViewById(R.id.text_find);
TextView text_me = findViewById(R.id.text_me);
text_we.setTextColor(0xfffafafa);
text_ad.setTextColor(0xff06c05f);
text_find.setTextColor(0xfffafafa);
text_me.setTextColor(0xfffafafa);
wechat.setImageResource(R.drawable.ic_message);
ad.setImageResource(R.drawable.ic_ad_focus);
find.setImageResource(R.drawable.ic_find);
me.setImageResource(R.drawable.ic_me);
}
});
//点击第三个
View find = findViewById(R.id.find);
find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView wechat = findViewById(R.id.img_we);
ImageView ad = findViewById(R.id.img_ad);
ImageView find = findViewById(R.id.img_find);
ImageView me = findViewById(R.id.img_me);
TextView text_we = findViewById(R.id.text_we);
TextView text_ad = findViewById(R.id.text_ad);
TextView text_find = findViewById(R.id.text_find);
TextView text_me = findViewById(R.id.text_me);
text_we.setTextColor(0xfffafafa);
text_ad.setTextColor(0xfffafafa);
text_find.setTextColor(0xff06c05f);
text_me.setTextColor(0xfffafafa);
wechat.setImageResource(R.drawable.ic_message);
ad.setImageResource(R.drawable.ic_ad);
find.setImageResource(R.drawable.ic_find_focus);
me.setImageResource(R.drawable.ic_me);
}
});
//点击第四个
View me = findViewById(R.id.me);
me.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView wechat = findViewById(R.id.img_we);
ImageView ad = findViewById(R.id.img_ad);
ImageView find = findViewById(R.id.img_find);
ImageView me = findViewById(R.id.img_me);
TextView text_we = findViewById(R.id.text_we);
TextView text_ad = findViewById(R.id.text_ad);
TextView text_find = findViewById(R.id.text_find);
TextView text_me = findViewById(R.id.text_me);
text_we.setTextColor(0xfffafafa);
text_ad.setTextColor(0xfffafafa);
text_find.setTextColor(0xfffafafa);
text_me.setTextColor(0xff06c05f);
wechat.setImageResource(R.drawable.ic_message);
ad.setImageResource(R.drawable.ic_ad);
find.setImageResource(R.drawable.ic_find);
me.setImageResource(R.drawable.ic_me_focus);
}
});
}
//数据初始化
private void initList() {
Wechat ying = new Wechat("莹", R.drawable.ying);
wechatList.add(ying);
Wechat kong = new Wechat("空", R.drawable.kong);
wechatList.add(kong);
Wechat Babala = new Wechat("芭芭拉", R.drawable.babala);
wechatList.add(Babala);
Wechat babatos = new Wechat("巴巴托斯", R.drawable.babatos);
wechatList.add(babatos);
Wechat fexier = new Wechat("菲谢尔", R.drawable.fexier);
wechatList.add(fexier);
Wechat ganyu = new Wechat("甘雨", R.drawable.ganyu);
wechatList.add(ganyu);
Wechat keqing = new Wechat("刻晴", R.drawable.keqing);
wechatList.add(keqing);
Wechat klee = new Wechat("可莉", R.drawable.klee);
wechatList.add(klee);
Wechat qin = new Wechat("琴", R.drawable.qin);
wechatList.add(qin);
}
}