一. 在build.gradle中添加依赖
implementation 'androidx.recyclerview:recyclerview:1.1.0'
二. activity_main.xml
<?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"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
三. list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:rowCount="2"
android:columnCount="2">
<ImageView
android:id="@+id/img_view"
android:paddingLeft="10dp"
android:layout_gravity="center_vertical"
android:layout_rowSpan="2">
</ImageView>
<TextView
android:id="@+id/tv2"
android:layout_marginLeft="10dp"
android:text="111">
</TextView>
<TextView
android:id="@+id/tv3"
android:layout_marginLeft="10dp"
android:text="222">
</TextView>
</GridLayout>
四. Animal
package com.sxvbd.helloworld;
public class Animal {
private String aName;
private String aSpeak;
private int aIcon;
public Animal(String aName, String aSpeak, int aIcon) {
this.aName = aName;
this.aSpeak = aSpeak;
this.aIcon = aIcon;
}
public String getaName() {
return aName;
}
public void setaName(String aName) {
this.aName = aName;
}
public String getaSpeak() {
return aSpeak;
}
public void setaSpeak(String aSpeak) {
this.aSpeak = aSpeak;
}
public int getaIcon() {
return aIcon;
}
public void setaIcon(int aIcon) {
this.aIcon = aIcon;
}
}
五. RecycleAnimalAdapter
public class RecycleAnimalAdapter extends RecyclerView.Adapter<RecycleAnimalAdapter.ViewHolder> {
private List<Animal> animalList;
private Context context;
public RecycleAnimalAdapter(List<Animal> animalList, Context context) {
this.animalList = animalList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.list_item, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.imgIcon.setImageResource(animalList.get(position).getaIcon());
holder.nameTextView.setText(animalList.get(position).getaName());
holder.speakTextdView.setText(animalList.get(position).getaSpeak());
}
@Override
public int getItemCount() {
return animalList == null ? 0 : animalList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgIcon;
TextView nameTextView;
TextView speakTextdView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imgIcon = itemView.findViewById(R.id.img_view);
nameTextView = itemView.findViewById(R.id.tv2);
speakTextdView = (TextView) itemView.findViewById(R.id.tv3);
}
}
}
六. MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "wangyi";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Animal> animalList = new ArrayList<>();
animalList.add(new Animal("狗说", "你是狗么?", R.mipmap.dog));
animalList.add(new Animal("牛说", "你是牛么?", R.mipmap.cattle));
animalList.add(new Animal("鸭说", "你是鸭么?", R.mipmap.duck));
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(new RecycleAnimalAdapter(animalList,this));
}
}
七. 效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/08462078213b1b6147f29d9a3570b2cb.png)