Codenameone refresh list of images without reloadi

2020-07-24 03:57发布

问题:

I am Working on an app with requirement where we need to create a dynamic image gallery which refreshes after few minutes.When refresh happens three things should happen without reloading the page

1) Obsolete images should be removed 2) New Images should be added 3) Non Obsolete images should stay (not reload) 4) Images should be stacked next to each other as it will be mostly used on tablet

I was looking at Boxlayout or FlowLayout and I can add the image but I am not sure how to delete it dynamically .I was able to set UUID for the image component but was not able to get component based on UUID to remove it . How can I get component based on its UUID added to the form ? Is this the correct approach ? or there is already build in component that does that .

I saw this
How to add dynamic data in the renderer created using UI builder?

But I also read using List is discouraged 

https://www.codenameone.com/javadoc/com/codename1/ui/list/package-summary.html

回答1:

This is actually pretty simple to do with a BoxLayout.Y_AXIS Container. When you create an image component to add to the box layout do this:

myImageComponent.putClientProperty("imageId", imageId);

Then when you have the callback to refresh the list just do something like this:

for(Component cmp : parentContainer) {
    String id = (String)myImageComponent.getClientProperty("imageId");
    if(!existsInNewList(id)) {
       cmp.remove();
    }
}

When you are done updating the container just call animateLayout(200) or revalidate() to refresh the UI.



标签: codenameone