之前用的ViewPager适用于简单的广告切换,但实现页面间的切换最好是用官方推荐的Fragment来处理.

本人力争做到最简单、最有用,是想以后用到的时候能够方便的拿过来复制就能够了.

效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDgyNTIyOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">     

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDgyNTIyOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""> 
 

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDgyNTIyOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

功能:可点击button切换页面,也可滑动界面切换页面.

文件:java文件5个、xml文件4个、.9图片4张

java主类:

<span style="font-size:12px;">package com.example.viewpageractivity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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 { public ViewPager mViewPager;
public Button bt_myclass_studing;
public Button bt_myclass_over; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //调用封装好的actionbar
BuildingActionBar.setActionBarLayout(R.layout.actionbar,
MainActivity.this); bt_myclass_studing = (Button)this.findViewById(R.id.id_myclass_button_studing);
bt_myclass_over = (Button)this.findViewById(R.id.id_myclass_button_over); bt_myclass_studing.setText("正在进行");
bt_myclass_over.setText("已结课"); bt_myclass_studing.setOnClickListener(new myButtonListener());
bt_myclass_over.setOnClickListener(new myButtonListener()); //构建适配器
FragmenAdapter mFragmenAdapter = new FragmenAdapter(this.getSupportFragmentManager()); mViewPager = (ViewPager)this.findViewById(R.id.id_myclass_viewpager); mViewPager.setAdapter(mFragmenAdapter); //加入滑动监听
mViewPager.setOnPageChangeListener(new 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:
setButtonColor(true);
break;
case 1:
setButtonColor(false);
break;
}
}
}); setButtonColor(true);
} /**
* 设置按钮背景色
* 附上色值
* liteblue : #a1dff8
* */
public void setButtonColor(boolean myTouchButton) { if (myTouchButton == true) {
//当点击左边按钮的点击效果
bt_myclass_studing.setBackgroundResource(R.drawable.title_switch_left_on);
bt_myclass_studing.setTextColor(getResources().getColor(R.color.white));
bt_myclass_over.setBackgroundResource(R.drawable.title_switch_right_off);
bt_myclass_over.setTextColor(getResources().getColor(R.color.liteblue));
mViewPager.setCurrentItem(0, true);
} else {
//当点击右边按钮的点击效果
bt_myclass_over.setBackgroundResource(R.drawable.title_switch_right_on);
bt_myclass_over.setTextColor(getResources().getColor(R.color.white));
bt_myclass_studing.setBackgroundResource(R.drawable.title_switch_left_off);
bt_myclass_studing.setTextColor(getResources().getColor(R.color.liteblue));
mViewPager.setCurrentItem(1, true);
}
} /**
* 按钮的监听
* */
class myButtonListener implements View.OnClickListener{ @Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.id_myclass_button_studing:
setButtonColor(true);
break;
case R.id.id_myclass_button_over:
setButtonColor(false);
break;
}
}
} }
</span>

FragmentPagerAdapter适配器类

<span style="font-size:12px;">package com.example.viewpageractivity;

import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; public class FragmenAdapter extends FragmentPagerAdapter{ List<Fragment> mFragment; public FragmenAdapter(FragmentManager fm) {
super(fm); mFragment = new ArrayList<Fragment>(); mFragment.add(new FragmentActivityA()); mFragment.add(new FragmentActivityB()); } @Override
public Fragment getItem(int arg0) {
return mFragment.get(arg0);
} @Override
public int getCount() {
return mFragment.size();
} }
</span>

FragmentActivityA类

<span style="font-size:12px;">package com.example.viewpageractivity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentActivityA extends Fragment{ public View myView; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { //加入布局文件
myView = inflater.inflate(R.layout.fragmenta, container, false); return myView; }
}
</span>

FragmentActivityB类

<span style="font-size:12px;">package com.example.viewpageractivity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentActivityB extends Fragment{ public View myView; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { //加入布局文件
myView = inflater.inflate(R.layout.fragmentb, container, false); return myView;
} }
</span>

之前用到的封装好的Actionbar类

<span style="font-size:12px;">package com.example.viewpageractivity;

import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
/**
* 设置action bar
* */
public class BuildingActionBar {
public static ActionBar mActionbar;
/**
* 设置action bar
* */
public static void setActionBarLayout(int layoutID,Activity mActivity) { mActionbar = mActivity.getActionBar(); if (mActionbar != null) { // 设置home标题隐藏
mActionbar.setDisplayShowHomeEnabled(false); // 使自己定义的普通View能在title栏显示。即actionBar.setCustomView能起作用
mActionbar.setDisplayShowCustomEnabled(true); LayoutInflater inflator = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(layoutID, null); ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); mActionbar.setCustomView(v, layout); } else {
Log.e("BuildingActionBar", "actionbar为空");
}
}
}
</span>

actionbar的xml文件

