What's the difference between ViewFlipper and ViewSwitcher - android

They both inherit ViewAnimator. I know that ViewSwitcher allows only two views, while ViewFlipper allows more. But why did Android create ViewSwitcher, if it is just a ViewFlipper with 2 views? Are there any other differences? On what condition will be using the one superior than the other?

From what I can tell, ViewSwitcher is used if you want to switch between two views like you said. Useful if you have 2 views that you have to go back and forth fairly regularly. And the class implements a ViewFactory if you wish to use it.
However, ViewFlipper can be used if you want to periodically change the views. Say like an automated flipping book of some sort. Though a custom-adapter gallery is much better at this.
And yep, that's all. They really aren't that much more useful than the ViewAnimator. Why Android development makes anything is really a mystery to me.
I would normally just prefer using a ViewAnimator because it gives you more freedom and thus flexibility in how you design. But if you want features like the ViewFactory and you only got 2 views use the ViewSwitcher. If you want to be able to periodically change views use ViewFlipper. If you don't need either use a ViewAnimator.

ViewFlipper supports more than two views, ViewSwitcher only supports 2.

I'm not sure if there's any big differences between them but from what I've gathered the difference is that ViewSwitcher is used in circumstances where the view is the same but the data is different - like on a calendar app - we're just changing the data in the view.

ViewFlipper is allowed in app widgets, while ViewSwitcher is not.
https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

Related

Android: dialog vs nested layout performance

My app is quite complex, i have a lot of views and viewgroups.
I have already turned some of the nested linearlayouts into relativelayouts, but the tree is still quite deep.
In the views tree there are branches that i need to display only sometimes, i would like to know what performs better: a sub-tree to show and hide or a dialog?
Here's a rough image i've done to try to explain:
Image clearly shows: 4 views is better than 7, but what if i hide (gone) viewgroup #5?
How heavy is opening/managing a "complex" dialog?
What are the pros and cons?
Thanx.
Using nested layouts is not recommended because of performance issues.
Well some layouts can only be made by doing some level of nesting. But you should avoid having too many nested LinearLayouts and even more important NEVER nest LinearLayouts with weights. You can read a little more about optimizing layouts in the official docs.
Personally I use LinearLayouts for simple stuff and start using RelativeLayout when the layout gets more complex. So, my suggestion would be to use the dialog. It will also help in making the code easier to read.

View Flipping in Android using same activity

I am new to android therefore I got a little bit lost with all those ViewFlipper, ViewSwitcher, ViewAnimator and ViewPager. Moreover, I am not sure what's happening under the hood.
Basically I have an activity which shows some data. With swipe (or button, doesnt matter) I would like to scroll the view and get to another page (as seen in the picture below).
Is it possible to implement something like that without changing to another activity?
I am a little bit confused regarding views and access to the design elements. How those pages are located each to another? e.g. If I am currently seeing Page1, can I modify content of Page3? Or plainly saying, are all page views loaded all together? As if I set setContentView(R.layout.xlayout); then I can access only xlayout elements.
But if I use same activity, then I have a thread there which updates a counter on Page1, if I change view to Page 2, the counter will not find Page1 Counter TextView and will complain.
As I Understand Android 4.0 has ViewPager which is similar to the seen in the picture. I am using GB. Should I use support library or can I just go around and implement something similar without importing any libraries?
(Sorry, my description is a little bit messy)
Yes, you can use ViewSwitcher, ViewFlipper and ImageSwitcher depending on your requirements.
ViewSwitcher may have two childs at max. And these child might be a View or an object of subclass of view.
ViewFlipper: May have as many childs you want. and these child might be a View or an object of subclass of view.
ImageSwitcher might be used to switch images over.
By using view flipper you can display one item at a time, and adding a gesture overlay, you can apply sliding effect. To apply View Flipper, you need to add all the views to ViewFlipper, and showNext and showPrevious methods are used to show next and previous child.
You need to use a ViewPager to have the same behaviour as in Google Play.
it only available on recent version of Android, but you can use the Compatibility Package to make it available for older version.
With a ViewFlipper you can't "scroll" between two pages.

GridLayout and dynamic views

I want to use a GridLayout to reproduce a layout similar to what we currently se on the Android Market (I mean Play Store !).
Now I managed to do something relatively similar :
This looks nice, but I do not think this would scale well with many Views if I used bitmaps instead of colors for the backgrounds of the Views. As I understand GridLayout cannot use an adapter, so I would have to manage myself the management of Views when they are shown/hidden.
Any suggestions ? I don't have to use a gridlayout if this layout is possible with another view that I can plug to an adapter.
No there is no magic solution that will auto-populate your views. This is a pretty custom case. Your best bet will be to set a few timers that will periodically load/update the content of each of the sections. You will have to create/destroy/animate all of the views yourself. But I like the idea and glad to see that you are trying to make attractive dynamic content.

Is it possible to reuse the views in ViewFlipper?

May be a dumb Question.But still, is it possible to reuse the views in viewflipper?
Now,i have three imageviews in a viewflipper.is it possible to have a single imageview and change the source to it?
You can probably reuse view if you want to take care of the bookkeeping your self. However the viewflipper requires at least 2 views. From the Android ViewFlipper Docs:
Simple ViewAnimator that will animate between two or more views
that have been added to it. Only one child is shown at a time. If
requested, can automatically flip between each child at a regular
interval.
You would have to remove the ImageView from the ViewFlipper and then put it somewhere else. You can't put it into two ViewGroups at the same time (you'll get an exception that the view has already a parent).
But this is an overhead which you simply don't need to do. Simply create new ImageViews and use them. The memory consuming part of an ImageView is not the object itself but the bitmap it draws so I really recommend to read this article.

what does ImageSwitcher actually DO?

I'm trying to snaz up my android apps and I see that ImageSwitcher is being referenced a lot for all sorts of animation tasks, but the google docs are totally spartan and don't describe anything other than the methods that are in the class. Meanwhile the examples all make use of gallery, and don't explain why.
Does anyone have a link to (or care to explain) any info on what the class actually does and how it's meant to be used?
I can't give a definitive answer as I've never used it. My best guess comes from working down the inheritance chain...
ViewAnimator...
Base class for a FrameLayout container that will perform animations when switching between its views.
ViewSwitcher
ViewAnimator that switches between two views, and has a factory from which these views are created. You can either use the factory to create the views, or add them yourself. A ViewSwitcher can only have two child views, of which only one is shown at a time.
Then looking at another direct subclass of ViewSwitcher...
TextSwitcher
Specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.
So reading between the lines, an ImageSwitcher is a ViewAnimator which is optimised for images (i.e., drawables) and as it inherits directly from ViewSwitcher it can only have two images.
So, paraphrasing the TextSwitcher overview, I would say that...
Whenever <insert setImageXXX method here> is called, ImageSwitcher animates the current image out and animates the new image in.
As I said, it's just a 'best guess'.

Categories

Resources