Viewswitcher usage examples of Android development _android

Source: Internet
Author: User
Tags prev

This example describes the Viewswitcher usage of Android development. Share to everyone for your reference, specific as follows:

Android.widget.ViewSwitcher is a viewanimator subclass that toggles between two view, but only one view at a time.

The code for the Viewswitcher AddView function is as follows:

/**
 * {@inheritDoc}
 * *
 @throws illegalstateexception If this switcher already contains
 two children */< c5/> @Override public
void AddView (View child, int index, viewgroup.layoutparams params) {
  if (Getchildcount () & Gt;= 2) {
    throw new IllegalStateException ("Can ' t add more than 2 views to a Viewswitcher");
  }
  Super.addview (Child, index, params);


It can be seen that if the number of view more than two, will throw an exception: java.lang.IllegalStateException, printing "can" t add more than 2 views to a Viewswitcher ". You can use Viewswitcher's factory to create a view or add a view that you create.

Here is an example of how to use Viewswitcher.

Layout file: Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:l ayout_height= "match_parent" android:orientation= "vertical" tools:context= ". 
      Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:orientation= "Horizontal" > <button android:id= "@+id/prev" android:layout_width= "0
    DP "android:layout_height=" wrap_content "android:layout_weight=" 1 "android:text=" previous "/> <button android:id= "@+id/next" android:layout_width= "0DP" android:layout_height= "Wrap_conten" T "android:layout_weight=" 1 "android:text=" Next "/> </LinearLayout> <viewswitcher an Droid:id= "@+id/viewswitcher" android:layout_width= "Match_parEnt "android:layout_height=" wrap_content "> <imageview android:layout_width=" wrap_content " android:layout_height= "Wrap_content" android:src= "@drawable/ic_launcher"/> <linearlayout Andro Id:layout_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "center" Andro id:orientation= "vertical" > <button android:layout_width= "wrap_content" Android:layout_hei ght= "Wrap_content" android:text= "-button 2-"/> <textview android:layout_width= "Wrap_con Tent "android:layout_height=" Wrap_content "android:text=" LinearLayout 2 "/> &LT;/LINEARLAYOUT&G
  T

 </ViewSwitcher> </LinearLayout>

The code for the activity:

Package com.example.AndroidTest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationUtils;
Import Android.widget.Button;
Import Android.widget.ViewSwitcher;
  public class MyActivity extends activity {Button Buttonprev, buttonnext;
  Viewswitcher Viewswitcher;
  Animation Slide_in_left, Slide_out_right;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Buttonprev = (Button) Findviewbyid (R.id.prev);
    Buttonnext = (Button) Findviewbyid (R.id.next);
    Viewswitcher = (viewswitcher) Findviewbyid (R.id.viewswitcher); Slide_in_left = Animationutils.loadanimation (This, Android.
    R.anim.slide_in_left); Slide_out_right = Animationutils.loadanimation (This, Android.
    R.anim.slide_out_right);
    Viewswitcher.setinanimation (Slide_in_left); Viewswitcher.setoutAnimation (Slide_out_right);
        Buttonprev.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View arg0) {
      Viewswitcher.showprevious ();
    }
    });
        Buttonnext.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View arg0) {
      Viewswitcher.shownext ();
    }
    });
  ;

 }
}

To implement the effect diagram:

Using Viewswitcher's setfactory setting toggles the view into two steps.

Step one: Get an example of Viewswithcer

Switcher = (viewswitcher) Findviewbyid (R.id.viewswitcher);

Part II: Implementing Interface Viewfactory

Switcher.setfactory (New Viewfactory ()
{
  @Override public
  View Makeview ()
  {
    return Inflater.inflate (R.layout.slidelistview, null);
  }
);

More interested readers of Android-related content can view this site's topics: "Introduction to Android Development and advanced Tutorials", "Summary of Android Control usage", "Android SMS and phone operation Tips" and "Android Multimedia operating Tips Summary" (audio, video, Recording, etc.) "

I hope this article will help you with the Android program.

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.