question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatic scrolling when new data set

See original GitHub issue

I have a weird problem with autoscrolled position if I set data to my controller. I have a screen with pagination, i have ProgressModel that is at the bottom of the list and I show/hide it according to loading state. It looks that when I display progress and then show data (not hiding progress), recyclerview is scrolled so the progress is still visible.

My controller is really simple

class ProductsEpoxyController : EpoxyController() {
    var products: List<ApiProduct> by adapterProperty(this, listOf())
    var showError by adapterProperty(this, false)
    var showProgress by adapterProperty(this, true)

    override fun buildModels() {
        products.forEach {
            ProductModel_()
                    .id(it.id)
                    .product(it)
                    .addTo(this)
        }
        ProgressModel()
                .id(-100)
                .addIf(showProgress, this)
    }
}

adapterProperty is just a shortcut for rebuilding models when property changes.

The flow of commands is

showProgress = true … (few hundred millis) products = listOf(…)

The recyclerview is scrolled at the bototm and progress is visible.

Im not really sure if this problem is related to this library, but I have a feeling that I had this issue with first version of Epoxy and I solved that by replacing Epoxy with own solution.

I can provide any more info if needed. Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

73reactions
elihartcommented, Jun 3, 2017

@davidbilik I can reproduce it in the Epoxy sample, the key is that the items have to be inserted at position 0. If that happens RecyclerView tries to keep the previous items in frame since it assumes that is what the user was looking at. Generally in our UI’s we have a header at position 0 so we don’t have the scrolling problem.

I think this behavior is often good for the user, but definitely not what we want in the case of a loader. I’m not sure the best way to disable this behavior for your use case. The easiest thing to do is have a header item at position 0. Another option is to temporarily remove the loader when you insert the new items. A third option is to register an adapter change listener and force a scroll to the top

controller.getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
    @Override
    public void onItemRangeInserted(int positionStart, int itemCount) {
        if(positionStart == 0) {
            layoutManager.scrollToPosition(0);
       }
    }
});
29reactions
elihartcommented, Jun 2, 2017

Hm, we do something very similar in many places in our app and haven’t had this problem. This is probably just RecyclerView being weird, Epoxy never tells the recyclerview to scroll, it only dispatches item change notifications (in this case that items were inserted).

One reason I have seen RecyclerView auto scroll is if a view is focusable. In that case it scrolls to keep it in position. If that is the case you could make your loader not focusable, or set android:descendantFocusability="beforeDescendants" on your RecyclerView.

As another sanity check I would recommend turning on logging (https://github.com/airbnb/epoxy/wiki/Epoxy-Controller#debug-logs) and seeing what change notifications your controller is doing.

I believe this might also happen if you have your LayoutManager set to stack from bottom, or some other layout manager setting to keep the bottom in focus.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to auto-scroll to end of div when data is added? [duplicate]
This displays a vertical scroll bar once enough data is added to the table. However, when new data is added I would like...
Read more >
Auto-Scroll in Javascript - Medium
I've found out that when a new user joins the chat and try to read the previous messages was automatically scrolled to the...
Read more >
Kaggle Kernel Result Autoscroll | Data Science and Machine ...
Currently, while the program is processing the data, the box in which a cell's result is shown does not auto scroll to the...
Read more >
C# scrolls data automatically - MSDN - Microsoft
Please scroll to the middle, to: "Latest results for Schedule" you can see the data gets scrolled by itself or it can be...
Read more >
Automatic scrolling, only if a user already scrolled the bottom ...
Let's add a new isNearBottom variable, which we can set, whenever the user scrolled within the scrollContainer element. For this, we need to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found