<span style="font-size:12px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbarLayoutId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1D92F9" > <TextView
android:id="@+id/actionbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#fff"
android:layout_marginLeft="3dip"
android:text="左边"
android:textSize="20sp" /> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true" > <Button
android:id="@+id/id_myclass_button_studing"
android:layout_width="wrap_content"
android:layout_height="48dip"
android:background="@drawable/title_switch_left_off"
android:textColor="#A3A3A3"
android:textSize="15sp" /> <Button
android:id="@+id/id_myclass_button_over"
android:layout_width="wrap_content"
android:layout_height="48dip"
android:layout_toRightOf="@id/id_myclass_button_studing"
android:background="@drawable/title_switch_right_off"
android:textColor="#A3A3A3"
android:textSize="15sp" />
</RelativeLayout> </RelativeLayout></span>

主类的xml文件

<span style="font-size:12px;"><RelativeLayout 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"
tools:context=".MainActivity" > <android.support.v4.view.ViewPager
android:id="@+id/id_myclass_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout></span>

还有两个fragmentActivity 的布局文件可任意定义 就不再贴上来了

ViewPager (下)-- 利用 Fragment 实现美丽的 页面切换的更多相关文章

  1. Windows下利用Chrome调试IOS设备页面

    本文介绍如何在 Windows 系统中连接 iOS设备 并对 Web 页面进行真机调试 必须前提 iOS设备.数据线 Node.js 环境 Chrome 浏览器 环境准备 安装Node环境 参考Nod ...

  2. centos下利用phantomjs来完成网站页面快照截图

    最近研究了下phantomjs,感觉还是非常不错的. 首先到官网下载一个源码包 http://phantomjs.org/download.html 点击源码包下载如图: 然后在linux下将必要的一 ...

  3. 利用pushState开发无刷页面切换

    转载:http://www.cnblogs.com/flash3d/archive/2013/10/23/3384823.html 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与 ...

  4. 利用pushState开发无刷页面切换(转)

    相关文档:https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulatingthebrowser_history 实现目标 ...

  5. Android中通过Fragment进行简单的页面切换

    首先是activity中的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constr ...

  6. Android之怎样实现滑动页面切换【Fragment】

    Fragment 页面切换不能滑动 所以对于listview 能够加入的左右滑动事件 .不会有冲突比如(QQ的好友列表的删除)  Fragment 和viewpager 的差别  Viewpager ...

  7. Qt学习笔记(2)-利用StackWidget实现选项卡式页面

    学习笔记第二篇,利用Qt实现选项卡式的页面,效果如图1.1-图1.3所示.程序实现的功能是通过点击状态栏实现不同页面的切换,实际上Qt中自带有Tab选项卡式的控件,本文利用StackWidge实现类似 ...

  8. ionic-native-transitions调用原生页面切换实现ionic路由切换

    废话不多说:ionic-native-transitions调用原生页面切换实现ionic路由切换,从而大大提升ionic应用的性能. ionic-native-transitions是一个ionic ...

  9. ViewPager和Fragment组合 v4包下的页面切换

    /* *主页面下 */ //-------------主页面下---------------------- package com.example.viewpagerfragment; import ...

随机推荐

  1. js阻止默认事件与js阻止事件冒泡

    e.stopPropagation(); //阻止事件冒泡 功能:停止事件冒泡 function stopBubble(e) { // 如果提供了事件对象,则这是一个非IE浏览器 if ( e &am ...

  2. Select 选择算法 - 编程珠玑(续) 笔记

    Select 算法 I 编程珠玑(续)介绍的 Quickselect 算法 选择 N 个元素中的第 K 小(大)值,是日常场景中常见的问题,也是经典的算法问题. 选取 N 个元素的数组的中的第 K 小 ...

  3. python 多线程学习小记

    python对于thread的管理中有两个函数:join和setDaemon setDaemon:如果在程序中将子线程设置为守护线程,则该子线程会在主线程结束时自动退出,设置方式为thread.set ...

  4. mysql源码安装(包括5.5和5.7)

    1.mysql5.5源码安装 yum install -y cmake ncurses-devel ncurses cd /usr/src wget -c https://cdn.mysql.com/ ...

  5. Python的主要库

    本文在Creative Commons许可证下发布 市面上的分析工具大致分为两大类,菜单式的工具和命令行式的工具.前者适合于初学入门,类似于跟团旅游,提供了固定的路线.分析套路比较固定化,点几下鼠标就 ...

  6. 紫书 例题 9-4 UVa 116 ( 字典序递推顺序)

    这道题在递推方式和那个数字三角形有一点相像,很容易推出来 但是这道题要求的是字典序,这里就有一个递推顺序的问题 这里用逆推,顺推会很麻烦,为什么呢? 如果顺推的话,最后一行假设有种情况是最小值,那么你 ...

  7. hdu 5312 Sequence(数学推导——三角形数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)  ...

  8. List与array的相互转换

    1.List->Array 调用List的toArray方法 List<String> list = new ArrayList<String>(); list.add( ...

  9. 用C#调用Lua脚本

    用C#调用Lua脚本 一.引言 学习Redis也有一段时间了,感触还是颇多的,但是自己很清楚,路还很长,还要继续.上一篇文章简要的介绍了如何在Linux环境下安装Lua,并介绍了在Linux环境下如何 ...

  10. Scott Hanselman的问题-2

    .Net程序员面试 每个人都应知道篇 (回答Scott Hanselman的问题)   昨天回答了Scott Hanselman在他清单上关于C#那部分的题目,.Net 程序员面试 C# 语言篇 (回 ...