0

I'm developing a Treeview layout in Android and I have a problem with the refresh. This is what I see

scrrenshot

Now I receive a message to add a resource to this treeview. What I'd like is to have the device to be added while I'm watching the treeview, without it scrolling to the beginning, be recreated or stuff like that. Only the minimum scroll to make room to the new component.

My treeview is built in this way:

Every element is a LinearLayout with some properties, visible (like the name) and invisible (like the ip, the father and so on)
I find the father of all the devices (rootFather)
Recursively I examine every device and add it to the proper father (which is a LinearLayout)
I embed the rootFather in a ScrollView
I embed the ScrollView in a LinearLayout
I display the layout

Now when a new device "arrives", I parse it and build it then I add it to the proper father and this whole process happens in the Activity that shows the Treeview.

The problem is that nothing happens, no refresh, anything. If I press the back button and reopen the Activity with the Treeview the object is correctly displayed.

What is the correct approach to solve this problem? Should I change approach? Should I call some magic method after the child has been added?

Thanks

Luigi Tiburzi
  • 4,265
  • 7
  • 32
  • 43

1 Answers1

1

I have implemented TreeViews look-a-likes using ExpandableListViews. They're pretty easy to use and any invalidation required can be triggered very easy with calls to #notifyDataSetInvalidated();

Here is a link to get you started with ExpandableListViews. The advantage of using ListViews and ExpandableListViews is that they take care of the algorithm you tried to implement above.

Hope it helps and it is not too late for a re-factoring! :)

gunar
  • 14,660
  • 7
  • 56
  • 87
  • The problem is that ExpandableListView supports only a 2 levels in-depth and I need something which is unlimited. So from here the idea of building my own treeview with, of course, all the problems connected... BTW your answer if still useful because gives at least an alternative – Luigi Tiburzi May 30 '13 at 09:26
  • 1
    You could cascade the ExpandableListView view implementation so that each child which is not a leaf could be an ExpandableListView. – gunar May 30 '13 at 09:56
  • MM yes, that's interesting though I fear the time for a refactoring would be not small and the chance of having it broken enormous! – Luigi Tiburzi May 30 '13 at 10:03
  • Why don't you try to allocate 2-3 hours then to try this approach instead of continuing with current approach? I believe you will gain more on the long run ;) – gunar May 30 '13 at 10:11
  • Finally I did it!! I added the new Resource to the appropriate father, called father.childDrawableStateChanged(newResource); and then newResource.setVisibility(View.VISIBLE); and works perfectly. Thanks for the suggestion though, the next time I'll surely use ExpandableListView!! – Luigi Tiburzi May 31 '13 at 09:14