ViewFlipper vs Fragments
Asked Answered
R

2

10

I have an Activity with a ViewFlipper that flips between a bunch of views (pages) with my data.

I am considering of using the fragments API to switch between my views. What are the benefits of doing so?

Could I see a performance gain by using fragments since the ViewFlipper essentially toggles the visibility flags and fragments actually replace the view hierarchy as you add/remove them?

Can someone give us more insight on this?

Thanks!

Retouch answered 25/1, 2012 at 19:33 Comment(1)
If you want a performance analysis you should do it yourself. Stack is generally not good at answering these kinds of question.Sneeze
E
3

EDIT: I'm talking about ViewPager here, not ViewFlipper.

The benefit of using a ViewPager is that it's very simple to use. You simply create a FragmentPagerAdapter and fill out a couple simple fields. Below is an example that I wrote to display items that are passed from the parent class.

public static class DashboardPagerAdapter extends FragmentPagerAdapter {

    private static final int NUM_ITEMS_PER_SCREEN = 12;

    private List<View> mAllItems;
    private int mNumScreens;

    public DashboardPagerAdapter(FragmentManager fm, List<View> allItems) {
        super(fm);
        mAllItems = allItems;
        mNumScreens = (int) Math.ceil(mAllItems.size()
                / (float) NUM_ITEMS_PER_SCREEN);
        Log.d(TAG, "num screens: " + mNumScreens);
    }

    @Override
    public int getCount() {
        return mNumScreens;
    }

    @Override
    public Fragment getItem(int position) {
        DashboardScreenFragment screen = DashboardScreenFragment
                .newInstance();

        for (int i = position * NUM_ITEMS_PER_SCREEN; i < (position + 1)
                * NUM_ITEMS_PER_SCREEN; i++) {
            if (i >= mAllItems.size()) {
                break;
            }
            screen.addChild(mAllItems.get(i));
        }
        return screen;
    }
}

And setting it up is super simple:

    final DashboardPagerAdapter adapter = new DashboardPagerAdapter(
            getFragmentManager(), mAllButtons);

Contrary to what @sebap123 said, you can use Fragments with Android v4 and above with the Support Library; see http://developer.android.com/sdk/compatibility-library.html for details, but it really just involves putting the jar file in your /libs directory.

BTW, note that with ViewPager, each "page" is itself a Fragment.

As for whether there is a performance improvement, that depends on what you're comparing it to. I highly recommend trying out ViewPager and only after worrying about performance problems. As Donald Knuth said, "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

In any case, if you have a large number of pages, see http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html for a more appropriate adapter.

Eclosion answered 30/1, 2012 at 4:10 Comment(3)
I am talking about using a ViewFlipper to flip between fragments. I am referring to the comparison between using FragmentManager to add/remove fragments versus using a ViewFlipper that just flips through Views (not fragments again!)Retouch
The difference is in the visuals. If you want the user to be able to swipe horizontally between screens, you should use ViewFlipper. If you simply want to add and remove fragments (e.g. with buttons), you should use a FragmentManager.Eclosion
Wow, you know what I just realized? I've been talking about ViewPager this whole time, not ViewFlipper. ViewFlipper is completely different, I'm sorry. Rather than delete the whole thing, I'm going to edit and replace "ViewFlipper" with ViewPager, even though it doesn't directly answer the question.Eclosion
D
1

A Fragment is a behavior or part of a user interface in an Activity.
You can combine several fragments in one action to create a multi-level user interface and reuse a fragment in several actions. You can imagine the fragment as a modular activity section, which has its own life cycle, receives its own input events and which you can add or remove during work (sort of like auxiliary activity, which you can reuse in different actions).

A fragment must always be embedded in the action, and the fragment life cycle directly depends on the host life cycle. For example, when an action is paused, that is, all fragments in it, and when the action is destroyed, as well as all fragments. However, while the action is being performed (it is in the state of a resumed life cycle), you can independently control each fragment, for example, add or delete them. When you perform such a fragment transaction, you can also add it to the back stack, which is controlled by activity - each entry in the previous stack in the activity is a record of the fragment transaction that occurred. The back stack allows the user to cancel the fragment transaction (move backward) by pressing the BACK button.

But ViewFlipper works in one action. So it's just a ViewGroup. A simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is displayed at a time. If required, you can automatically switch between each child at regular intervals.

Defoliant answered 22/11, 2021 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.