本節(jié)帶來的是Android 3.0后引入的一個UI控件——ViewPager(視圖滑動切換工具),實(shí)在想不到 如何來稱呼這個控件,他的大概功能:通過手勢滑動可以完成View的切換,一般是用來做APP 的引導(dǎo)頁或者實(shí)現(xiàn)圖片輪播,因?yàn)槭?.0后引入的,如果想在低版本下使用,就需要引入v4 兼容包哦~,我們也可以看到,ViewPager在:android.support.v4.view.ViewPager目錄下~ 下面我們就來學(xué)習(xí)一下這個控件的基本用法~ 官方API文檔:ViewPager
ViewPager就是一個簡單的頁面切換組件,我們可以往里面填充多個View,然后我們可以左 右滑動,從而切換不同的View,我們可以通過setPageTransformer()方法為我們的ViewPager 設(shè)置切換時的動畫效果,當(dāng)然,動畫我們還沒學(xué)到,所以我們把為ViewPager設(shè)置動畫 放到下一章繪圖與動畫來講解!和前面學(xué)的ListView,GridView一樣,我們也需要一個Adapter (適配器)將我們的View和ViewPager進(jìn)行綁定,而ViewPager則有一個特定的Adapter—— PagerAdapter!另外,Google官方是建議我們使用Fragment來填充ViewPager的,這樣 可以更加方便的生成每個Page,以及管理每個Page的生命周期!給我們提供了兩個Fragment 專用的Adapter:FragmentPageAdapter和FragmentStatePagerAdapter 我們簡要的來分析下這兩個Adapter的區(qū)別:
- FragmentPageAdapter:和PagerAdapter一樣,只會緩存當(dāng)前的Fragment以及左邊一個,右邊 一個,即總共會緩存3個Fragment而已,假如有1,2,3,4四個頁面:
處于1頁面:緩存1,2
處于2頁面:緩存1,2,3
處于3頁面:銷毀1頁面,緩存2,3,4
處于4頁面:銷毀2頁面,緩存3,4
更多頁面的情況,依次類推~- FragmentStatePagerAdapter:當(dāng)Fragment對用戶不 見得時,整個Fragment會被銷毀, 只會保存Fragment的狀態(tài)!而在頁面需要重新顯示的時候,會生成新的頁面!
綜上,F(xiàn)ragmentPageAdapter適合固定的頁面較少的場合;而FragmentStatePagerAdapter則適合 于頁面較多或者頁面內(nèi)容非常復(fù)雜(需占用大量內(nèi)存)的情況!
我們先來介紹最普通的PagerAdapter,如果想使用這個PagerAdapter需要重寫下面的四個方法: 當(dāng)然,這只是官方建議,實(shí)際上我們只需重寫getCount()和isViewFromObject()就可以了~
- getCount():獲得viewpager中有多少個view
- destroyItem():移除一個給定位置的頁面。適配器有責(zé)任從容器中刪除這個視圖。 這是為了確保在finishUpdate(viewGroup)返回時視圖能夠被移除。
而另外兩個方法則是涉及到一個key的東東:
- instantiateItem(): ①將給定位置的view添加到ViewGroup(容器)中,創(chuàng)建并顯示出來 ②返回一個代表新增頁面的Object(key),通常都是直接返回view本身就可以了,當(dāng)然你也可以 自定義自己的key,但是key和每個view要一一對應(yīng)的關(guān)系
- isViewFromObject(): 判斷instantiateItem(ViewGroup, int)函數(shù)所返回來的Key與一個頁面視圖是否是 代表的同一個視圖(即它倆是否是對應(yīng)的,對應(yīng)的表示同一個View),通常我們直接寫 return view == object!
運(yùn)行效果圖:
關(guān)鍵部分代碼:
好的,代碼寫起來也是非常簡單的:首先是每個View的布局,一式三份,另外兩個View一樣:
view_one.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:background="#FFBA55"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個Page"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
然后編寫一個自定義個的PagerAdapter:
MyPagerAdapter.java:
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<View> viewLists;
public MyPagerAdapter() {
}
public MyPagerAdapter(ArrayList<View> viewLists) {
super();
this.viewLists = viewLists;
}
@Override
public int getCount() {
return viewLists.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewLists.get(position));
return viewLists.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewLists.get(position));
}
}
接著到Activity了,和以前學(xué)的ListView非常類似:
OneActivity.java:
public class OneActivity extends AppCompatActivity{
private ViewPager vpager_one;
private ArrayList<View> aList;
private MyPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
vpager_one = (ViewPager) findViewById(R.id.vpager_one);
aList = new ArrayList<View>();
LayoutInflater li = getLayoutInflater();
aList.add(li.inflate(R.layout.view_one,null,false));
aList.add(li.inflate(R.layout.view_two,null,false));
aList.add(li.inflate(R.layout.view_three,null,false));
mAdapter = new MyPagerAdapter(aList);
vpager_one.setAdapter(mAdapter);
}
}
好的,關(guān)鍵代碼就上述部分,非常容易理解~
就是跟隨著ViewPager滑動而滑動的標(biāo)題咯,這兩個是官方提供的,一個是普通文字, 一個是帶有下劃線,以及可以點(diǎn)擊文字可切換頁面,下面我們來寫個簡單的例子~
運(yùn)行效果圖:
關(guān)鍵代碼實(shí)現(xiàn):
這里兩者的區(qū)別僅僅是布局不一樣而已,其他的都一樣:
PagerTitleStrip所在Activtiy的布局: activity_two.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#CCFF99"
android:gravity="center"
android:text="PagerTitleStrip效果演示"
android:textColor="#000000"
android:textSize="18sp" />
<android.support.v4.view.ViewPager
android:id="@+id/vpager_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="top"
android:textColor="#FFFFFF" />
</android.support.v4.view.ViewPager>
</LinearLayout>
而PagerTabStrip所在的布局:
activity_three.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#C0C080"
android:gravity="center"
android:text="PagerTabStrip效果演示"
android:textSize="18sp" />
<android.support.v4.view.ViewPager
android:id="@+id/vpager_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v4.view.PagerTabStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</LinearLayout>
接下來的兩者都一樣了,我們先來編寫一個自定義的PagerAdapter,除了前面重寫的 四個方法外,我們需要另外重寫一個方法:getPageTitle(),這個設(shè)置標(biāo)題的~ 代碼如下:
MyPagerAdapter2.java:
/**
* Created by Jay on 2015/10/8 0008.
*/
public class MyPagerAdapter2 extends PagerAdapter {
private ArrayList<View> viewLists;
private ArrayList<String> titleLists;
public MyPagerAdapter2() {}
public MyPagerAdapter2(ArrayList<View> viewLists,ArrayList<String> titleLists)
{
this.viewLists = viewLists;
this.titleLists = titleLists;
}
@Override
public int getCount() {
return viewLists.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewLists.get(position));
return viewLists.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewLists.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
return titleLists.get(position);
}
}
最后是Activity部分,兩個都是一樣的:
TwoActivity.java:
/**
* Created by Jay on 2015/10/8 0008.
*/
public class TwoActivity extends AppCompatActivity {
private ViewPager vpager_two;
private ArrayList<View> aList;
private ArrayList<String> sList;
private MyPagerAdapter2 mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
vpager_two = (ViewPager) findViewById(R.id.vpager_two);
aList = new ArrayList<View>();
LayoutInflater li = getLayoutInflater();
aList.add(li.inflate(R.layout.view_one,null,false));
aList.add(li.inflate(R.layout.view_two,null,false));
aList.add(li.inflate(R.layout.view_three, null, false));
sList = new ArrayList<String>();
sList.add("橘黃");
sList.add("淡黃");
sList.add("淺棕");
mAdapter = new MyPagerAdapter2(aList,sList);
vpager_two.setAdapter(mAdapter);
}
}
好了,非常簡單,有疑問的話,自己下demo看看就懂了~
當(dāng)然,示例2很多時候,只是中看不中用,實(shí)際開發(fā)中我們可能需要自行定制這個標(biāo)題欄, 下面我們就來寫個簡單的例子來實(shí)現(xiàn)TabHost的效果,如果你不知道TabHost是什么鬼的 話,那么,請看效果圖!
運(yùn)行效果圖:
實(shí)現(xiàn)邏輯解析:
下面我們來講解下實(shí)現(xiàn)上述效果的邏輯,然后貼代碼:
首先是布局:頂部一個LinearLayout,包著三個TextView,weight屬性都為1,然后下面跟著 一個滑塊的ImageView,我們設(shè)置寬度為match_parent;最底下是我們的ViewPager,這里可能 有兩個屬性你并不認(rèn)識,一個是:flipInterval:這個是指定View動畫間的時間間隔的!
而persistentDrawingCache:則是設(shè)置控件的繪制緩存策略,可選值有四個:
- none:不在內(nèi)存中保存繪圖緩存;
- animation:只保存動畫繪圖緩存;
- scrolling:只保存滾動效果繪圖緩存;
- all:所有的繪圖緩存都應(yīng)該保存在內(nèi)存中;
可以同時用2個,animation|scrolling這樣~
布局代碼:activity_four.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#FFFFFF">
<TextView
android:id="@+id/tv_one"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="橘黃"
android:textColor="#000000" />
<TextView
android:id="@+id/tv_two"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="淡黃"
android:textColor="#000000" />
<TextView
android:id="@+id/tv_three"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="淺棕"
android:textColor="#000000" />
</LinearLayout>
<ImageView
android:id="@+id/img_cursor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="https://atts.w3cschool.cn/attachments/image/cimg/>
<android.support.v4.view.ViewPager
android:id="@+id/vpager_four"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1.0"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
</LinearLayout>
接著到我們的Activity了,我們來捋下思路:
Step 1:我們需要讓我們的移動塊在第一個文字下居中,那這里就要算一下偏移量: 先獲得圖片寬度pw,然后獲取屏幕寬度sw,計算方法很簡單:
offset(偏移量) = ((sw / 3)-pw) / 2 //屏幕寬/3 - 圖片寬度,然后再除以2,左右嘛!
然后我么你調(diào)用setImageMatrix設(shè)置滑塊當(dāng)前的位置:
同時我們也把切換一頁和兩頁,滑塊的移動距離也算出來,很簡單:
one = offset 2 + pw;
two = one 2;- Step 2:當(dāng)我們滑動頁面時,我們的滑塊要進(jìn)行移動,我們要為ViewPager添加一個 OnPageChangeListener事件,我們需要對滑動后的頁面來做一個判斷,同時記錄滑動前處于 哪個頁面,下面自己畫了個圖,可能更容易理解吧!
PS:太久沒寫字,字很丑,能看清就好,字丑人美,哈哈~
嗯,如果還是不能理解的話,自己動手畫畫圖就知道了,下面上代碼:
FourActvitiy.java:
/**
* Created by Jay on 2015/10/8 0008.
*/
public class FourActivity extends AppCompatActivity implements View.OnClickListener,
ViewPager.OnPageChangeListener {
private ViewPager vpager_four;
private ImageView img_cursor;
private TextView tv_one;
private TextView tv_two;
private TextView tv_three;
private ArrayList<View> listViews;
private int offset = 0;//移動條圖片的偏移量
private int currIndex = 0;//當(dāng)前頁面的編號
private int bmpWidth;// 移動條圖片的長度
private int one = 0; //移動條滑動一頁的距離
private int two = 0; //滑動條移動兩頁的距離
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four);
initViews();
}
private void initViews() {
vpager_four = (ViewPager) findViewById(R.id.vpager_four);
tv_one = (TextView) findViewById(R.id.tv_one);
tv_two = (TextView) findViewById(R.id.tv_two);
tv_three = (TextView) findViewById(R.id.tv_three);
img_cursor = (ImageView) findViewById(R.id.img_cursor);
//下劃線動畫的相關(guān)設(shè)置:
bmpWidth = BitmapFactory.decodeResource(getResources(), R.mipmap.line).getWidth();// 獲取圖片寬度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;// 獲取分辨率寬度
offset = (screenW / 3 - bmpWidth) / 2;// 計算偏移量
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
img_cursor.setImageMatrix(matrix);// 設(shè)置動畫初始位置
//移動的距離
one = offset * 2 + bmpWidth;// 移動一頁的偏移量,比如1->2,或者2->3
two = one * 2;// 移動兩頁的偏移量,比如1直接跳3
//往ViewPager填充View,同時設(shè)置點(diǎn)擊事件與頁面切換事件
listViews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
listViews.add(mInflater.inflate(R.layout.view_one, null, false));
listViews.add(mInflater.inflate(R.layout.view_two, null, false));
listViews.add(mInflater.inflate(R.layout.view_three, null, false));
vpager_four.setAdapter(new MyPagerAdapter(listViews));
vpager_four.setCurrentItem(0); //設(shè)置ViewPager當(dāng)前頁,從0開始算
tv_one.setOnClickListener(this);
tv_two.setOnClickListener(this);
tv_three.setOnClickListener(this);
vpager_four.addOnPageChangeListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_one:
vpager_four.setCurrentItem(0);
break;
case R.id.tv_two:
vpager_four.setCurrentItem(1);
break;
case R.id.tv_three:
vpager_four.setCurrentItem(2);
break;
}
}
@Override
public void onPageSelected(int index) {
Animation animation = null;
switch (index) {
case 0:
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
}
break;
case 1:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, one, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
}
break;
case 2:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, two, 0, 0);
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
}
break;
}
currIndex = index;
animation.setFillAfter(true);// true表示圖片停在動畫結(jié)束位置
animation.setDuration(300); //設(shè)置動畫時間為300毫秒
img_cursor.startAnimation(animation);//開始動畫
}
@Override
public void onPageScrollStateChanged(int i) {
}
@Override
public void onPageScrolled(int i, float v, int i1) {
}
}
嗯,關(guān)于動畫可能你并不熟悉,沒事,下一章我們帶大家一起扣~
嗯,在前面講解Fragment的時候我們就講解了一個使用示例: Android基礎(chǔ)入門教程——5.2.4 Fragment實(shí)例精講——底部導(dǎo)航欄+ViewPager滑動切換頁面 這里就不再詳述了,有興趣的點(diǎn)下鏈接看看即可~
關(guān)于ViewPager,限于篇幅,有些地方并沒有講到,其他的大家需要自己查閱文檔了~
另外,上面也說了,ViewPager的動畫我們會在下一章講解!好的,就說這么多~
嗯,國慶前曾說會在國慶假期里完成整個系列,結(jié)果一篇都沒寫,實(shí)在抱歉... 因?yàn)槊米舆^來玩了,So,你懂的~
,會加快進(jìn)度~,爭取早日進(jìn)階!
更多建議: