notifyDatasetChanged()
无法正常工作,但不能
notifydatasetChanged()
,如果我将数据传递给eventList
初始化recyclerview
列表后直接添加适配器。ActEvents.kt
class ActEvents : AppCompatActivity(){
// Initializing an empty ArrayList to be filled with animals
var eventList: MutableList<TestModel> = ArrayList()
@set:Inject var retrofit: Retrofit? = null
private lateinit var personDisposable: Disposable
/******************* Life Cycle Methods *************************/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initOnCreate()
}
override fun onDestroy() {
super.onDestroy()
initOnDestroy()
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
initOnCreateOptionsMenu(menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean { return initOnOptionsItemSelected(item) }
/******************* Life Cycle Methods *************************/
/******************** Subscribers *******************************/
private fun setRxJavaRecievers() {
personDisposable = RxBus.listen(RxEvent.RxEventList::class.java).subscribe {
eventList = it.personName.getmData()
event_list.adapter!!.notifyDataSetChanged()
}
}
/******************** Subscribers *******************************/
/******************* Init Methods *******************************/
/** Init On Create **/
private fun initOnCreate() {
//Set up the UI
setContentView(R.layout.act_events)
//Inject Dagger Component
(application as CaringApp).netComponent.inject(this)
//Set up toolbar
setToolbar()
//Set up the recycler view
initRecyclerView()
//Set Rx java receivers
setRxJavaRecievers()
}
/** Init On Destroy **/
private fun initOnDestroy() {
//Un Register disposable
if (!personDisposable.isDisposed) personDisposable.dispose()
}
/** Initialize recyclerView **/
private fun initRecyclerView() {
val mLayoutManager = LinearLayoutManager(applicationContext)
event_list.layoutManager = mLayoutManager
event_list.itemAnimator = DefaultItemAnimator()
event_list.adapter = AdptEvents(eventList,this)
}
/******************* Init Methods *******************************/
}
AdptEvents.kt
class AdptEvents (val items: MutableList<TestModel>, val context: Context) : RecyclerView.Adapter<ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.row_event, parent, false))
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvAnimalType.text = items[position].getName()
}
}
class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each animal to
val tvAnimalType = view.txtTitle!!
}
答案 0 :(得分:0)
Recyclerview notifyDatasetChanged()
仅在您不更改列表的引用之前才起作用。但是在您的接收器中,您可能正在更改对列表的引用
private fun setRxJavaRecievers() {
personDisposable = RxBus.listen(RxEvent.RxEventList::class.java).subscribe {
eventList = it.personName.getmData()
event_list.adapter!!.notifyDataSetChanged()
}
}
所以代替
private fun setRxJavaRecievers() {
personDisposable = RxBus.listen(RxEvent.RxEventList::class.java).subscribe {
eventList.clear()
eventList.addAll(it.personName.getmData())
event_list.adapter!!.notifyDataSetChanged()
}
}
或再次设置适配器以使其正常工作