Code Monkey home page Code Monkey logo

Comments (6)

pakerfeldt avatar pakerfeldt commented on May 29, 2024

That might make sense. But consider this scenario.

You have 10 elements in the adapter and side buffer is set to 1 and you're currently focusing on element 5. Hence, ViewFlow has loaded view 4, 5, 6. Then, let's assume setSelection(1) is called. That would result in the currently loaded views: 0, 1, 2, hence, the previously shown View 5 is not loaded any more.

Question: What would be passed as previousView in the new onSwitched method?
I am very tempted to answer null on that since the ViewFlow has removed the view from the buffer and should hold no reference to it. Passing a view that is expected to be freed from memory doesn't sound too good.

from android-viewflow.

tadfisher avatar tadfisher commented on May 29, 2024

I agree with this, and passing null will also prevent memory leaks caused by references on the application side. Some might view null-valued parameters as poor API design, however. Google does it anyway, such as in adapter-view binding.

from android-viewflow.

pakerfeldt avatar pakerfeldt commented on May 29, 2024

I have the fix almost done but need to close down for the evening. Hopefully I will push it tomorrow.

from android-viewflow.

tadfisher avatar tadfisher commented on May 29, 2024

Thanks. I was just thinking that a second callback might be more appropriate, since taking these references might influence GC unnecessarily... for example, if you take a reference to the view prior to removing it from the buffer. But checking for this case would also prevent that.

from android-viewflow.

pakerfeldt avatar pakerfeldt commented on May 29, 2024

First of all, a previous view will never be passed if it was just removed.

But we still might have the case that a view is removed later on, if the user is storing a reference to the view or doing some asynchronous task. But this issue is applicable for the current view as well. Separating the interface into two callbacks won't solve that problem I think. We need to remember that ViewFlow is re-using old Views as well when appropriate. Therefore, modifying a View from a ViewSwitchListener asynchronously might have strange effects if ViewFlow is trying to re-use the View before the asynchronous task is complete.

If you have control over both the ViewSwitchListener and the Adapter you can avoid these situations by checking which View is being re-used when the getView(...) method is called in the Adapter. Better yet, if you can modify the View synchronously, this will never occur.

I should probably clarify things in the Javadoc for the ViewSwitchListener.

from android-viewflow.

pakerfeldt avatar pakerfeldt commented on May 29, 2024

Implementing this in postViewSwitched (which is called when the user swipes) is very straight-forward. But I just figured out, doing this in setSelection requires bigger changes. In setSelection I'm taking a shortcut and remove all views from the buffer, and then request to get a new view from the adapter for each element. Of course re-using the actual view objects, but still, if I would simply save the previous view in the beginning of setSelection, that view would no longer correspond to the previous view as it has been re-used by another (arbitrary) element.

I certainly need to re-do setSelection anyway because it is very sub-optimal. Especially if someone uses setSelection to just go back and forth one view at a time, as setSelection will ask the adapter for all views and not really make use of the buffer.

from android-viewflow.

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.