/*

*主页面下

*/

//-------------主页面下----------------------

package com.example.viewpagerfragment;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

//注意继承的是FragmentActivity在这里用的是android.support.v4.app.Fragment包下的Fragment

public class MainActivity extends FragmentActivity implements OnClickListener {

private ViewPager vp;
    private TextView tv_textview1;
    private TextView tv_textview2;
    private TextView tv_textview3;
    private TextView tv_textview4;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //找到控件ViewPager
        vp = (ViewPager) findViewById(R.id.vp);
        tv_textview1 = (TextView) findViewById(R.id.tv_textview1);
        tv_textview2 = (TextView) findViewById(R.id.tv_textview2);
        tv_textview3 = (TextView) findViewById(R.id.tv_textview3);
        tv_textview4 = (TextView) findViewById(R.id.tv_textview4);
        //设置ViewPager的适配器
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            
            @Override
            public int getCount() {
                // 返回fragment页数
                return 4;
            }
            
            @Override
            public Fragment getItem(int arg0) {
                //创建fragment
                Fragment fragment=null;
                switch (arg0) {
                case 0:
                    fragment=new Fragment1();
                    break;
                case 1:
                    fragment=new Fragment2();
                    break;
                case 2:
                    fragment=new Fragment3();
                    break;
                case 3:
                    fragment=new Fragment4();
                    break;

default:
                    break;
                }
                //返回fragment
                return fragment;
            }
        });
        //设置ViewPager的滑动监听     并实现3个方法
        vp.setOnPageChangeListener(new OnPageChangeListener() {
            
            @Override
            public void onPageSelected(int arg0) {
                switch (arg0) {
                case 0:
                    //当fragment为第一张页面时,textview设置为红色背景,其他设置为白色背景
                    tv_textview1.setBackgroundColor(Color.RED);
                    tv_textview2.setBackgroundColor(Color.WHITE);
                    tv_textview3.setBackgroundColor(Color.WHITE);
                    tv_textview4.setBackgroundColor(Color.WHITE);
                    break;
                case 1:
                    tv_textview1.setBackgroundColor(Color.WHITE);
                    tv_textview2.setBackgroundColor(Color.RED);
                    tv_textview3.setBackgroundColor(Color.WHITE);
                    tv_textview4.setBackgroundColor(Color.WHITE);
                    break;
                case 2:
                    tv_textview1.setBackgroundColor(Color.WHITE);
                    tv_textview2.setBackgroundColor(Color.WHITE);
                    tv_textview3.setBackgroundColor(Color.RED);
                    tv_textview4.setBackgroundColor(Color.WHITE);
                    break;
                case 3:
                    tv_textview1.setBackgroundColor(Color.WHITE);
                    tv_textview2.setBackgroundColor(Color.WHITE);
                    tv_textview3.setBackgroundColor(Color.WHITE);
                    tv_textview4.setBackgroundColor(Color.RED);
                    break;

default:
                    break;
                }
                
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                
                
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0) {
                
                
            }
        });
        //设置textView的监听
        tv_textview1.setOnClickListener(this);
        tv_textview2.setOnClickListener(this);
        tv_textview3.setOnClickListener(this);
        tv_textview4.setOnClickListener(this);
        
        //设置当进入activity时的默认页面
        tv_textview1.setBackgroundColor(Color.RED);
        vp.setCurrentItem(0);
    }

@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;
    }

//实现textview的监听的方法
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tv_textview1:
            //viewpager设置为第一fragment
            vp.setCurrentItem(0);
            break;
        case R.id.tv_textview2:
            vp.setCurrentItem(1);
            break;
        case R.id.tv_textview3:
            vp.setCurrentItem(2);
            break;
        case R.id.tv_textview4:
            vp.setCurrentItem(3);
            break;

default:
            break;
        }
        
    }
    
}
//--------------------main.xml布局文件--------------------------

<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" >

<android.support.v4.view.ViewPager
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/vp"></android.support.v4.view.ViewPager>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
        <TextView android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="40dp"
        android:id="@+id/tv_textview1"
        android:text="首页"
        android:gravity="center"/>
        <TextView android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="40dp"
        android:id="@+id/tv_textview2"
        android:text="首页1"
        android:gravity="center"/>
        <TextView android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="40dp"
        android:id="@+id/tv_textview3"
        android:text="首页2"
        android:gravity="center"/>
        <TextView android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="40dp"
        android:id="@+id/tv_textview4"
        android:text="首页3"
        android:gravity="center"/>
        
    </LinearLayout>

</LinearLayout>

//-----------------------创建-Fragment1---------------------

package com.example.viewpagerfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
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) {
        View view=inflater.inflate(R.layout.frgment_item1, null);
        return view;
    }

}
//-----------------创建-------Fragment2-----------------------------

package com.example.viewpagerfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
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) {
        View view=inflater.inflate(R.layout.frgment_item2, null);
        return view;
    }

}

//----------------创建------Fragment3---------------------

