Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> ViewPager初體驗

ViewPager初體驗

編輯:關於Android編程

這個是第一種方法,將View轉為對象來加載。下面還會有第二種方法通過Fragment加載視圖。
先上圖
效果圖
下面放碼過來了
先上資源母乳!2個java文件,5個xml,View1-4位圖片所見的可滑動的四個界面這裡寫圖片描述
activity_main.xml




    

        

        

    

view1-4類似,上一個




    

MyPagerAdapter.class
繼承PagerAdapter加入兩個方法,後面幾個需要自己加進去

package com.superxingyun.pageradapter;

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

import java.util.List;

/**
 * Created by 月滿軒尼詩 on 2016/8/16.
 */
public class MyPagerAdapter extends PagerAdapter{

    private ListviewList;
    private ListtitleList;
    MyPagerAdapter(List viewList, ListtitleList){
        this.viewList = viewList;
        this.titleList = titleList;
    }

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

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

    /**
     *實例化一個頁卡
    */
    @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 CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

MainActivity.class
看注釋就能看清楚了

package com.superxingyun.pageradapter;

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

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

public class MainActivity extends AppCompatActivity {

    private ListviewList;
    private ViewPager pager;
    private PagerTabStrip pagerTabStrip;
    private ListtitleList;

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

        //初始化
        viewList = new ArrayList<>();
        pager = (ViewPager) findViewById(R.id.pager);

        //將視圖轉化為對象
        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);

        //為ViewPager頁卡設置標題
        titleList = new ArrayList();
        titleList.add("第一頁");
        titleList.add("第二頁");
        titleList.add("第三頁");
        titleList.add("第四頁");

        //為PagerTabStrip設置屬性
        pagerTabStrip = (PagerTabStrip) findViewById(R.id.tab);
        pagerTabStrip.setBackgroundColor(Color.MAGENTA);
        pagerTabStrip.setTextColor(Color.WHITE);
        pagerTabStrip.setDrawFullUnderline(false);
        pagerTabStrip.setTabIndicatorColor(Color.GREEN);

        //創建PagerAdapter的適配器
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(viewList, titleList);

        //ViewPager加載適配器
        pager.setAdapter(myPagerAdapter);

    }
}

使用Fragment加載:
其實效果是一樣的,只不過上面是的3個一組,而這個放在一起了
添加5個java文件
Fragment1-4:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> package com.superxingyun.pageradapter; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.Layout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by 月滿軒尼詩 on 2016/8/17. */ public class Fragment1 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { return inflater.inflate(R.layout.view1, container, false); } }

MyFragmentPagerAdapter數據源

package com.superxingyun.pageradapter;

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

import java.util.List;

/**
 * Created by 月滿軒尼詩 on 2016/8/17.
 */
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    private ListfragmentList;
    private ListtitleList;

    public MyFragmentPagerAdapter(FragmentManager fm, Listfragments, ListtitleList) {
        super(fm);
        this.fragmentList = fragments;
        this.titleList = titleList;
    }

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

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

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

MainActivity:

package com.superxingyun.pageradapter;

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

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

public class MainActivity extends FragmentActivity {

    private ListviewList;
    private ViewPager pager;
    private PagerTabStrip pagerTabStrip;
    private ListtitleList;
    private ListfragmentList;

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

        //初始化
        viewList = new ArrayList<>();
        pager = (ViewPager) findViewById(R.id.pager);

        //將視圖轉化為對象
        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();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());

        //為ViewPager頁卡設置標題
        titleList = new ArrayList();
        titleList.add("第一頁");
        titleList.add("第二頁");
        titleList.add("第三頁");
        titleList.add("第四頁");

        //為PagerTabStrip設置屬性
        pagerTabStrip = (PagerTabStrip) findViewById(R.id.tab);
        pagerTabStrip.setBackgroundColor(Color.MAGENTA);
        pagerTabStrip.setTextColor(Color.WHITE);
        pagerTabStrip.setDrawFullUnderline(false);
        pagerTabStrip.setTabIndicatorColor(Color.GREEN);

        //創建PagerAdapter的適配器
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(viewList, titleList);

        //ViewPager加載適配器
        //pager.setAdapter(myPagerAdapter);

        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList, titleList);
        pager.setAdapter(myFragmentPagerAdapter);
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved