Understand the working mechanism of ViewFlipper and the viewflipper Mechanism

Source: Internet
Author: User

Understand the working mechanism of ViewFlipper and the viewflipper Mechanism

When we use ViewFlipper, we lament that ViewFlipper is easy to use. On the other hand, we often lament that ViewFlipper provides too few interface methods and that many common effects are not good, it is difficult to use your skills. This article will be detailed.
I will introduce the architecture of ViewFlipper and its working principles in detail. I believe that after reading this article, you will find that ViewFlipper can display a lot of results, you can also customize your own ViewFlipper as needed.
Class inheritance relationship diagram:

The relationship graph of the class shows that ViewFlipper is a subclass of ViewAnimator, while ViewAnimator is a subclass of FrameLayout. Do you have any conjecture about the working principle of ViewFlipper when you see this inheritance relationship?

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 following describes the process in detail through the source code.

Main Methods in ViewFlipper:

@android.view.RemotableViewMethodpublic 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();}

 

private void updateRunning(boolean flipNow) {    boolean running = mVisible && mStarted && mUserPresent;    if (running != mRunning) {        if (running) {            showOnly(mWhichChild, flipNow);            postDelayed(mFlipRunnable, mFlipInterval);        } else {            removeCallbacks(mFlipRunnable);        }        mRunning = running;    }    if (LOGD) {        Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted                + ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning);    }}

Looking at these three methods, we can see that, whether startFlipper or stopFlipper methods, they call the updateRunning method. The difference is that they set different values for the mStart variable, the mStart value determines whether ViewFlipper is start or stop.

The updateRunning method determines whether to send a message to the Message stack or remove the message based on the running value.

private final Runnable mFlipRunnable = new Runnable() {    @Override    public void run() {        if (mRunning) {            showNext();            postDelayed(mFlipRunnable, mFlipInterval);        }    }};

If it is startFlipper, that is, set the status of the next View to Visible, and other views to Gone. The method for executing this job is showNext (). The implementation of this method is in ViewAnimator. In fact, ViewFlipper does such a thing. Next let's take a look at ViewAnimator.

The main methods in ViewAnimator are as follows:

public void showNext() {         setDisplayedChild(mWhichChild + 1);     }  

 

public void setDisplayedChild(int whichChild) {         mWhichChild = whichChild;         if (whichChild >= getChildCount()) {             mWhichChild = 0;         } else if (whichChild < 0) {             mWhichChild = getChildCount() - 1;         }         boolean hasFocus = getFocusedChild() != null;         // This will clear old focus if we had it         showOnly(mWhichChild);         if (hasFocus) {             // Try to retake focus if we had it             requestFocus(FOCUS_FORWARD);         }     }  

The two methods are used to determine which View to display next. After the View to be displayed is determined, the showOnly (mWhichChild) method is called.

void showOnly(int childIndex) {          final int count = getChildCount();          for (int i = 0; i < count; i++) {              final View child = getChildAt(i);              final boolean checkForFirst = (!mFirstTime || mAnimateFirstTime);              if (i == childIndex) {                  if (checkForFirst && mInAnimation != null) {                      child.startAnimation(mInAnimation);                  }                  child.setVisibility(View.VISIBLE);                  mFirstTime = false;              } else {                  if (checkForFirst && mOutAnimation != null && child.getVisibility() == View.VISIBLE) {                      child.startAnimation(mOutAnimation);                  } else if (child.getAnimation() == mInAnimation)                      child.clearAnimation();                  child.setVisibility(View.GONE);              }          }      }  

The main task of this method is to display mWhichChild and set others to the Gone status. At the same time, this method also calls the switching animation, if there is an animation.

In fact, in the ViewAnimator class, we mainly do two things. One is to set the subview to Visible as required, and the other subviews to Gone. The other is the processing of animations.

Have you found that ViewFlipper is so simple?

 

Related Article

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.