RecyclerView基本用法

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

实现简单的水果名字对应图片列表

运行效果:
在这里插入图片描述

首先添加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) {
   
   
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪の星空朝酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值