Giter Club home page Giter Club logo

Comments (7)

idanatz avatar idanatz commented on May 30, 2024

Hi,
I'm not sure I understand the issue.
Please specify each issue steps in details on how to recreate, expected outcome, and the actual outcome.
Attach code snippets or gifs that represent those issues.

In general, the item animations are controlled by the recycler view and the diff utils mechanisms, not the adapter.
If an item is not visible to the user the animation will not run, else it should. (again based on the diffing mechanism, not the adapter)

from oneadapter.

vellrya avatar vellrya commented on May 30, 2024

Probably video should help to understand what I wrote about)
https://dropmefiles.com/G3uhp

New element, which add to top of current elements in adapter, doesn't appear in top automatically. It's required from user to scroll up to see newly added element - it isn't user friendly, I think.

from oneadapter.

idanatz avatar idanatz commented on May 30, 2024

Thanks for the video.
It's a known issue with RecyclerView and LayoutManager, just google "recyclerview notifyiteminserted not animating" and you will find dozens of questions regarding the same issue.

This is happening because LayoutManager thinks the item is inserted above the first item. This behavior makes sense when you are not at the top of the list but I agree that it is unintuitive when you are at the top of the list.

It's not the adapter's responsibility to change this behavior.

A possible solution can be found here

from oneadapter.

vellrya avatar vellrya commented on May 30, 2024

Thank you for explanation, I already found this SO answer, but unable to get acceptable result (works in 1 case of 10, probably).
If I call restoreScrollPositionAfterAdAdded right after setItems, diffUtil can't calculate new list properly at this time(?) and in method I get:

firstVisibleItemPosition: 0

but layoutManager.scrollToPosition(0) does nothing in 90% of trying and manual scrolling needed. In 10% of case recyclerView correctly scroll to new element, but this is sad statistics. Probably postDelayed handler can help, but this is a reinventing the wheel...
Another idea: some callback from oneAdapter should help call restoreScrollPosition at a right time, what do you think?
Current code:

        dialogViewModel.allDialogs.observe(this) { dialogs ->
            oneAdapter.setItems(dialogs)
            restoreScrollPositionAfterAdAdded()
        }

from oneadapter.

idanatz avatar idanatz commented on May 30, 2024

I understand your issue, its indeed due to the async nature of diffUtil.
You can attach a hook to the RecyclerView to be notified when the data actually changes after the diffing is done.

I can expose a method on the adapter that will let you register a listener to be notified when the data is changing.
The method is registerAdapterDataObserver with a AdapterDataObserver instance passed as a parameter.
With it you can listen to onItemRangeInserted and if the positionStart is 0 scroll to the position.

Like this:

	oneAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
		override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
                        int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()

			if (firstVisibleItemPosition == 0 && positionStart == 0) {
				layoutManager.scrollToPosition(0)
			}
		}
	})

I've tested and it worked 100% of the time.

from oneadapter.

vellrya avatar vellrya commented on May 30, 2024

Great solution!

Right now it can be fixed with this code:

binding.dialogsRV.adapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
            override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
                val layoutManager = (binding.dialogsRV.layoutManager as LinearLayoutManager?) ?: return
                val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()

                if (firstVisibleItemPosition == 0 && positionStart == 0) {
                    layoutManager.scrollToPosition(0)
                }
            }
        })

Works as expected, thank you!
I will switch to registerAdapterDataObserver method of the oneAdapter directly, when it will available)

Upd. I made some modification to handle update, when element moves to first position.
Useful for those who have the same question.

        val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        binding.dialogsRV.layoutManager = layoutManager
        binding.dialogsRV.adapter?.registerAdapterDataObserver(object :
            RecyclerView.AdapterDataObserver() {
            override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
                val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
                if (firstVisibleItemPosition == 0 && positionStart == 0) {
                    layoutManager.scrollToPosition(0)
                }
            }

            override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
                val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
                if (firstVisibleItemPosition == 0 && toPosition == 0) {
                    layoutManager.scrollToPosition(0)
                }
                super.onItemRangeMoved(fromPosition, toPosition, itemCount)
            }
        })

from oneadapter.

idanatz avatar idanatz commented on May 30, 2024

v2.0.2 is out with your request

from oneadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.