package com.example.viewpagerfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
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) {
        View view=inflater.inflate(R.layout.frgment_item3, null);
        return view;
    }

}

//-----------------创建-----Fragment4----------------------

package com.example.viewpagerfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
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) {
        View view=inflater.inflate(R.layout.frgment_item4, null);
        return view;
    }

}

//--------------创建布局文件----frgment_item1.xml-------------------

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#00ffff"
        android:text="1"
        android:gravity="center"
        />

</LinearLayout>

//-----------------创建 布局文件  frgment_item2.xml  -------------------------

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff00ff"
        android:text="2"
        android:gravity="center"
        />

</LinearLayout>

//------------创建布局文件  frgment_item2.xml  --------------------------------

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff00"
        android:text="3"
        android:gravity="center"
        />

</LinearLayout>

//--------------------创建布局文件    frgment_item4.xml   -----------------------------------------

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff0000"
        android:text="4"
        android:gravity="center"
        />

</LinearLayout>

//--------------完了-----------------------

ViewPager和Fragment组合 v4包下的页面切换的更多相关文章

  1. app包中的fragment和v4包中的fragment的使用的区别

    app包中的fragment和v4包中的fragment的使用的区别 1.尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的 2.androi ...

  2. 14 Fragment的V4包的使用

    activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  3. FragmentManager V4包下 应该用FragmentActivity

    import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity ...

  4. 微信小程序自定义下导航页面切换效果的合理写法

    上图::: 导航模板内容页面的定义: <template name="naviBot">   <view class='navwrap t_cen font_26 ...

  5. Fragment用app包还是v4包解析

    转自:http://blog.csdn.net/zc0908/article/details/50721553 1)问题简述 相信很多的朋友在调用Fragment都会遇到下面的情况: 这个时候问题来了 ...

  6. Android做法说明(3)---Fragment使用app袋或v4包解析

    Android做法说明(3)---Fragment使用app袋或v4包解析 1)问题简述 相信非常多的朋友在调用Fragment都会遇到以下的情况: watermark/2/text/aHR0cDov ...

  7. Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件

    SwipeRefreshLayout在v4包下,相应的v4Demo中也有相应的样例.假设没有请下载最新support-v4 SwipeRefreshLayout 仅仅能有一个直接子View,可能是一个 ...

  8. 关于Android studio下V4包 KeyEventCompat 类找不到问题

    V4包 KeyEventCompat 类找不到问题   本文链接:https://blog.csdn.net/shanshan_1117/article/details/84344557 今天我把su ...

  9. 关于app.FragmentManager和v4包的FragmentPagerAdapter冲突

    这几天发现一个问题我用getFragmentManager()得到FragmentManager不能放到FragmentPagerAdapter里面去.由于FragmentPagerAdapter里面 ...

随机推荐

  1. Storm 分配逻辑

    ps:都是学习的别人的博客,只是做了个整理所有就写成了原创,其实都是人家的东西 当一个topology在storm cluster中运行时,它的并发主要跟3个逻辑对象相关:worker,executo ...

  2. 判断手机电脑微信 js

    if ((navigator.userAgent.match(/(MicroMessenger)/i))) { //微信浏览器 //location.href=""; } else ...

  3. 洛谷-哥德巴赫猜想(升级版)-BOSS战-入门综合练习1

    题目背景 Background 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约数的数,如2和 ...

  4. linux 非root用户 ssh 免密码登录

    之所以要把这个记录下来 是因为它的确和root用户不一样root用户 不需要改动什么权限问题  只要生成私钥/公钥对 即可 但是一样的操作在普通用户上就出了问题了 折腾了老半天 ssh-keygen ...

  5. KNN算法的补充

    文本自动分类技术是文字管理的基础.通过快速.准确的文本自动分类,可以节省大量的人力财力:提高工作效率:让用户快速获得所需资源,改善用户体验.本文着重对KNN文本分类算法进行介绍并提出改进方法. 一.相 ...

  6. iOS拨打电话

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  7. Windows进程间通信(中)

    二.文件映射 文件映射(Memory-Mapped Files)能使进程把文件内容当作进程地址区间一块内存那样来对待.因此,进程不必使用文件I/O操作,只需简单的指针操作就可读取和修改文件的内容. W ...

  8. qtp中vb脚本,经典收藏

    1.在脚本运行过程中屏蔽鼠标键盘输入 SystemUtil.BlockInput ‘开始处 这里是你的脚本 SystemUtil.UnblockInput ’结尾处 ----------------- ...

  9. hdu_4718_The LCIS on the Tree(树链剖分+线段树合并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4718 题意:给你一棵树,每个节点有一个值,然后任给树上的两点,问这两点的最长连续递增区间是多少 题解: ...

  10. hdu_4539_郑厂长系列故事——排兵布阵(状压DP|最大团)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 题意:中文,不解释 题解:将每一行的状态压缩,然后进行DP,也可以用最大团做.这里我用的DP # ...