View pager for android (11), androidviewpager

Source: Internet
Author: User

View pager for android (11), androidviewpager

The ViewPager function provides interface switching. we can define a group of views and switch them between left and right in the current interface.

When using ViewPager, we need the following preparations:

1. Prepare the adapter:

<span style="font-size:18px;">PagerAdapter mPagerAdapter=new PagerAdapter() {@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0==arg1;}@Overridepublic int getCount() {return viewList.size();}@Overridepublic void destroyItem(View container, int position, Object object) {// TODO Auto-generated method stub((ViewPager)container).removeView(viewList.get(position));}@Overridepublic Object instantiateItem(View container, int position) {((ViewPager)container).addView(viewList.get(position));return  viewList.get(position);}};</span>


The ViewPager adapter inherits from the PagerAdapter base class and implements the following methods: 

Determine whether the object production interface is used

<span style="font-size:18px;">public boolean isViewFromObject(View arg0, Object arg1){}</span>


Obtain the total number of views to be displayed.

<span style="font-size:18px;">public int getCount() {}</span>


Destroy position page

<span style="font-size:18px;">public void destroyItem(View container, int position, Object object) {}</span>


Initial position location testing interface

<span style="font-size:18px;">public Object instantiateItem(View container, int position) {}</span>


2. Register a listener event

<span style="font-size:18px;">public class PageChangeListener implements OnPageChangeListener{@Overridepublic void onPageScrollStateChanged(int arg0) {}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageSelected(int arg0) {}}</span>

Implementation Method:

<span style="font-size:18px;">public void onPageScrollStateChanged(int arg0) {}</span>
This method is called when the state changes. There are three States (0, 1, 2) in arg0. When it is 0, it indicates that nothing is done currently; when it is 1, it indicates that it is sliding; if the value is 2, the sliding is complete.

<span style="font-size:18px;">public void onPageScrolled(int arg0, float arg1, int arg2) {}</span>
This method is called during sliding. It is always called before the sliding stops. arg0 indicates the page currently sliding, arg1 indicates the page offset percentage, and arg2 indicates the pixel position of the page offset.


<span style="font-size:18px;">public void onPageSelected(int arg0) {}</span>
This method is called after the jump, and arg0 is the currently selected page.


Next we will implement the following interface after login:


Login_anim.xml layout file:

<span style="font-size:18px;"><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <android.support.v4.view.ViewPager        android:id="@+id/vp_login"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" >            </android.support.v4.view.ViewPager>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal" >        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="bottom"            android:layout_marginBottom="30dp"            android:gravity="center_horizontal" >            <ImageView                android:id="@+id/iv_page0"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:scaleType="matrix"                android:src="@drawable/page_now" />            <ImageView                 android:id="@+id/iv_page1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:scaleType="matrix"                android:src="@drawable/page"/>            <ImageView                 android:id="@+id/iv_page2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:scaleType="matrix"                android:src="@drawable/page"/>            <ImageView                 android:id="@+id/iv_page3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:scaleType="matrix"                android:src="@drawable/page"/>            <ImageView                 android:id="@+id/iv_page4"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:scaleType="matrix"                android:src="@drawable/page"/>            <ImageView                 android:id="@+id/iv_page5"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:scaleType="matrix"                android:src="@drawable/page"/>        </LinearLayout>    </LinearLayout></FrameLayout></span>

The imageviews defined above are the dots at the bottom of the Image View. Later, the image View icons are changed by listening to the sliding events of ViewPager to emphasize the currently displayed View.


<span style="font-size:18px;">public class LoginAnimActivity extends Activity implements OnClickListener{private ViewPager vp_login;private ImageView iv_page0;private ImageView iv_page1;private ImageView iv_page2;private ImageView iv_page3;private ImageView iv_page4;private ImageView iv_page5;private Button btn_start;private int currIndex=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.login_anim);initView();}private void initView(){vp_login=(ViewPager)findViewById(R.id.vp_login);</span>
<Span style = "font-size: 18px;"> // register the listening event vp_login.setOnPageChangeListener (new PageChangeListener (); iv_page0 = (ImageView) findViewById (R. id. iv_page0); iv_page1 = (ImageView) findViewById (R. id. iv_page1); iv_page2 = (ImageView) findViewById (R. id. iv_page2); iv_page3 = (ImageView) findViewById (R. id. iv_page3); iv_page4 = (ImageView) findViewById (R. id. iv_page4); iv_page5 = (ImageView) findViewById (R. id. iv_page5); </span>
<Span style = "font-size: 18px;"> // bind the adapter vp_login.setAdapter (inflaterView ();}/** load data VIEW */private PagerAdapter inflaterView () {</span>
<Span style = "font-size: 18px;"> LayoutInflater layoutInflater = LayoutInflater. from (this); View view1 = layoutInflater. inflate (R. layout. login_anim1, null); // these views are the view view2 = layoutInflater to be displayed. inflate (R. layout. login_anim2, null); View view3 = layoutInflater. inflate (R. layout. login_anim3, null); View view4 = layoutInflater. inflate (R. layout. login_anim4, null); View view5 = layoutInflater. inflate (R. layout. login_anim5, null); View view6 = layoutInflater. inflate (R. layout. login_anim6, null); btn_start = (Button) view6.findViewById (R. id. btn_start); final ArrayList <View> viewList = new ArrayList <View> (); // Add these views to the viewList in the set. add (view1); viewList. add (view2); viewList. add (view3); viewList. add (view4); viewList. add (view5); viewList. add (view6); PagerAdapter mPagerAdapter = new PagerAdapter () {// load these views @ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;} @ Overridepublic int getCount () {return viewList. size () ;}@ Overridepublic void destroyItem (View container, int position, Object object Object) {// TODO Auto-generated method stub (ViewPager) container ). removeView (viewList. get (position) ;}@ Overridepublic Object instantiateItem (View container, int position) {(ViewPager) container ). addView (viewList. get (position); return viewList. get (position) ;}}; return mPagerAdapter ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_start: // start break; default: break;} public class PageChangeListener implements OnPageChangeListener {@ Overridepublic void merge (int arg0) {}@ Overridepublic void merge (int arg0, float arg1, int arg2) {}@ Overridepublic void onPageSelected (int arg0) {switch (arg0) {case 0: iv_page0.setImageDrawable (getResources (). getDrawable (R. drawable. page_now); iv_page1.setImageDrawable (getResources (). getDrawable (R. drawable. page); break; case 1: iv_page1.setImageDrawable (getResources (). getDrawable (R. drawable. page_now); iv_page0.setImageDrawable (getResources (). getDrawable (R. drawable. page); iv_page2.setImageDrawable (getResources (). getDrawable (R. drawable. page); break; case 2: iv_page2.setImageDrawable (getResources (). getDrawable (R. drawable. page_now); iv_page1.setImageDrawable (getResources (). getDrawable (R. drawable. page); iv_page3.setImageDrawable (getResources (). getDrawable (R. drawable. page); break; case 3: iv_page3.setImageDrawable (getResources (). getDrawable (R. drawable. page_now); iv_page2.setImageDrawable (getResources (). getDrawable (R. drawable. page); iv_page4.setImageDrawable (getResources (). getDrawable (R. drawable. page); break; case 4: iv_page4.setImageDrawable (getResources (). getDrawable (R. drawable. page_now); iv_page3.setImageDrawable (getResources (). getDrawable (R. drawable. page); iv_page5.setImageDrawable (getResources (). getDrawable (R. drawable. page); break; case 5: iv_page5.setImageDrawable (getResources (). getDrawable (R. drawable. page_now); iv_page4.setImageDrawable (getResources (). getDrawable (R. drawable. page); break; default: break; }}}</span>


Login_anim1 layout file:

<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ drawable/w01"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "\ n High imitation \ n is the best way to learn development" android: layout_alignParentTop = "true" android: layout_marginTop = "35dp" android: textSize = "22sp" android: textColor = "# fff"/> </RelativeLayout> </span>

Login_anim2 layout file:

<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: background = "@ drawable/w02" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "\ n press and hold to talk \ n chat is so simple and easy" android: layout_alignParentTop = "true" android: layout_marginTop = "35dp" android: textSize = "22sp" android: textColor = "# fff"/> </RelativeLayout> </span>


Login_anim3 layout file:

<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: background = "@ drawable/w03" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "through video chat \ n you can even face-to-face communication with friends \ n" android: layout_alignParentTop = "true" android: layout_marginTop = "35dp" android: textSize = "22sp" android: textColor = "# fff"/> </RelativeLayout> </span>


Login_anim4 layout file:

<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: background = "@ drawable/w04" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "Shake your cell phone \ n or someone nearby \ n knows more" android: layout_alignParentTop = "true" android: layout_marginTop = "35dp" android: textSize = "22sp" android: textColor = "# fff"/> </RelativeLayout> </span>


Login_anim5 layout file:

<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: background = "@ drawable/w05" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "\ n you can also share your life with friends through the circle of friends \ n" android: layout_alignParentTop = "true" android: layout_marginTop = "35dp" android: textSize = "22sp" android: textColor = "# fff"/> </RelativeLayout> </span>


Login_anim6 layout file:

<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: background = "@ drawable/w01" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "\ n \ nOK, witness the power of high imitation. "android: layout_alignParentTop =" true "android: layout_marginTop =" 35dp "android: textSize =" 22sp "android: textColor = "# fff"/> <Button android: id = "@ + id/btn_start" android: layout_width = "120dp" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true" android: layout_marginBottom = "120dp" android: text = "" android: textSize = "18sp" android: textColor = "# fff" android: background = "@ drawable/btn_style_green" android: layout_gravity = "center_vertical"/> </RelativeLayout> </span>





Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/42277073 sentiment control _







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.