Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android-ViewPager的使用

Android-ViewPager的使用

編輯:關於Android編程

Android-ViewPager的使用


ViewPager是安卓App很常用的工具類,通常是用來設置界面導航,比如微信,QQ都是使用這種,還有就是新聞類App,很常見
比如:
這裡寫圖片描述
這就是一個典型的例子。
復習一下這個知識點。


大致步驟和之前的ListView差不多。
1.構造適配器
2.數據源
3.加載適配器
這裡寫圖片描述


activity_main.xml




    
    


ViewPager使用v4的兼容包
view1-view4.xml




    

自定義適配器
MyPagerAdapter.java

package com.xieth.as.againviewpagerdemo;

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

import java.util.List;

/**
 * Created by YR on 2016/04/05.
 */
public class MyPagerAdapter extends PagerAdapter{

    private List viewList = null;

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

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

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

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}

MainActivity.java

package com.xieth.as.againviewpagerdemo;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

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

public class MainActivity extends AppCompatActivity {

    private ViewPager pager = null;
    private MyPagerAdapter adapter = null;
    private View[] views = new View[4];
    private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
    //數據源
    private List viewList = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        adapter = new MyPagerAdapter(viewList);
        pager.setAdapter(adapter);

    }

    private void initViews() {
        pager = (ViewPager) findViewById(R.id.id_viewpager);

        viewList = new ArrayList<>();
        for (int i = 0; i < viewId.length; i++) {
            views[i] = View.inflate(this, viewId[i], null);
            viewList.add(views[i]);
        }
    }

}

運行:
這裡寫圖片描述
這是一個簡單的例子,然後可以在上面添加標題欄。
使用android.support.v4.view.PagerTabStrip<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjxiciAvPg0KPHN0cm9uZz5hY3Rpdml0eV9tYWluLnhtbDwvc3Ryb25nPjwvcD4NCjxwcmUgY2xhc3M9"brush:java;">

MainActivity.java

package com.xieth.as.againviewpagerdemo;

import android.os.Bundle;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

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

public class MainActivity extends AppCompatActivity {

    private ViewPager pager = null;
    private MyPagerAdapter adapter = null;
    private View[] views = new View[4];
    private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
    private String[] titleStr = {"第一頁", "第二頁", "第三頁", "第四頁"};
    //數據源
    private List viewList = null;
    private List titleList = null;
    // 標題
    private PagerTabStrip tab = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        adapter = new MyPagerAdapter(viewList, titleList);
        pager.setAdapter(adapter);

    }

    private void initViews() {
        pager = (ViewPager) findViewById(R.id.id_viewpager);
        tab = (PagerTabStrip) findViewById(R.id.id_tab);
        viewList = new ArrayList<>();
        titleList = new ArrayList<>();
        for (int i = 0; i < viewId.length; i++) {
            views[i] = View.inflate(this, viewId[i], null);
            viewList.add(views[i]);
        }
        for (int i = 0; i < titleStr.length; i++) titleList.add(titleStr[i]);
    }

}

運行:
這裡寫圖片描述


可以把下面長的短線去掉:

tab.setDrawFullUnderline(false);

運行:
這裡寫圖片描述


除了這樣加載,還可以使用Fragment作為載體。
fragment1-fragment4.java
id改一下即可

package com.xieth.as.againviewpagerdemo;

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

/**
 * Created by YR on 2016/04/05.
 */
public class Fragment1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.view1, container, false);
    }
}

自定義適配器
MyFragmentPagerAdapter.java

package com.xieth.as.againviewpagerdemo;

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

import java.util.List;

/**
 * Created by YR on 2016/04/05.
 */
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    //數據源
    private List fragList = null;
    private List titleList = null;

    public MyFragmentPagerAdapter(FragmentManager fm, List fragList, List titleList) {
        super(fm);
        this.fragList = fragList;
        this.titleList = titleList;
    }

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

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

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

MainActivity.java

package com.xieth.as.againviewpagerdemo;

import android.os.Bundle;
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.view.View;

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

public class MainActivity extends AppCompatActivity {

    private ViewPager pager = null;
    private MyPagerAdapter adapter = null;
    private View[] views = new View[4];
    private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
    private String[] titleStr = {"第一頁", "第二頁", "第三頁", "第四頁"};
    //數據源
    private List viewList = null;
    private List titleList = null;
    // 標題
    private PagerTabStrip tab = null;

    // Fragment適配器
    private MyFragmentPagerAdapter myFragmentPagerAdapter = null;

    private List fragList = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        tab.setDrawFullUnderline(false);

        adapter = new MyPagerAdapter(viewList, titleList);
       // pager.setAdapter(adapter);

        pager.setAdapter(myFragmentPagerAdapter);
    }

    private void initViews() {
        pager = (ViewPager) findViewById(R.id.id_viewpager);
        tab = (PagerTabStrip) findViewById(R.id.id_tab);
        fragList = new ArrayList<>();
        viewList = new ArrayList<>();
        titleList = new ArrayList<>();
        for (int i = 0; i < viewId.length; i++) {
            views[i] = View.inflate(this, viewId[i], null);
            viewList.add(views[i]);
        }
        for (int i = 0; i < titleStr.length; i++) titleList.add(titleStr[i]);

        fragList.add(new Fragment1());
        fragList.add(new Fragment2());
        fragList.add(new Fragment3());
        fragList.add(new Fragment4());

        // Frgament適配器
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList, titleList);
    }

}

運行:
這裡寫圖片描述
基本上是一致的:
然後可以打印一下,獲取當前頁面是第幾個頁面:
需要實現ViewPager.OnPageChangeListener接口

@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) {

    }

記得設置事件:

pager.setOnPageChangeListener(this);

運行:
這裡寫圖片描述


完整代碼:

package com.xieth.as.againviewpagerdemo;

import android.os.Bundle;
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.view.View;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    private ViewPager pager = null;
    private MyPagerAdapter adapter = null;
    private View[] views = new View[4];
    private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
    private String[] titleStr = {"第一頁", "第二頁", "第三頁", "第四頁"};
    //數據源
    private List viewList = null;
    private List titleList = null;
    // 標題
    private PagerTabStrip tab = null;

    // Fragment適配器
    private MyFragmentPagerAdapter myFragmentPagerAdapter = null;

    private List fragList = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        tab.setDrawFullUnderline(false);

        adapter = new MyPagerAdapter(viewList, titleList);
       // pager.setAdapter(adapter);

        pager.setAdapter(myFragmentPagerAdapter);
        pager.setOnPageChangeListener(this);
    }

    private void initViews() {
        pager = (ViewPager) findViewById(R.id.id_viewpager);
        tab = (PagerTabStrip) findViewById(R.id.id_tab);
        fragList = new ArrayList<>();
        viewList = new ArrayList<>();
        titleList = new ArrayList<>();
        for (int i = 0; i < viewId.length; i++) {
            views[i] = View.inflate(this, viewId[i], null);
            viewList.add(views[i]);
        }
        for (int i = 0; i < titleStr.length; i++) titleList.add(titleStr[i]);

        fragList.add(new Fragment1());
        fragList.add(new Fragment2());
        fragList.add(new Fragment3());
        fragList.add(new Fragment4());

        // Frgament適配器
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList, titleList);
    }

    @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) {

    }
}

OK。
記錄一下。


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved