新知识,新摘要:

效果图:framgent导入包都是v4包下,谨慎导入错误!

首先设置viewPager布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager1221"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom">

    </android.support.v4.view.ViewPager>
    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
       android:layout_alignParentBottom="true"
        android:background="@color/colorPurple"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/you"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="你好"
            android:textSize="20sp"
            android:gravity="center"
            />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#FFFFFF"></View>
        <TextView
            android:id="@+id/me"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="我好"
            android:textSize="20sp"
            android:gravity="center"
            />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#FFFFFF"></View>
        <TextView
            android:id="@+id/hei"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="大家好"
            android:textSize="20sp"
            android:gravity="center"
            />

    </LinearLayout>
</RelativeLayout>

其次设置子布局framgent:设置三个页面例如页面一

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="50dp"
         android:text="第一个页面"
        android:textSize="30sp"
        android:gravity="center"
        android:background="@color/colorPurple"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/first"
        android:src="@mipmap/cute"/>

</RelativeLayout>

然后分别设置三个页面的framgent类继承Fragment类,列如第一页面:

package com.example.administrator.test_1216.framgent;

import com.example.administrator.test_1216.R;

public class OneFramgent1221 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 添加布局
        View v=inflater.inflate(R.layout.oneframgent1221_item,null);
        return v;
    }
}
其次设置adapter继承

package com.example.administrator.test_1216.adapter;

public class FramgentPagerAdapter1221 extends FragmentPagerAdapter{

    //声明集合
  private List<Fragment> list;
    public FramgentPagerAdapter1221(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list = list;
    }

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

    @Override
    public int getCount() {
        return list.size();
    }
}
最后设置activity:代码思路:a、声明控件,绑定id,首先实现文字控件的颜色改变和监听;

              b、声明viewPager控件,绑定id,声明集合,将framgent类型的页面加入到list集合中

              c、初始化适配器,加载适配器;

              d、为viewPager设置监听,页面滚动切换页面时候文字发生颜色发生改变;

代码如下:

package com.example.administrator.test_1216;

public class FramgentPagerActivity1221 extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    //声明控件
    private TextView you;
    private TextView me;
    private TextView hei;
    private int currentColor;
    private  int otherColor;
    private ViewPager viewpager1221;
    private FramgentPagerAdapter1221 adapter1221;
    List<Fragment> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagerviewframgent1221);
        currentColor=getResources().getColor(R.color.currentColor);
        otherColor=getResources().getColor(R.color.otherColor);
        intiview();
        andViewPager();
    }

    private void andViewPager() {
        viewpager1221= (ViewPager) findViewById(R.id.viewpager1221);
        //初始化集合
        list=new ArrayList<Fragment>();
        //添加页面到集合
        list.add(new OneFramgent1221());
        list.add(new TwoFramgent1221());
        list.add(new ThreeFramgent1221());
        //初始化适配器
        adapter1221=new FramgentPagerAdapter1221(getSupportFragmentManager(),list);
        viewpager1221.setAdapter(adapter1221);
        //设置每次启动软件时的显示页面
        viewpager1221.setCurrentItem(0);
        //设置viewpager的监听
        viewpager1221.setOnPageChangeListener(this);
    }

    private void intiview() {
        //绑定id
        you= (TextView) findViewById(R.id.you);
        me= (TextView) findViewById(R.id.me);
        hei= (TextView) findViewById(R.id.hei);
        //添加初始默认颜色
        you.setTextColor(currentColor);
        me.setTextColor(otherColor);
        hei.setTextColor(otherColor);
        //设置监听
        you.setOnClickListener(this);
        me.setOnClickListener(this);
        hei.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //声明下标
        int index=0;
        switch (v.getId()){
            case R.id.you:
                you.setTextColor(currentColor);
                me.setTextColor(otherColor);
                hei.setTextColor(otherColor);
                index=0;
                break;
            case R.id.me:
                you.setTextColor(otherColor);
                me.setTextColor(currentColor);
                hei.setTextColor(otherColor);
                index=1;
                break;
            case R.id.hei:
                you.setTextColor(otherColor);
                me.setTextColor(otherColor);
                hei.setTextColor(currentColor);
                index=2;
                break;
        }
        // 为viewpager设置下标
        viewpager1221.setCurrentItem(index);
    }
