Android UI編程(9)——ViewPager、Activity

來源:互聯網
上載者:User

標籤:viewpager   activity   滑屏效果   

利用ViewPager可以實現滑屏效果,如今智能手機隨處可見滑屏效果。最常見的就是手機launcher上的滑屏,以及各大型軟體個菜單之間的滑屏,如。

參考部落格:http://blog.csdn.net/harvic880925/article/details/38521865,裡面有對ViewPager詳細講解

先看



代碼

AndroidManifest.xml——沒有做任何修改,建立工程預設

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.wxl.viewpager"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.wxl.viewpager.MainActivity"            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>

activity_main.xml

<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"    android:background="#ffffc0"    tools:context=".MainActivity" >    <LinearLayout        android:layout_width="match_parent"    android:layout_height="50dp"    android:gravity="center"    android:background="#ffffff">            <TextView             android:id="@+id/textView1"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_gravity="center"            android:layout_weight="1"            android:text=""            android:gravity="center"/>        <TextView             android:id="@+id/textView2"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="通訊錄"            android:gravity="center"/>        <TextView             android:id="@+id/textView3"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="發現"            android:gravity="center"/>        <TextView             android:id="@+id/textView4"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="我"            android:gravity="center"/>            </LinearLayout>        <LinearLayout         android:layout_width="match_parent"        android:layout_height="5dip">        <ImageView        android:id="@+id/imageView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:src="@drawable/a" />    </LinearLayout>        <android.support.v4.view.ViewPager           android:id="@+id/viewPager"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_gravity="center" >     </android.support.v4.view.ViewPager></LinearLayout>

lay1.xml

<?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:orientation="vertical"     android:gravity="center"    android:background="#123456">        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第一卡頁"        android:textSize="50sp"        android:textColor="#ffffff"/></LinearLayout>
lay2.xml

<?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:gravity="center"    android:orientation="vertical"     android:background="#cccfff">        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第二卡頁"        android:textSize="50sp"        android:textColor="#ffffff"/></LinearLayout>
lay3.xml

<?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:orientation="vertical"     android:gravity="center"    android:background="#00ff00">        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第三卡頁"        android:textSize="50sp"        android:textColor="#ffffff"/></LinearLayout>
lay4.xml

<?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:orientation="vertical"     android:gravity="center"    android:background="#ff00ff">    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第四卡頁"        android:textSize="50sp"        android:textColor="#ffffff"/>    </LinearLayout>
MainActivity.java

package com.wxl.viewpager;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import android.widget.LinearLayout.LayoutParams;import android.widget.TextView;import android.widget.Toast;import android.app.Activity;public class MainActivity extends Activity implements OnClickListener{private ViewPager viewPager;//頁卡內容 private List<View> views;// Tab頁面列表 private ImageView imageView;private View view1,view2,view3,view4;//各個頁卡  private TextView textView1,textView2,textView3,textView4;private int offset = 0;// 動畫圖片位移量      private int currIndex = 0;// 當前頁卡編號     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initImageView();        initViewPager();                textView1 = (TextView)this.findViewById(R.id.textView1);        textView2 = (TextView)this.findViewById(R.id.textView2);        textView3 = (TextView)this.findViewById(R.id.textView3);        textView4 = (TextView)this.findViewById(R.id.textView4);        textView1.setOnClickListener(this);        textView2.setOnClickListener(this);        textView3.setOnClickListener(this);        textView4.setOnClickListener(this);    }  @Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (arg0.getId()) {case R.id.textView1:{currIndex = 0;break;}case R.id.textView2:{currIndex = 1;break;}case R.id.textView3:{currIndex = 2;break;}case R.id.textView4:{currIndex = 3;break;}}viewPager.setCurrentItem(currIndex); }    /**       * 初始化動畫,這個就是頁卡滑動時,下面的橫線也滑動的效果,在這裡需要計算一些資料       */      private void initImageView() {         imageView= (ImageView) findViewById(R.id.imageView);               // 擷取螢幕密度(方法1)         int screenWidth  = getWindowManager().getDefaultDisplay().getWidth();       // 螢幕寬(像素,如:480px)                LayoutParams layoutParams = (LayoutParams)imageView.getLayoutParams();       layoutParams.width = screenWidth/4;       imageView.setLayoutParams(layoutParams);              offset = layoutParams.width;   }       private void initViewPager() {          viewPager=(ViewPager) findViewById(R.id.viewPager);          views=new ArrayList<View>();          LayoutInflater inflater=getLayoutInflater();          view1=inflater.inflate(R.layout.lay1, null);          view2=inflater.inflate(R.layout.lay2, null);          view3=inflater.inflate(R.layout.lay3, null);                view4=inflater.inflate(R.layout.lay4, null);        views.add(view1);          views.add(view2);          views.add(view3);                views.add(view4);        viewPager.setAdapter(new MyViewPagerAdapter(views));          viewPager.setCurrentItem(0);          viewPager.setOnPageChangeListener(new MyOnPageChangeListener());      }        public class MyViewPagerAdapter extends PagerAdapter{          private List<View> mListViews;                    public MyViewPagerAdapter(List<View> mListViews) {              this.mListViews = mListViews; //構造方法,參數是我們的頁卡,這樣比較方便         }            @Override          public void destroyItem(ViewGroup container, int position, Object object)   {                 container.removeView(mListViews.get(position));  //刪除頁卡        }              @Override  //這個方法用來執行個體化頁卡                 public Object instantiateItem(ViewGroup container, int position) {                         container.addView(mListViews.get(position), 0);  //添加頁卡              return mListViews.get(position);          }            @Override          public int getCount() {                       return  mListViews.size();  //返回頁卡的數量         }                    @Override          public boolean isViewFromObject(View arg0, Object arg1) {                         return arg0==arg1;  //官方提示這樣寫         }                 @Override          public int getItemPosition(Object object) {          return POSITION_NONE;          }     }         public class MyOnPageChangeListener implements OnPageChangeListener{                int one = offset;// 頁卡1 -> 頁卡2 位移量          public void onPageScrollStateChanged(int arg0) {                                      }            public void onPageScrolled(int arg0, float arg1, int arg2) {                                      }            public void onPageSelected(int arg0) {                           Animation animation = new TranslateAnimation(one*currIndex, one*arg0, 0, 0);            currIndex = arg0;              animation.setFillAfter(true);// True:圖片停在動畫結束位置              animation.setDuration(300);              imageView.startAnimation(animation);              Toast.makeText(MainActivity.this, "您選擇了"+ (viewPager.getCurrentItem()+1)+"頁卡", Toast.LENGTH_SHORT).show();          }                }}
總結

擷取布局視圖

LayoutInflater inflater=getLayoutInflater();  view1=inflater.inflate(R.layout.lay1, null);  view2=inflater.inflate(R.layout.lay2, null);  view3=inflater.inflate(R.layout.lay3, null);        view4=inflater.inflate(R.layout.lay4, null);
缺陷

點擊標題列中的“”、“通訊錄”、“發現”、“我”的時候,卡頁之間不是直接跳躍過去,而不是一步一步跳過去的。即如果當前卡頁處於卡頁1,現在點擊到卡頁3時,是先跳躍到卡頁2,在跳躍卡頁3的,可以很明顯看出。


Android UI編程(9)——ViewPager、Activity

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.