tags:

views:

91

answers:

1

I have built my interface by using ViewStubs, which I inflate during onCreate.

But later in my app, I want to change the View completely, by loading different View into the same place. How do I achieve that?

+3  A: 

Remove the old View via removeView(). Then inflate and add the replacement via addView().

Though if you're going to be bouncing back and forth a lot, consider using a FrameLayout or ViewFlipper or something to have both views loaded at once, only making one visible.

CommonsWare
How do I addView to a ListView. I love the way ListView works, having row selection of views, and onclick behavioral. I would like to reuse this, but my ListView would have different views for each row. Is that possible? Related to this: http://stackoverflow.com/questions/2482794/how-to-set-clickable-and-highlightable-a-view
Pentium10
Use my `MergeAdapter` or something like it. http://github.com/commonsguy/cwac-merge
CommonsWare
I've tried that, but no luck. The onlick and highlight doesn't work for custom views. For the items added via the items array this works. But when I add a View the click and highlight doesn't work. Do you have any new idea?
Pentium10
I had to tweak a little bit, to return true from isEnabled, as in your MergeAdapter method was having false. Is that how you intended to publish?
Pentium10
Further checking, I think your SackOfViewsAdapter isEnabled should return `return(views.get(position).isEnabled());` not `false`
Pentium10
I am not convinced that widgets being enabled and rows being enabled are inextricably tied. You are welcome to extend `SackOfViewsAdapter`, override `isEnabled()` to return what you want, and use that instead of calling `addView()` directly on the `MergeAdapter`.
CommonsWare
Back to the issue, how do I removeView() as I read that is unsupportedmethod on ListViews.
Pentium10
You would need to remove the row from your adapter.
CommonsWare
and using your MergeAdapter, how do I remove an item from it?
Pentium10
`MergeAdapter` doesn't presently support that. You would need to remove the View from whatever holds it (e.g., `SackOfViewsAdapter`), then add on-change notification support. When I suggested `MergeAdapter` a few comments ago, it was in response to "my ListView would have different views for each row". In all likelihood, the right answer is for you to create your own `Adapter` class, extending `BaseAdapter`, that does exactly what you want, exactly how you want it done.
CommonsWare