ViewPager
The ViewPager class provides new effects of multi-interface switching. The new effect has the following features:
[1] One of the interfaces in A group is currently displayed.
[2] when a user slides between the left and right, the current screen displays the current interface and a part of the next interface.
[3] After sliding, the page automatically jumps to the selected page
ViewPager comes from google's complementary component android-support-v4.jar, which is located in the androidSDK folder
Android-sdks \ extras \ android \ support \
Reference android-support-v4.jar to a project
Note: The Android Support package under Extras needs to be downloaded from the Android SDK Manager,
Or directly download the jar package to reference it in the project.
After being introduced, it can be directly used as a control in the project.
Configuration File page file
<Android. support. v4.view. ViewPager
Android: id = "@ + id/viewPager1"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_centerVertical = "true"/>
Initialize controls
ViewPager1 = (ViewPager) findViewById (R. id. viewPager1 );
ViewPager1.setAdapter (new PageAdapter (new ArrayList <View> ()));
Configure page change events for the adapter
ViewPager1
. SetOnPageChangeListener (new OnPageChangeListener (){
// Select on the page
@ Override
Public void onPageSelected (int position ){
TopText. setText (String. valueOf (position + 1) + "/" + String. valueOf (lists. length ));
}
@ Override
Public void onPageScrollStateChanged (int state ){
}
@ Override
Public void onPageScrolled (int position,
Float positionOffset, int positionOffsetPixels ){
}
});
The adapter used by ViewPager is based on the PagerAdapter base class.
The following four methods are implemented:
// Obtain the number of interfaces of the current form
Public int getCount ()
// Initialize the position page
Public Object instantiateItem (View collection, int position)
// Destroy the position page
Public void destroyItem (View collection, int position, Object view)
// Determine whether the interface is generated by the object
Public boolean isViewFromObject (View arg0, Object arg1)
When using the ViewPager control, you can set the View as the data carrier in the ArrayList. Each item (that is, each page)
Display as a View, which can adapt to the display of a large number of pages or varying page lengths
ViewFilpper
The Viewfilpper control is mainly used to switch between pages in the same Activity. The longest case is that there are multiple pages in a FrameLayout, such as a system setting page and a personalized setting page.
The ViewFilpper control is one of the built-in controls in the system. It mainly sets the animation effect for the switching between two pages. ViewFilpper inherits ViewAnimator from FrameLayout,
The android. widget. ViewAnimator class inherits from FrameLayout. The ViewAnimator class is used to provide animation effects for View switching in FrameLayout. 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.
Generally, ViewAnimator is not directly used. Instead, ViewFlipper and ViewSwitcher are used. ViewFlipper can be used to specify the switching effect between multiple views in FrameLayout. It can be specified at one time or a separate effect can be specified during each switching. This class provides the following functions:
IsFlipping: used to determine whether View switching is in progress
SetFilpInterval: set the time interval for switching between views.
StartFlipping: Use the interval set above to start switching all views.
StopFlipping: Stop View switching
ViewFilpper usage:
Configuration page file
<ViewFlipper
Android: id = "@ + id/flipper"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_below = "@ + id/CockpitLayout">
<Include
Android: id = "@ + id/secondlayout"
Layout = "@ layout/second">
</Include>
<Include
Android: id = "@ + id/firstlayout"
Layout = "@ layout/first">
</Include>
</ViewFlipper>
After page files are configured, you can operate ViewFilpper and set the switching animation in the code.
ViewFlow
Android-viewflow is the effect library for view switching on the Android platform.
ViewFlow is equivalent to a ViewGroup that provides horizontal scrolling for Android UI parts and binds entries using the Adapter. As mentioned in the document, you can consider using ViewFlow when you need to slide in a series of views with uncertain numbers. If the number of views is determined, you should use Fragments or ViewPager In the compatible library.
Use ViewFlow
How to use it? First, add the following content to your layout file:
<Org. taptwo. android. widget. ViewFlow
Android: id = "@ + id/viewflow"
App: sidebuffer = "5"
/>
The app: sidebuffer attribute is customized by the ViewFlow component. To use these attributes, you need to add the following xml namespace:
Xmlns: app = "http://schemas.android.com/apk/res/your.application.package.here"
Then add the following code to your Activity to use ViewFlow:
ViewFlow viewFlow = (ViewFlow) findViewById (R. id. viewflow );
ViewFlow. setAdapter (myAdapter );
// Set the position of the initial view
// ViewFlow. setAdapter (myAdapter, 8 );
// Listen to view switching events. You do not need to listen for simple requests.
ViewFlow. setOnViewSwitchListener (new ViewSwitchListener (){
Public void onSwitched (View v, int position ){
// Your code here
}
});
Of course, you can also use the FlowIndicator in this library to provide an indicator for you to switch between multiple views. Currently, this library has implemented two types of indicators: FlowIndicator; the other is the title indicator TitleFlowIndicator.
Use the dot indicator
The dot indicator can be used as follows:
Define it in layout first
<Org. taptwo. android. widget. CircleFlowIndicator
Android: padding = "10dip" android: layout_height = "wrap_content"
Android: layout_width = "wrap_content" android: id = "@ + id/viewflowindic"
Android: background = "#00000000"
/>
Then call it in activity
CircleFlowIndicator indic = (CircleFlowIndicator)
FindViewById (R. id. viewflowindic );
ViewFlow. setFlowIndicator (indic );
The dot indicator also supports activeColor, inactiveColor, activeType (fill or stroke), inactiveType (fill or stroke), and fadeOut (sets the number of seconds when the dot is automatically hidden, and will not be automatically hidden if it is 0) and radius (the radius of the dot.
Use the title indicator
The title indicator is also defined in layout:
<Org. taptwo. android. widget. TitleFlowIndicator
Android: id = "@ + id/viewflowindic"
Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent"
App: footerLineHeight = "2dp"
App: footerTriangleHeight = "10dp"
App: textColor = "# FFFFFFFF" app: selectedColor = "# FFFFC445"
App: footerColor = "# FFFFC445" app: titlePadding = "10dp"
App: textSize = "11dp" app: selectedSize = "12dp"
Android: layout_marginTop = "10dip"
App: clipPadding = "5dp"/>
Then call it in activity:
TitleFlowIndicator indicator = (TitleFlowIndicator)
FindViewById (R. id. viewflowindic );
Indicator. setTitleProvider (myTitleProvider );
ViewFlow. setFlowIndicator (indicator );
The ViewFlow Library provides three components and their usage (from its documentation). You need to download the code and put it in a package in your project, of course, you can also package the component library into a jar call.
Comparison
Based on the Usage description of the three methods above, we can see that both ViewPager and ViewFlow can use the adapter to adapt a large amount of data. In addition, ViewFlow also has a location prompt with the origin and title. The two are similar. ViewFilpper is suitable for a limited number of page switches and can customize each switching animation. It is suitable for screen switching between applications, similar to ActivityGroup.
Because ViewFlow provides source code, it is more scalable and can be customized as needed, such as loop playback.