RecycleView基本使用及常见问题汇总

RecycleView属于android基础组件,比较常用,下面总结下其使用的基础流程及常见问题解决办法(常见问题会定期更新)

基础使用

  • 首先在xml里引入RecycleView

    <?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"
    tools:context=".RecycleTestActivity">
    <SeekBar android:id="@+id/sk" app:layout_constraintTop_toTopOf="parent" android:layout_width="match_parent" android:padding="20dp" android:layout_height="wrap_content"/>

    复制代码
      <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_marginTop="50dp"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scrollbars="vertical"
          app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

  • 编写adapter,分以下几步

1.创建自定义adapter类,创建内部类MyViewHolder,并实现RecycleView的ViewHolder,参数传递view

复制代码
class MyAdapter(val data:List<Int>) {
    class MyHolder(view:View):ViewHolder(view){
        
    }
}
  1. 创建item layout,里面根据需要创建对应的布局,这里假如里面仅有一个progressbar,注意根布局高度改为wrap content,否则的话实际效果每个item占用了一屏

    <?xml version="1.0" encoding="utf-8"?>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ProgressBar android:id="@+id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/Widget.AppCompat.ProgressBar.Horizontal" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

3.在MyHolder里获取到progressBar

复制代码
class MyAdapter(val data:List<Int>) {
    class MyHolder(view:View):ViewHolder(view){
        val progressBar:ProgressBar = view.findViewById(R.id.progress)
    }
}

4.MyAdapter继承Recycleview.Adapter,并实现对应的三个方法

复制代码
class MyAdapter(val data:List<Int>):Adapter<MyAdapter.MyHolder>() {
    class MyHolder(view:View):ViewHolder(view){
        val progressBar:ProgressBar = view.findViewById(R.id.progress)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        TODO("Not yet implemented")
    }

    override fun getItemCount(): Int {
        TODO("Not yet implemented")
    }

    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        TODO("Not yet implemented")
    }
}
  1. 在onCreateViewHolder里创建MyHolder并返回,在getItemCount里返回 data.size,在onBindViewHolder里绑定组件

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

    复制代码
     override fun getItemCount(): Int {
        return data.size
     }
    
     override fun onBindViewHolder(holder: MyHolder, position: Int) {
         holder.progressBar.progress = data[position]
     }
  2. 在activity里获取recycleView,设置adapter 设置manager,adapter创建时把数据传入

    recyclerView = findViewById(R.id.recyclerView)
    myAdapter = MyAdapter(list)
    recyclerView.adapter = myAdapter
    recyclerView.layoutManager = LinearLayoutManager(this)

遇到的问题

1.当item里有progress bar时,数据变化让progress bar进度条变化,此时没什么问题,但如果progress bar设置了自定义的progressDrawable,那么其中一个item的进度条变化就会导致别的item显示的progress进度出现异常

复制代码
android:progressDrawable="@drawable/progress_bg"

progress_bg文件内容

复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--背景色-->
    <item android:id="@+id/background">
        <shape>
            <corners android:radius="100px" />
            <solid android:color="@color/progress_track" />
        </shape>
    </item>

    <!--进度条色-->
    <item android:id="@+id/progress">
        <clip android:clipOrientation="horizontal">
            <shape>
                <corners android:radius="100px" />
                <solid android:color="@color/progress_indicator" />
            </shape>
        </clip>
    </item>
</layer-list>

解决办法:

在onBindViewHolder时调用 holder.ProgressBar.progressDrawable.mutate()

复制代码
  override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.ProgressBar.progressDrawable.mutate()
相关推荐
用户82492819253628 分钟前
把android资源类型详解
android
IT观测40 分钟前
深度分析俩款主流移动统计工具Appvue和openinstall
android·java·数据库
用户3386755819542 分钟前
Android 四种常用布局完全解析(附实战项目截图)
android
用户50875321684444 分钟前
Android 资源类型全解析:深入理解四种常用布局
android
XiaoLeisj1 小时前
Android 文件存储实战:从应用私有目录读写到网络文件落盘与公共存储接入
android·java·网络·文件操作
恋猫de小郭1 小时前
Android Studio Panda 2 ,支持 AI 用 Vibe Coding 创建项目
android·前端·flutter
zhouping@1 小时前
[极客大挑战 2020]Greatphp
android·ide·web安全·android studio
毕设源码-邱学长1 小时前
【开题答辩全过程】以 基于 Android的超市服务评价系统的设计与实现为例,包含答辩的问题和答案
android
子非鱼yy2 小时前
详解MySQL的MVCC:多版本并发控制核心原理与实战解析
android·adb
$Qw2 小时前
google firebase service account json
android·google