Android ViewFlipper usage example (Registration Guide page)

Source: Internet
Author: User

Android ViewFlipper usage example (Registration Guide page)
In fact, the working mechanism of ViewFlipper is very simple. For example, the sub-View added to ViewFlipper is displayed in sequence as one of the sub-views, and the other sub-views are set to the Gone status. The ViewFlipper page can be statically loaded, that is, simply write in xml. To add Java code, it means dynamic loading. For specific dynamic interfaces, see the ViewFlipper method.
1. The following is the relationship between the ViewFlipper class structure and the Inheritance and structure. For example, although the ViewFlipper class has very few methods, remember that it has a parent class method.
The method for dynamically loading the view is from the parent ViewGroup.

MVfFlipper. addView (child, index );

2. This class has the following animation-related functions:

SetInAnimation: Set the animation used when the View enters the screen. This function has two versions, one of which accepts a single parameter and the type is android. view. animation. animation; one accepts two parameters, whose types are Context and int, respectively, the Context object and the resourceID that defines the Animation.
SetOutAnimation: Specifies the animation used when the View exits the screen. The setInAnimation parameter function is the same.
ShowNext: Call this function to display the next View in FrameLayout.
ShowPrevious: Call this function to display the previous View in FrameLayout.

 

3. Differences between ViewFlipper and viewPager 1. viewPager is compatible with lower versions, while ViewFlipper is a new control introduced only in android4.0.
2. viewPager is one page, while ViewFlipper is one layer. Can all be animated.
3. ViewPager comes from google's complementary component android-support-v4.jar, which is located in the androidSDK folder.
Detailed Directory android-sdks \ extras \ android \ support \ reference android-support-v4.jar to the project note: this package needs to download Android Support package under Android SDK Manager.
4. ViewPager is more suitable for complex view switching, and Viewpager has its own adapter, which makes it adapt to complex objects and enables dynamic data loading. Used with Fragment.

 

 

4. Develop applications based on ViewFlipper's working mechanism. 1. Make a simple picture view.
2. Complete the registration for the set process class (such as user registration, help manual, and guide page ).
5. ViewPager source code
/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package android.widget;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.res.TypedArray;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.util.Log;import android.view.accessibility.AccessibilityEvent;import android.view.accessibility.AccessibilityNodeInfo;import android.widget.RemoteViews.RemoteView;/** * Simple {@link 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. * * @attr ref android.R.styleable#ViewFlipper_flipInterval * @attr ref android.R.styleable#ViewFlipper_autoStart */@RemoteViewpublic class ViewFlipper extends ViewAnimator {    private static final String TAG = "ViewFlipper";    private static final boolean LOGD = false;    private static final int DEFAULT_INTERVAL = 3000;    private int mFlipInterval = DEFAULT_INTERVAL;    private boolean mAutoStart = false;    private boolean mRunning = false;    private boolean mStarted = false;    private boolean mVisible = false;    private boolean mUserPresent = true;    public ViewFlipper(Context context) {        super(context);    }    public ViewFlipper(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray a = context.obtainStyledAttributes(attrs,                com.android.internal.R.styleable.ViewFlipper);        mFlipInterval = a.getInt(                com.android.internal.R.styleable.ViewFlipper_flipInterval, DEFAULT_INTERVAL);        mAutoStart = a.getBoolean(                com.android.internal.R.styleable.ViewFlipper_autoStart, false);        a.recycle();    }    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            final String action = intent.getAction();            if (Intent.ACTION_SCREEN_OFF.equals(action)) {                mUserPresent = false;                updateRunning();            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {                mUserPresent = true;                updateRunning(false);            }        }    };    @Override    protected void onAttachedToWindow() {        super.onAttachedToWindow();        // Listen for broadcasts related to user-presence        final IntentFilter filter = new IntentFilter();        filter.addAction(Intent.ACTION_SCREEN_OFF);        filter.addAction(Intent.ACTION_USER_PRESENT);        getContext().registerReceiver(mReceiver, filter);        if (mAutoStart) {            // Automatically start when requested            startFlipping();        }    }    @Override    protected void onDetachedFromWindow() {        super.onDetachedFromWindow();        mVisible = false;        getContext().unregisterReceiver(mReceiver);        updateRunning();    }    @Override    protected void onWindowVisibilityChanged(int visibility) {        super.onWindowVisibilityChanged(visibility);        mVisible = visibility == VISIBLE;        updateRunning(false);    }    /**     * How long to wait before flipping to the next view     *     * @param milliseconds     *            time in milliseconds     */    @android.view.RemotableViewMethod    public void setFlipInterval(int milliseconds) {        mFlipInterval = milliseconds;    }    /**     * Start a timer to cycle through child views     */    public void startFlipping() {        mStarted = true;        updateRunning();    }    /**     * No more flips     */    public void stopFlipping() {        mStarted = false;        updateRunning();    }    @Override    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {        super.onInitializeAccessibilityEvent(event);        event.setClassName(ViewFlipper.class.getName());    }    @Override    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {        super.onInitializeAccessibilityNodeInfo(info);        info.setClassName(ViewFlipper.class.getName());    }    /**     * Internal method to start or stop dispatching flip {@link Message} based     * on {@link #mRunning} and {@link #mVisible} state.     */    private void updateRunning() {        updateRunning(true);    }    /**     * Internal method to start or stop dispatching flip {@link Message} based     * on {@link #mRunning} and {@link #mVisible} state.     *     * @param flipNow Determines whether or not to execute the animation now, in     *            addition to queuing future flips. If omitted, defaults to     *            true.     */    private void updateRunning(boolean flipNow) {        boolean running = mVisible && mStarted && mUserPresent;        if (running != mRunning) {            if (running) {                showOnly(mWhichChild, flipNow);                Message msg = mHandler.obtainMessage(FLIP_MSG);                mHandler.sendMessageDelayed(msg, mFlipInterval);            } else {                mHandler.removeMessages(FLIP_MSG);            }            mRunning = running;        }        if (LOGD) {            Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted                    + ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning);        }    }    /**     * Returns true if the child views are flipping.     */    public boolean isFlipping() {        return mStarted;    }    /**     * Set if this view automatically calls {@link #startFlipping()} when it     * becomes attached to a window.     */    public void setAutoStart(boolean autoStart) {        mAutoStart = autoStart;    }    /**     * Returns true if this view automatically calls {@link #startFlipping()}     * when it becomes attached to a window.     */    public boolean isAutoStart() {        return mAutoStart;    }    private final int FLIP_MSG = 1;    private final Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            if (msg.what == FLIP_MSG) {                if (mRunning) {                    showNext();                    msg = obtainMessage(FLIP_MSG);                    sendMessageDelayed(msg, mFlipInterval);                }            }        }    };}
6. demo Below is an example of this demo program. It is based on the release of open source, imitating the code in Momo and guiding the registration process. A simplified demo. It is easy to learn and also for your reference.

: Click

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.