ViewPager Entry 1: ViewPager entry

Source: Internet
Author: User

ViewPager Entry 1: ViewPager entry

You can use ViewPager to switch between different views.

For example, the sliding between the four views is realized.


1. Create a project and introduce the ViewPager control ViewPager. It is a class that comes with an additional package in the google SDk and can be used to switch between screens.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.skyfin.viewpage.MainActivity" ><android.support.v4.view.ViewPager    android:id="@+id/pager"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    ></android.support.v4.view.ViewPager></LinearLayout>
2. Create four new LinearLayout views for slide Switching
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"        android:background="@color/antiquewhite"    android:gravity="center"    android:orientation="vertical" >    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="tab2"        android:textSize="20dp"        android:textColor="@color/black"        /></LinearLayout>
3. Load the view into Viewpager.
Import android. r. id; import android. app. activity; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. pagerTabStrip; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. util. log; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; public class MainActivity extends Activity {ViewPager = null; public String TAG = "tag"; ArrayList <View> viewContainter = new ArrayList <View> (); arrayList <String> titleContainer = new ArrayList <String> (); @ SuppressWarnings ("deprecation") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ViewPager = (ViewPager) findViewById (R. id. pager); View view1 = LayoutInflater. from (this ). inflate (R. layout. tab1, null); View view2 = LayoutInflater. from (this ). inflate (R. layout. tab2, null); View view3 = LayoutInflater. from (this ). inflate (R. layout. tab3, null); View view4 = LayoutInflater. from (this ). inflate (R. layout. tab4, null); // viewpager starts to add viewContainter. add (view1); viewContainter. add (view2); viewContainter. add (view3); viewContainter. add (view4); ViewPager. setAdapter (new PagerAdapter () {// number of components in viewpager @ Override public int getCount () {return viewContainter. size () ;}// destroy the current component @ Override public void destroyItem (ViewGroup container, int position, Object object) {(ViewPager) container) during sliding switching ). removeView (viewContainter. get (position);} // component generated during each slide @ Override public Object instantiateItem (ViewGroup container, int position) {(ViewPager) container ). addView (viewContainter. get (position); return viewContainter. get (position) ;}@ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}@ Override public int getItemPosition (Object object) {return super. getItemPosition (object) ;}}); ViewPager. setOnPageChangeListener (new OnPageChangeListener () {@ Override public void onPageScrollStateChanged (int arg0) {}@ Override public void onPageScrolled (int arg0, float arg1, int arg2) {}@ Override public void onPageSelected (int arg0) {}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
Code Description 1. Define a ViewPager container
ViewPager ViewPager = null;ArrayList<View> viewContainter = new ArrayList<View>();
2. initialize and change the View and ViewPager and add them to the viewContainter container of the View.
View view1 = LayoutInflater. from (this ). inflate (R. layout. tab1, null); View view2 = LayoutInflater. from (this ). inflate (R. layout. tab2, null); View view3 = LayoutInflater. from (this ). inflate (R. layout. tab3, null); View view4 = LayoutInflater. from (this ). inflate (R. layout. tab4, null); // viewpager starts to add viewContainter. add (view1); viewContainter. add (view2); viewContainter. add (view3); viewContainter. add (view4 );
3. Add to PagerAdapter and set the listener
ViewPager. setAdapter (new PagerAdapter () {// number of components in viewpager @ Override public int getCount () {return viewContainter. size () ;}// destroy the current component @ Override public void destroyItem (ViewGroup container, int position, Object object) {(ViewPager) container) during sliding switching ). removeView (viewContainter. get (position);} // component generated during each slide @ Override public Object instantiateItem (ViewGroup container, int position) {(ViewPager) container ). addView (viewContainter. get (position); return viewContainter. get (position) ;}@ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}@ Override public int getItemPosition (Object object) {return super. getItemPosition (object) ;}@ Override public CharSequence getPageTitle (int position) {return titleContainer. get (position) ;}}); ViewPager. setOnPageChangeListener (new OnPageChangeListener () {@ Override public void onPageScrollStateChanged (int arg0) {}@ Override public void onPageScrolled (int arg0, float arg1, int arg2) {}@ Override public void onPageSelected (int arg0 ){}});

The following four functions must be rewritten by PageAdapter:

  • Boolean isViewFromObject (View arg0, Object arg1)
  • Int getCount ()
  • Void destroyItem (ViewGroup container, int position, Object object Object)
  • Object instantiateItem (ViewGroup container, int position)

Let's first look at each function. What have we done above:

[Java]View plaincopy
  1. @ Override
  2. Public int getCount (){
  3. // TODO Auto-generated method stub
  4. Return viewList. size ();
  5. }
GetCount (): returns the number of views to slide.

[Java]View plaincopy
  1. @ Override
  2. Public void destroyItem (ViewGroup container, int position,
  3. Object object ){
  4. // TODO Auto-generated method stub
  5. Container. removeView (viewList. get (position ));
  6. }
DestroyItem (): deletes the View at the specified position from the current container;
[Java]View plaincopy
  1. @ Override
  2. Public Object instantiateItem (ViewGroup container, int position ){
  3. // TODO Auto-generated method stub
  4. Container. addView (viewList. get (position ));
  5. Return viewList. get (position );
  6. }
  7. };
InstantiateItem (): do two things: first: add the current View to the container, and second: return the current View
[Java]View plaincopy
  1. @ Override
  2. Public boolean isViewFromObject (View arg0, Object arg1 ){
  3. // TODO Auto-generated method stub
  4. Return arg0 = arg1;
  5. }
IsViewFromObject ()

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.