ViewPager - Trocando a tela lateralmente arrastando com dedo

Top  Previous  Next

Tela

 

Botão direito sobre o projeto

Fica assim:

 

ViewSwinppingActivity.java

 

package ngvl.android.viewswipping;

 

import java.util.ArrayList;

import java.util.List;

 

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.os.Parcelable;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

 

public class ViewSwinppingActivity extends Activity {

 private ViewPager awesomePager;

 private static int NUM_AWESOME_VIEWS = 3;

 private Context cxt;

 private AwesomePagerAdapter awesomeAdapter;

 

 /** Called when the activity is first created. */

 @Override

 public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);

         cxt = this;

         

         awesomeAdapter = new AwesomePagerAdapter();

         awesomePager = (ViewPager) findViewById(R.id.awesomepager);

         awesomePager.setAdapter(awesomeAdapter);

 }

 

 private class AwesomePagerAdapter extends PagerAdapter {

 

         private int[] layouts = {R.layout.layout1, R.layout.layout2, R.layout.layout3};

         

         @Override

         public int getCount() {

                 return NUM_AWESOME_VIEWS;

         }

 

         /**

          * Create the page for the given position. The adapter is responsible

          * for adding the view to the container given here, although it only

          * must ensure this is done by the time it returns from

          * {@link #finishUpdate()}.

          *

          * @param container

          *            The containing View in which the page will be shown.

          * @param position

          *            The page position to be instantiated.

          * @return Returns an Object representing the new page. This does not

          *         need to be a View, but can be some other container of the

          *         page.

          */

         @Override

         public Object instantiateItem(View collection, int position) {

                 Log.d("NGVL", "instantiateItem");

                 View v = LayoutInflater.from(cxt).inflate(layouts[position], null);

                 ((ViewPager) collection).addView(v, 0);

                 

                 if (position == 0){

                 List<String> nomes = new ArrayList<String>();

                for (int i = 0; i < 1000; i++) {

                                 nomes.add("Pessoa "+ i);

                         }

                 ListView lst = (ListView)findViewById(R.id.listView1);

                 lst.setAdapter(new ArrayAdapter<String>(cxt, android.R.layout.simple_list_item_1, nomes));

                 }

 

                 return v;

         }

 

         /**

          * Remove a page for the given position. The adapter is responsible for

          * removing the view from its container, although it only must ensure

          * this is done by the time it returns from {@link #finishUpdate()}.

          *

          * @param container

          *            The containing View from which the page will be removed.

          * @param position

          *            The page position to be removed.

          * @param object

          *            The same object that was returned by

          *           {@link #instantiateItem(View, int)}.

          */

         @Override

         public void destroyItem(View collection, int position, Object view) {

                 ((ViewPager) collection).removeView((View)view);

                 Log.d("NGVL", "destroyItem");

         }

 

         @Override

         public boolean isViewFromObject(View view, Object object) {

                 Log.d("NGVL", "isViewFromObject");

                 return view == ((View) object);

         }

 

         /**

          * Called when the a change in the shown pages has been completed. At

          * this point you must ensure that all of the pages have actually been

          * added or removed from the container as appropriate.

          *

          * @param container

          *            The containing View which is displaying this adapter's

          *            page views.

          */

         @Override

         public void finishUpdate(View arg0) {

                 Log.d("NGVL", "finishUpdate");

         }

 

         @Override

         public void restoreState(Parcelable arg0, ClassLoader arg1) {

                 Log.d("NGVL", "restoreState");

         }

 

         @Override

         public Parcelable saveState() {

                 Log.d("NGVL", "saveState");

                 return null;

         }

 

    /**

      * Called when a change in the shown pages is going to start being made.

      * @param container The containing View which is displaying this adapter's

      * page views.

      */

         @Override

         public void startUpdate(View arg0) {

                 Log.d("NGVL", "startUpdate");

         }

 }

}

 

Layouts

 

layout1.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

 android:id="@+id/linearLayout1"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:background="#333333"

 android:orientation="vertical"

 xmlns:android="http://schemas.android.com/apk/res/android">

 <TextView

         android:text="Tela 1"

         android:textAppearance="?android:attr/textAppearanceLarge"

         android:layout_width="wrap_content"

         android:id="@+id/textView1"

         android:layout_height="wrap_content"></TextView>

 <ListView

         android:id="@+id/listView1"

         android:layout_weight="1"

         android:layout_height="wrap_content"

         android:layout_width="match_parent"></ListView>

</LinearLayout>

 

layout2.xml

 

<LinearLayout

 android:id="@+id/linearLayout1"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:background="#00FF00"

 xmlns:android="http://schemas.android.com/apk/res/android">

 <TextView

         android:text="Tela 2"

         android:textAppearance="?android:attr/textAppearanceLarge"

         android:layout_width="wrap_content"

         android:id="@+id/textView1"

         android:layout_height="wrap_content"></TextView>

 <EditText

         android:id="@+id/editText1"

         android:layout_weight="1"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:inputType="number"

         android:maxLength="14">

         <requestFocus></requestFocus>

 </EditText>

 

 <Button

         android:text="VAI!"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:onClick="vai"/>

</LinearLayout>

 

layout3.xml

 

<LinearLayout

 android:id="@+id/linearLayout1"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:background="#0000FF"

 xmlns:android="http://schemas.android.com/apk/res/android">

 <TextView

         android:text="Tela 3"

         android:textAppearance="?android:attr/textAppearanceLarge"

         android:layout_width="wrap_content"

         android:id="@+id/textView1"

         android:layout_height="wrap_content"></TextView>

</LinearLayout>

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="#a4c639">

<android.support.v4.view.ViewPager

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:id="@+id/awesomepager"/>

</LinearLayout>

 

Manifest

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="ngvl.android.viewswipping"

    android:versionCode="1"

    android:versionName="1.0">

 

  <uses-sdk android:minSdkVersion="10" />

 

  <application android:icon="@drawable/icon" android:label="@string/app_name">

      <activity android:name=".ViewSwinppingActivity"

                android:label="@string/app_name">

          <intent-filter>

              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />

          </intent-filter>

      </activity>

 

  </application>

</manifest>