Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> ViewPager,viewpagerfragment

ViewPager,viewpagerfragment

編輯:關於android開發

ViewPager,viewpagerfragment


package com.example.viewpagertest;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

public class MainActivity extends Activity {
    private List<View> list;
    private ViewPager pager;
    private List<String> titList;
    private PagerTabStrip tabStrip;// 屬於Pager的一個子標簽

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        list = 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);

        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(view4);
        // 為頁卡設置標題
        titList = new ArrayList<String>();
        titList.add("第一頁");
        titList.add("第二頁");
        titList.add("第三頁");
        titList.add("第四頁");
        // 為PagerTabStrip設置一些屬性
        tabStrip = (PagerTabStrip) findViewById(R.id.tab);
        tabStrip.setTextColor(Color.BLACK);
        tabStrip.setBackgroundResource(R.drawable.ic_biaoti);

        tabStrip.setDrawFullUnderline(true);// 沒有下劃線
        tabStrip.setTabIndicatorColor(Color.GRAY);// 粗線的顏色
        // 初始化ViewPager
        pager = (ViewPager) findViewById(R.id.pager);

        // 創建PagerAdapter的適配器
        MyPagerAdapter adapter = new MyPagerAdapter(list, titList);
        // viewPager加載適配器
        pager.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
package com.example.viewpagertest;

import java.util.List;
import java.util.zip.Inflater;

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

public class MyPagerAdapter extends PagerAdapter {
    private List<View> list;
    private List<String> titList;

    public MyPagerAdapter(List<View> list, List<String> titList) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.titList = titList;
    }

    /**
     * 返回的是頁卡的數量
     * 
     * **/
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();// 頁卡的數量等於集合的大小
    }

    /**
     * View對象是否來自於對象
     */
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    /**
     * 實例化一個頁卡
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        container.addView(list.get(position));
        return list.get(position);
    }

    /**
     * 銷毀一個頁卡
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        container.removeView(list.get(position));

    }
    /**設置頁卡的標題
     * 在添加標題之後加一個getPagerTitle方法
     * */
    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return titList.get(position);
    }
}
package com.example.viewpagertest;

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

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.view1, container, false);
    }

}
package com.example.viewpagertest;

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

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.view2, container, false);
    }

}
package com.example.viewpagertest;

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

public class Fragment3 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.view3, container, false);
    }

}
package com.example.viewpagertest;

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

public class Fragment4 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.view4, container, false);
    }

}
<?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.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_gravity="top" >
        </android.support.v4.view.PagerTabStrip>
    </android.support.v4.view.ViewPager>

</LinearLayout>
<?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" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一個頁面" />

</LinearLayout>
<?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" >

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="第三個頁面" />/>

</LinearLayout>
<?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" >

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="第四個頁面" />/>

</LinearLayout>

 感悟:ViewPager即模仿微信可以左右滑屏。。。  在src下建立4個Fragment ,分別標識Fragment 1,Fragment 2,Fragment 3,Fragment 4.並在AndroidManifest中申明且分別建立4個布局文件View1,2 3 4.並引入到對應的Fragment中 ,注意要繼承Fragment不是Activity且實現onCreatView方法,return inflater.inflate(int resource, ViewGroup root, boolean attachToRoot);  然後在main.xml中加入

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

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

分別定義

private ViewPager viewPager;
private List<View> list;
private List<String> titlelist;布局別忘記要R.layout.main

先使view1 2 3 4 布局文件轉換成View對象View.inflate(Context context, int resource, ViewGroup root)

,titlelist集合實例化

然後把view 1 2 3 4加入到list集合中

 MyPagerAdapter要繼承PagerAdapter,繼承構造方法並添加沒有的構造方法,並把創建構造函數,添加List<View> list, List<String> titlelist 2 個參數

@Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        container.addView(list.get(position));
        return list.get(position);
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        container.removeView(list.get(position));
        
    }
//這裡是destroyItem方法

 

然後在MAinActivity中

private ViewPager viewPager; 是ViewPager。。。。然後在MyPagerAdapter中添加

@Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return titlelist.get(position);
    }

 

最後就是設置PagerTabStrip的一些參數

        tabStrip = (PagerTabStrip) findViewById(R.id.tab);
        tabStrip.setTextColor(Color.BLACK);
        tabStrip.setBackgroundResource(R.drawable.ic_biaoti);//背景圖片

        tabStrip.setDrawFullUnderline(true);// 沒有下劃線
        tabStrip.setTabIndicatorColor(Color.GRAY);// 粗線的顏色

 

OK。

 

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