Android 列表或网格形式展示大量数据:RecyclerView

目录

  1. RecyclerView是什么
  2. 如何使用
  3. RecyclerView 涉及到的类
  4. LayoutManager
  5. 为Item设置不同的布局样式
  6. 制作拖动的RecyclerView

一、RecyclerView是什么

RecyclerView是Android支持库中的一个控件,用于在列表或网格形式展示大量数据。它是ListView的升级版,提供了更灵活、可定制化的方式来展示和管理数据。

二、如何使用

RecyclerView的使用步骤:

  1. 创建RecyclerView控件对象
  2. 设置适配器 : 一般是使用自定义的适配器 , 设置给 RecyclerView 对象 ;
  3. 创建适配器的布局文件
  4. 创建并设置布局管理器 : 可以使用预置的布局管理器 , 也可以自定义布局管理器 ;

(1)创建控件


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/home_rv_product"
        android:layout_width="1500dp"
        android:layout_height="800dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

(2)创建适配器

class HomeProductAdapter(var productList:ArrayList<HomeProductBean>): RecyclerView.Adapter<HomeProductAdapter.MyViewHolder>() {
   
   

    inner class MyViewHolder(view: View): RecyclerView.ViewHolder(view){
   
   
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
   
   
       val view = LayoutInflater.from(parent.context).inflate(R.layout.home_item_home_product,parent,false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
   
   
        return productList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
   
   
    }
}
<?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"
    android:layout_width="200dp"
    android:layout_height="270dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    android:background="@drawable/dingdian_imge_price_bg">


    <ImageView
        android:id="@+id/iv_sugar"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:src
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前期后期

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

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

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

打赏作者

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

抵扣说明:

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

余额充值