自定义RecyclerView适配器不刷新视图notifyDataSetChange

时间:2018-03-16 11:41:17

标签: android android-recyclerview kotlin adapter notifydatasetchanged

我在两周内写了下面的代码。我真的很想学习,而且我正在努力。

我无法理解为什么我的适配器在调用brand intent方法时无法更新。

在logcat中,我可以看到我在编写搜索文本时更改了notifyDataChange()。我可以看到新的项目列表在过滤列表中。

你能让我的方式变得更容易吗?

FilterResult
open class MainAdapter (val coins: Array<Coin>, activity: Activity): RecyclerView.Adapter<CustomViewHolder>() , Filterable {

var resultList : Array<Coin>? = coins
val activity : Activity = activity

override fun getFilter(): Filter {
    return object : Filter() {
        override fun performFiltering(sText: CharSequence?): Filter.FilterResults {
            val filterResults = Filter.FilterResults()
            if (sText != null) {
                println("--------- "+sText.toString()+" ---------")

                resultList = coins.filter {
                    it.symbol.contains(sText,true)
                }.toTypedArray()

                for(i in resultList!!.indices)
                    println("***coin $i: "+resultList!![i].symbol+" = "+resultList!![i].price_usd)

                // Assign the data to the FilterResults
                filterResults.values = resultList
                filterResults.count = resultList?.size ?: 0
            }
            return filterResults
        }

        override fun publishResults(sText: CharSequence?,
                                    results: FilterResults?) {
            notifyDataSetChanged()
            println("notified")
        }
    }
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
    val li = LayoutInflater.from(parent.context)
    val cFR = li.inflate(R.layout.main_row,parent,false)
    return CustomViewHolder(cFR)
}

override fun getItemCount(): Int {
    return resultList!!.count()
}

override fun onBindViewHolder(holder: CustomViewHolder, position: Int){

    val coin = resultList!![position]
// Something..
    holder.coin = coin
}

 class CustomViewHolder (val view: View, var coin:Coin?=null):RecyclerView.ViewHolder(view){...}

1 个答案:

答案 0 :(得分:0)

您可以定义适配器类,如下面的示例代码...

class HeroAdapter(val item: MutableList<Hero>, val context: Context, val itemClick:OnRecyclerViewItemClickListener) : RecyclerView.Adapter<HeroAdapter.ItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemViewHolder {
    val v = LayoutInflater.from(parent?.context).inflate(R.layout.adapter_layout, parent, false)
    return ItemViewHolder(v)

}

var onClickListener: OnRecyclerViewItemClickListener? = null

open interface OnRecyclerViewItemClickListener {
    fun click(hero: Hero)
}

override fun onBindViewHolder(holder: ItemViewHolder?, position: Int) {
    var hero: Hero = item[position]
    onClickListener=itemClick
    holder?.mTvName?.text = hero.getName()
    holder?.mTvBio?.text = hero.getBio()
    holder?.mTvReal?.text = hero.getRealname()
    holder?.mTvFirst?.text = hero.getFirstappearance()
    holder?.mTvTeam?.text = hero.getTeam()
    holder?.mTvPublisher?.text = hero.getPublisher()
    holder?.mTvCreate?.text = hero.getCreatedby()
    Glide.with(context)
            .load(hero.getImageurl())
            .into(holder?.mIvImage)
    holder?.itemView?.setOnClickListener(View.OnClickListener {
        this.onClickListener?.click(hero)
    })
}

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

class ItemViewHolder : RecyclerView.ViewHolder {
    var mTvName: TextView? = null
    var mTvReal: TextView? = null
    var mTvCreate: TextView? = null
    var mTvBio: TextView? = null
    var mTvTeam: TextView? = null
    var mTvPublisher: TextView? = null
    var mTvFirst: TextView? = null
    var mIvImage: ImageView? = null

    constructor(itemView: View) : super(itemView) {
        mTvName = itemView.findViewById(R.id.alTvName)
        mTvReal = itemView.findViewById(R.id.alTvRealName)
        mTvFirst = itemView.findViewById(R.id.alTvFirst)
        mTvCreate = itemView.findViewById(R.id.alTvcreatedby)
        mTvBio = itemView.findViewById(R.id.alTvBio)
        mTvTeam = itemView.findViewById(R.id.alTvTeam)
        mTvPublisher = itemView.findViewById(R.id.alTvpublisher)
        mIvImage = itemView.findViewById(R.id.alIvUserImage)
    }
}

}

然后你可以通过以下代码通知适配器..

        adapter.notifyDataSetChanged();