recycleview可以说是listview(已经淘汰)的强化版,也是学完了listview才来的recycleview。其有强制的四级缓存机制,强制要求开发者使用优化。
注意:item的高度在listview和recycleview,代表意义不同,因为两者加载方式不同的原因。前者item高度不会生效,会自动采用wrap_content,后者是item高度会生效,指定item多高就是多高(可以自己采用wrap_content或者自己设定指定高度)。(被自己蠢哭过,)
目录
源码
activity:
package com.example.myapplication3;
import androidx.annotation.DrawableRes;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<Chat> list = new ArrayList<>();
RecyclerView mRecyclerView;
String[] na = {"秋秋", "彭彭彭彭", "我又发现", "实验教学准备兼职", "兼职文员", "此群涉嫌违规禁止访问",
"京东JD.COM", "test", "test", "test", "test", "test"};
String[] co = {"马上看完了", "312", "http://ffm.qq.com", "学刚:新年快乐", "鹿订记结账小姐姐:[OK]",
"徐一凡:[发呆]", "iPhone全场抢卷立减700元", "test", "test", "test", "test", "test"};
String[] ti = {"13:00", "星期二", "2020/1/27", "2020/1/1", "2019/4/27", "13:06:", "13:01", "test",
"test", "test", "test", "test"};
int[] id = new int[100];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recyclerView);
id[0] = (int) R.drawable.b;
id[1] = (int) R.drawable.a1;
id[2] = (int) R.drawable.a2;
id[3] = (int) R.drawable.a3;
id[4] = (int) R.drawable.b1;
id[5] = (int) R.drawable.b3;
id[6] = (int) R.drawable.b4;
id[7] = (int) R.drawable.b4;
id[8] = (int) R.drawable.b4;
id[9] = (int) R.drawable.b4;
id[10] = (int) R.drawable.b4;
id[11] = (int) R.drawable.b4;
for (int i = 0; i < 12; i++) {
Chat chat = new Chat();
chat.name = na[i];
chat.content = co[i];
chat.time = ti[i];
chat.pic = id[i];
list.add(chat);
}
mRecyclerView.setAdapter(new Myadapter());
//
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public class Myadapter extends RecyclerView.Adapter<MyRcycleviewhoder> {
@NonNull
@Override
public MyRcycleviewhoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 创建item,及其缓存类
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parent, false);
MyRcycleviewhoder myRcycleviewhoder = new MyRcycleviewhoder(view);
return myRcycleviewhoder;
}
@Override
public void onBindViewHolder(@NonNull MyRcycleviewhoder holder, int position) {
Chat chat = list.get(position);
holder.name.setText(chat.name);
holder.content.setText(chat.content);
holder.time.setText(chat.time);
holder.pic.setImageDrawable(getResources().getDrawable((int) chat.pic));
}
@Override
public int getItemCount() {
return list.size();
}
}
public class MyRcycleviewhoder extends RecyclerView.ViewHolder {//缓存处理4级
public TextView name;
public TextView content;
public TextView time;
public ImageView pic;
public MyRcycleviewhoder(@NonNull View itemView) {
super(itemView);//初始化模块
name = itemView.findViewById(R.id.name);
content = itemView.findViewById(R.id.content);
time = itemView.findViewById(R.id.time);
pic = itemView.findViewById(R.id.pic);
}
}
}
包装类
package com.example.myapplication3;
import java.util.Date;
public class Chat {
public String name;
public String content;
public String time;
public int pic;
}
layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#ededed"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView4"
app:layout_constraintVertical_bias="0.5">
</androidx.recyclerview.widget.RecyclerView>
<ImageView
android:id="@+id/imageView2"
android:layout_width="72dp"
android:layout_height="47dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ab" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="57dp"
android:layout_height="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ac" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:srcCompat="@drawable/aa" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:srcCompat="@drawable/ad" />
<ImageView
android:id="@+id/imageView"
android:layout_width="409dp"
android:layout_height="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/q" />
</androidx.constraintlayout.widget.ConstraintLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/a1" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="TextView"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/pic"
app:layout_constraintTop_toTopOf="@+id/pic" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="17sp"
app:layout_constraintBottom_toBottomOf="@+id/pic"
app:layout_constraintStart_toStartOf="@+id/name" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginRight="21dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>
备注:
至于drawable里面的图片资源就没有弄上来了,这不是重点。