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){
}
}
-
创建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")
}
}
-
在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] }
-
在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()