1. 程式人生 > >Android Fragment和ViewPager聯用

Android Fragment和ViewPager聯用

   VPandFMActivity.java
package com.example.administrator.main;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.adapter.TabFragmentPagerAdapter;
import com.example.administrator.fragment.FaJianFragment;
import com.example.administrator.fragment.ShouJianFragment;

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

    public class VPandFMActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView back;
    private TextView title;
    private TextView shoujian;
    private TextView fajian;
    private ViewPager myViewPager;
    private List<Fragment> list;
    private TabFragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 1、一般情況下去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //2、Activity繼承AppCompatActivity去掉標題欄
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_vpand_fm);
        initView();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        shoujian.setOnClickListener(this);
        fajian.setOnClickListener(this);
        myViewPager.setOnPageChangeListener(new MyPagerChangeListener());

        //把Fragment新增到List集合裡面
        list = new ArrayList<>();
        list.add(new ShouJianFragment());
        list.add(new FaJianFragment());
        adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list);
        myViewPager.setAdapter(adapter);
        myViewPager.setCurrentItem(0);  //初始化顯示第一個頁面

    }

    private void initView(){
        back = findViewById(R.id.back);
        title = findViewById(R.id.txt_title);
        shoujian = findViewById(R.id.shoujian);
        fajian = findViewById(R.id.fajian);
        myViewPager = findViewById(R.id.myViewPager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shoujian:
                myViewPager.setCurrentItem(0);
                shoujian.setTextColor(getResources().getColor(R.color.colorPrimary));
                fajian.setTextColor(getResources().getColor(R.color.black));
                break;
            case R.id.fajian:
                myViewPager.setCurrentItem(1);
                shoujian.setTextColor(getResources().getColor(R.color.black));
                fajian.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
        }
    }

    /**
     * 設定一個ViewPager的偵聽事件,當左右滑動ViewPager時選單欄被選中狀態跟著改變
     *
     */
    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    shoujian.setTextColor(getResources().getColor(R.color.colorPrimary));
                    fajian.setTextColor(getResources().getColor(R.color.black));
                    break;
                case 1:
                    shoujian.setTextColor(getResources().getColor(R.color.black));
                    fajian.setTextColor(getResources().getColor(R.color.colorPrimary));
                    break;
            }
        }
    }

}
activity_vpand_fm.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.main.VPandFMActivity">
    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#2D65B1"
        android:layout_alignParentTop="true">
        <ImageView
            android:id="@+id/back"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:src="@mipmap/back"/>
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="收件箱"
            android:layout_centerInParent="true"
            android:textSize="17sp"
            android:textColor="@color/color_white"/>
    </RelativeLayout>
    <LinearLayout
        android:id="@+id/menu"
        android:layout_below="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/shoujian"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="35dp"
                android:text="收件箱"
                android:textColor="@color/colorPrimary"
                android:gravity="center"
                android:textSize="15sp"/>
            <View
                android:layout_width="0.7dp"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:background="#878787"
                android:layout_marginBottom="5dp"/>
            <TextView
                android:id="@+id/fajian"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="35dp"
                android:text="收件箱"
                android:gravity="center"
                android:textSize="15sp"/>
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="0.7dp"
            android:background="#878787"/>
    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_below="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

其中TabFragmentPagerAdapter.java

package com.example.administrator.adapter;

import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
    private FragmentManager mfragmentManager;
    private List<Fragment> mlist;

    public TabFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.mlist = list;
    }

    @Override
    public Fragment getItem(int arg0) {
        return mlist.get(arg0);//顯示第幾個頁面
    }

    @Override
    public int getCount() {
        return mlist.size();//有幾個頁面
    }
}

至於Fragment是怎麼實現的就不再多說了。