//页面切换时候字体的颜色改变
    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                you.setTextColor(currentColor);
                me.setTextColor(otherColor);
                hei.setTextColor(otherColor);
                break;
            case 1:
                you.setTextColor(otherColor);
                me.setTextColor(currentColor);
                hei.setTextColor(otherColor);
                break;
            case 2:
                you.setTextColor(otherColor);
                me.setTextColor(otherColor);
                hei.setTextColor(currentColor);
                break;
        }

    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}

ViewPager+Fragment实现页面的切换的更多相关文章

  1. ViewPager+PagerTabStrip实现页面的切换

    页面切换效果图 首先创建布局: 代码: <?xml version="1.0" encoding="utf-8"?><LinearLayout ...

  2. Android开发之利用ViewPager实现页面的切换(仿微信、QQ)

    这里利用ViewPager实现页面的滑动,下面直接上代码: 1.首先写一个Activity,然后将要滑动的Fragment镶嵌到写好的Activity中. Activity的布局文件:activity ...

  3. Android中使用ViewPager实现屏幕页面切换和页面切换效果

    之前关于如何实现屏幕页面切换,写过一篇博文<Android中使用ViewFlipper实现屏幕切换>,相比ViewFlipper,ViewPager更适用复杂的视图切换,而且Viewpag ...

  4. ViewPager -- Fragment 切换卡顿 性能优化

    当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...

  5. 【原创】【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析   1.使用场景 ViewPager+Fragment实现界面切换,界面数量>=3   2.Fragment生命周期以及与Activ ...

  6. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  7. ViewPager和View组合 实现页面的切换

    //--------------主页面------------------------------- package com.bw.test; import java.util.ArrayList;i ...

  8. Android - FragmentTabHost 与 Fragment 制作页面切换效果

    使用 FragmentTabHost 与 Fragment 制作页面切换效果 API 19 TabHost已经不建议使用了.用 FragmentTabHost 来代替TabHost.实际上 Fragm ...

  9. Android - TabHost 与 Fragment 制作页面切换效果

    Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...

随机推荐

  1. recyleView使用笔记

    直接上代码: package com.test.recycleview; import android.app.Activity; import android.graphics.Canvas; im ...

  2. SQL Server 树形表非循环递归查询

    很多人可能想要查询整个树形表关联的内容都会通过循环递归来查...事实上在微软在SQL2005或以上版本就能用别的语法进行查询,下面是示例.   --通过子节点查询父节点WITH  TREE AS(  ...

  3. mac 设置 git 和github 告别命令行

    针对和我一样的新手,大虾们请轻拍. 很多小伙伴都想用git管理自己的代码,或者想在github上上传自己的项目.在网上找了几篇这方面的文章,都是用命令行设置的. 用命令行管理和安装太坑爹,这里有一个简 ...

  4. C#小程序呢飞行棋设计分析

    C#小程序飞行棋,程序效果图 1.设计分析 这个程序界面大致分为四部分: ① 最上面游戏名字界面 ②信息提示区 ③游戏界面区 ④游戏操作提示区 2.分区设计实现 一.游戏界面显示区,由于只需要显示出图 ...

  5. Web前端工程师

    前端开发,不仅仅是需要会写页面而已,还需要具备很多技能,现做如下总结: 会点设计,不要求精湛,处理图片,设计个小广告是要的: 精通HTML+CSS,并能快速处理各浏览器兼容问题: 熟练掌握Javasc ...

  6. Smarty模本引擎

    封装一个自定义Smarty引擎 Smart模板注释 基本语法:{* 注释内容 *} Smarty模板中的变量 简单变量 四种标量类型:整型.浮点型.布尔型和字符串型! 数组变量 可以给模板分配一个数组 ...

  7. Dynamic V Strongly Typed Views

    Come From https://blogs.msdn.microsoft.com/rickandy/2011/01/28/dynamic-v-strongly-typed-views/ There ...

  8. 样式:让div里的两个控件在一行的操作

    table的td里如果放一个text,希望在右侧再放一个按钮,让这两个控件在一行,但是放了之后总是底部不能对齐,这样的话,加上下边这句样式就可以了 position:relative; top:17p ...

  9. 引用对象的使用和易产生bug的示例

    本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5481608.html  (Robin) QuoteTest(引用对象技巧) import ja ...

  10. xampp 配置虚拟主机

    1.安装好xampp后 2.找到安装目录 apache目录--conf目录--extra目录--httpd-vhosts.conf文件(用记事本或者别的编辑器打开) 文件最后添加代码 <Virt ...