【Android筆記】ViewPager實現導航

1、加入ViewPager

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foregroundGravity="center" >

</android.support.v4.view.ViewPager>

2、加載顯示的頁卡

將Layout佈局轉換爲View對象java

1.
LayoutInflater if = getLayoutInflater().from(this);
if.inflate(resource, root);

2.
View.inflate(context, resource, root);
View view1 = View.inflate(this, R.layout.view1, null);

3、配置Adapter

1.PagerAdapter   數據源:List<View>

package com.example.demo12;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by WANG on 2016/9/30 0030.
 */

public class MyPagerAdapter extends PagerAdapter {

    private List<View> viewList;

    public MyPagerAdapter(List<View> viewList) {
        this.viewList = viewList;
    }

    /**
     * 返回的是頁卡的數量
     * @return
     */
    @Override
    public int getCount() {
        return viewList.size();
    }

    /**
     * View是否來自對象
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 實例化一個頁卡
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewList.get(position));
        return viewList.get(position);
    }

    /**
     * 銷燬一個頁卡
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }
}

2.FragmentPagerAdapter  數據源:List<Fragment>

因爲FragmentPagerAdapter是一次加載全部的Fragment,不能銷燬Fragment,因此當須要加載的Fragment比較少時,可使用FragmentPagerAdapter。android

package com.example.demo12;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by WANG on 2016/9/30 0030.
 */

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragmentList;
    private List<String> titleList;


    public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) {
        super(fm);
        this.fragmentList = fragmentList;
        this.titleList = titleList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

3.FragmentStatePagerAdapter  數據源:List<Fragment>

FragmentStatePagerAdapter 能夠自動維護Fragment的建立與銷燬。app

package com.example.demo12;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by WANG on 2016/9/30 0030.
 */

public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> fragmentList;
    private List<String> titleList;


    public MyFragmentStatePagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) {
        super(fm);
        this.fragmentList = fragmentList;
        this.titleList = titleList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }

    //建立
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }
    //銷燬
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }
}

4、Adapter裏面的經常使用方法

getCount()    //須要返回全部頁面的數量

isViewFromObject(View arg0, Object arg1)    //判斷試圖是否由對象產生
 
instantiateItem(ViewGroup container, int position)    //實例化界面

destroyItem(ViewGroup container, int position, Object object)    //銷燬頁面

getPageTitle(int position)    //返回頁面標題信息

5、PagerTabStrip和PagerTitleStrip

1.PagerTabStrip

1)在ViewPager中加入PagerTabStripide

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foregroundGravity="center" >

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foregroundGravity="top" >

    </android.support.v4.view.PagerTabStrip>

</android.support.v4.view.ViewPager>

2)添加標題並進行一些設置佈局

//爲頁卡設置標題
titleList = new ArrayList<String>();
titleList.add("第一頁");
titleList.add("第二頁");
titleList.add("第三頁");
titleList.add("第四頁");

/**
 * 爲PagerTabStrip設置一些屬性
 */
tab = (PagerTabStrip) findViewById(R.id.tab);
tab.setBackgroundColor(Color.CYAN);    //背景顏色
tab.setTextColor(Color.BLACK);         //文字顏色
tab.setDrawFullUnderline(false);       //tab的底線,不顯示
tab.setTabIndicatorColor(Color.BLUE);  //當前選中的tab的底線的顏色

3)在適配器中添加設置this

/**
 * 設置ViewPager的標題
 * @param position
 * @return
 */
@Override
public CharSequence getPageTitle(int position) {
    return titileList.get(position);
}

效果以下:spa

2.PagerTitleStrip

<android.support.v4.view.PagerTitleStrip
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foregroundGravity="top" >
            
</android.support.v4.view.PagerTitleStrip>

PagerTitleStrip與PagerTabStrip類似,通常使用時選擇其中一個。code

6、監聽器的使用

添加監聽xml

實現OnPageChangeListener接口,實現onPageSelected方法:對象

@Override
public void onPageSelected(int position) {
    Toast.makeText(this, "當前是第" + (position+1) + "界面", Toast.LENGTH_SHORT).show();
}

效果以下:

實例:

//MainActivity.java

package com.example.demo12;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    private List<View> viewList;
    private ViewPager viewPager;
    private PagerTabStrip tab;
    private List<String> titleList;

    private List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewList = new ArrayList<View>();

        /**
         * 經過View對象去做爲ViewPager的數據源
         */
        View view1 = View.inflate(this, R.layout.view1, null);
        View view2 = View.inflate(this, R.layout.view2, null);
        View view3 = View.inflate(this, R.layout.view3, null);
        View view4 = View.inflate(this, R.layout.view4, null);

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);

        /**
         * 經過Fragment做爲ViewPager的數據源
         */
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());

        //爲頁卡設置標題
        titleList = new ArrayList<String>();
        titleList.add("第一頁");
        titleList.add("第二頁");
        titleList.add("第三頁");
        titleList.add("第四頁");

        /**
         * 爲PagerTabStrip設置一些屬性
         */
        tab = (PagerTabStrip) findViewById(R.id.tab);
        tab.setBackgroundColor(Color.CYAN);    //背景顏色
        tab.setTextColor(Color.BLACK);         //文字顏色
        tab.setDrawFullUnderline(false);       //tab的底線,不顯示
        tab.setTabIndicatorColor(Color.BLUE);  //當前選中的tab的底線的顏色

        //初始化ViewPager
        viewPager = (ViewPager) findViewById(R.id.pager);

        /**
         * PagerAdapter
         */
        //建立PagerAdapter的適配器
        MyPagerAdapter adapter = new MyPagerAdapter(viewList, titleList);
        //ViewPager加載適配器
        //viewPager.setAdapter(adapter);

        /**
         * FragmentPagerAdapter
         */
        //建立FragmentPagerAdapter的適配器
        MyFragmentPagerAdapter adapter1 = new MyFragmentPagerAdapter(getSupportFragmentManager(),
                                                                    fragmentList, titleList);
        //ViewPager加載適配器
        viewPager.setAdapter(adapter1);

        viewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Toast.makeText(this, "當前是第" + (position+1) + "界面", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
<!--view1.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一個界面" />

</LinearLayout>
//Fragment1.java
package com.example.demo12;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by WANG on 2016/9/30 0030.
 */

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.view1, container, false);
    }
}
相關文章
相關標籤/搜索