fragment与viewPager的结合

通过fragment与viewPager实现如下的效果(可以手指滑动切换界面,也可以点击按钮切换界面):
这里写图片描述

1、创建好项目后,再创建四个继承了Fragment的子类,OneFragment、TwoFragment、ThreeFragment、FourFragment,及其对应的layout布局文件,因代码差不多,这里只给出其中一个:
OneFragment 的代码:

package com.lu.fragmeng;

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

public class OneFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        return inflater.inflate(R.layout.one_fragment, null);
    }

}

OneFragment 的布局文件one_fragment:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f00"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textSize="28sp" 
        android:layout_gravity="center_vertical|center_horizontal"
        />

</FrameLayout>

MainActivity继承的是FragmengActivity,代码如下:

package com.lu.fragmeng;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;

public class MainActivity extends FragmentActivity
{
    OneFragment oneFragment;
    TwoFragment twoFragment;
    ThreeFragment threeFragment;
    FourFragment fourFragment;

    Button btn1, btn2, btn3, btn4;
    ViewPager viewPager;
    ArrayList<Fragment> fragmentList;       //集合用于存放四个Fragment对象;
    OnPageChangeListener pageChangeListener;//viewPager的事件监听;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadUI();
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(0);
        changeItem();
        viewPager.setOnPageChangeListener(pageChangeListener);//viewPager的事件监听;
    }

    private void loadUI()
    {
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        viewPager = (ViewPager)findViewById(R.id.viewPager);

        fragmentList = new ArrayList<Fragment>();
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();

        fragmentList.add(oneFragment);
        fragmentList.add(twoFragment);
        fragmentList.add(threeFragment);
        fragmentList.add(fourFragment);
    }

    //viewPager的事件监听;
    private void changeItem()
    {
        pageChangeListener = new OnPageChangeListener() 
        {           
            @Override
            public void onPageSelected(int position) 
            {
                viewPager.setCurrentItem(position);
            }

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

            @Override
            public void onPageScrollStateChanged(int arg0) 
            {               
            }
        };
    }

    public void changeView(View view)
    {
        switch(view.getId())
        {
        case R.id.btn1:
            viewPager.setCurrentItem(0);
            break;
        case R.id.btn2:
            viewPager.setCurrentItem(1);
            break;
        case R.id.btn3:
            viewPager.setCurrentItem(2);
            break;
        case R.id.btn4:
            viewPager.setCurrentItem(3);
            break;
        }
    }

    class MyAdapter extends FragmentPagerAdapter
    {

        public MyAdapter(FragmentManager fm) 
        {
            super(fm);
        }

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

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

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="1" 
            android:onClick="changeView"
            />
        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="2" 
            android:onClick="changeView"
            />
        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="3" 
            android:onClick="changeView"
            />
        <Button
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="4" 
            android:onClick="changeView"
            />

    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0"
        />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值