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

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

基础使用

  • 首先在xml里引入RecycleView


    

    

  • 编写adapter,分以下几步

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

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


    

3.在MyHolder里获取到progressBar

class MyAdapter(val data:List) {
    class MyHolder(view:View):ViewHolder(view){
        val progressBar:ProgressBar = view.findViewById(R.id.progress)
    }
}

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

class MyAdapter(val data:List):Adapter() {
    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]
    }
  1. 在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文件内容



    
    
        
            
            
        
    

    
    
        
            
                
                
            
        
    

解决办法:
在onBindViewHolder时调用 holder.ProgressBar.progressDrawable.mutate()

  override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.ProgressBar.progressDrawable.mutate()

你可能感兴趣的:(android)