realm / realm-android-adapters

Adapters for combining Realm Java with Android UI components and framework classes
realm.io
Apache License 2.0
414 stars 134 forks source link

Method "onChanged()" of RecyclerView.AdapterDataObserver isn't being called #122

Closed marc97 closed 7 years ago

marc97 commented 7 years ago

I have a RecyclerViewRealmAdapter registering a RecyclerView.AdapterDataObserver, but onChanged() is never fired when an item is inserted/removed in realm. I've tested it using the default RecyclerView.Adapter and works perfectly.

final RecyclerView.Adapter<?> adapter = mRecyclerView.getAdapter();
RecyclerView.AdapterDataObserver emptyObserver = new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            // Not called
        }
};
adapter.registerAdapterDataObserver(emptyObserver); 

Versions used: 'io.realm:realm-gradle-plugin:3.1.4' 'io.realm:android-adapters:2.0.0'

zaki50 commented 7 years ago

I guess RecyclerView.AdapterDataObserver.onItemRangeInserted() or RecyclerView.AdapterDataObserver.onItemRangeRemoved() is called instead of RecyclerView.AdapterDataObserver.onChanged().

Could you check those methods as well?

Recent RecyclerViewRealmAdapter calles RecyclerView.Adapter.notifyItemRangeInserted() or RecyclerView.Adapter.notifyItemRangeRemoved() instead of RecyclerView.Adapter.notifyDataSetChanged() and notifyItemRangeInserted() and notifyItemRangeRemoved() does not trigger onChanged().

ni032mas commented 7 years ago

I have the same problem. I checked, instead of RecyclerView.AdapterDataObserver.onChanged(), called RecyclerView.AdapterDataObserver.onItemRangeInserted() or RecyclerView.AdapterDataObserver.onItemRangeRemoved().

zaki50 commented 7 years ago

@ni032mas That's the intended behavior. Why is that the problem?

marc97 commented 7 years ago

@zaki50 I've checked the methods RecyclerView.AdapterDataObserver.onItemRangeInserted( and RecyclerView.AdapterDataObserver.onItemRangeRemoved() and both works well. This issue can be closed.

ni032mas commented 7 years ago

I had a problem, now it's not there )

dalinaum commented 7 years ago

@marc97 It's good to hear that your problem is resolved, I will close this issue.

rubenwardy commented 7 years ago

That's the intended behavior. Why is that the problem?

Why is it intended behaviour? Surely the onChanged callback should be called whenever the data changes?

zaki50 commented 7 years ago

@rubenwardy To achieve correct animation on RecyclerView, more fine-grained callback than onChanged () should be called.

DenizBabat commented 1 year ago
class foo: Fragment{

private val documentRecyclerView get() = dataBinding.documentRecyclerView

private fun manipulateRecyclerViewScroll(){
        lifecycleScope.launchWhenResumed {
            documentRecyclerView.doOnPreDraw {
                documentRecyclerView.adapter!!.registerAdapterDataObserver(object :
                    RecyclerView.AdapterDataObserver() {
                    override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
                        super.onItemRangeChanged(positionStart, itemCount)
                        documentRecyclerView.scrollToPosition(0)
                    }
                })
            }
        }
    }
}