RecyclerView基本用法学习笔记(一)
实现简单的水果名字对应图片列表
运行效果:

首先添加RecyclerView引用
在项目的gradle中添加如下:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
当然了每个人的版本可能不一样,你的recyclerview的support库要一致。
第二步修改activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_test"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
第三步添加数据实体类
新建Fruit类
package com.example.recyclerviewtest;
public class Fruit {
private String name;
private int imageId;
//这里的get,set方法可以通过快捷施法alt+insert
//选择getandset然后全选即可自动生成
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {

本文详细介绍如何使用RecyclerView在Android中创建带有图片的水果列表。从添加依赖到创建适配器,一步步教你实现动态列表布局。
最低0.47元/天 解锁文章
1995

被折叠的 条评论
为什么被折叠?



