上一段时间写过一篇文章《基于ViewPager实现微信页面切换效果

里面实现了相似微信Tab的页面。可是这样的实现方法有个问题。就是以后全部的代码逻辑都必须在MainActivity中实现。这样就造成MainActivity文件很臃肿,不利于代码管理。

以下我们基于ViewPager和FragmentPagerAdapter实现滑动通用Tab。

布局文件基本和上篇文章一致。

1.top.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="45dp"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="寻 领"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" /> </LinearLayout>

2.bottom.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="55dp"
android:background="#ffffff"
android:orientation="horizontal" > <LinearLayout
android:id="@+id/id_tab_lost"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" > <ImageButton
android:id="@+id/id_tab_lost_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:clickable="false"
android:src="@drawable/lost" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="寻物"
android:textColor="#000000" />
</LinearLayout> <LinearLayout
android:id="@+id/id_tab_found"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" > <ImageButton
android:id="@+id/id_tab_found_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:clickable="false"
android:src="@drawable/found" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="认领"
android:textColor="#000000" />
</LinearLayout> <LinearLayout
android:id="@+id/id_tab_publish"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" > <ImageButton
android:id="@+id/id_tab_publish_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:clickable="false"
android:src="@drawable/tab_address_normal" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公布"
android:textColor="#000000" />
</LinearLayout> <LinearLayout
android:id="@+id/id_tab_settings"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" > <ImageButton
android:id="@+id/id_tab_settings_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:clickable="false"
android:src="@drawable/tab_settings_normal" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
android:textColor="#000000" />
</LinearLayout> </LinearLayout>

3.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" > <include layout="@layout/top" /> <android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager> <include layout="@layout/bottom" /> </LinearLayout>

4.tab01.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:gravity="center"
android:text="This is Lost Tab"
android:textSize="30sp"
android:textStyle="bold" /> </LinearLayout>

tab02.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:gravity="center"
android:text="This is Found Tab"
android:textSize="30sp"
android:textStyle="bold" /> </LinearLayout>

tab03.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:gravity="center"
android:text="This is Publish Tab"
android:textSize="30sp"
android:textStyle="bold" /> </LinearLayout>

tab04.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:gravity="center"
android:text="This is Settings Tab"
android:textSize="30sp"
android:textStyle="bold" /> </LinearLayout>

5.MainActivity.java:

package com.lostandfound.activity;

import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
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.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout; public class MainActivity extends FragmentActivity implements OnClickListener
{
private ViewPager mViewPager;
private FragmentPagerAdapter mAdapter;
private List<Fragment> mFragments; private LinearLayout mTabLost;
private LinearLayout mTabFound;
private LinearLayout mTabPublish;
private LinearLayout mTabSettings; private ImageButton mImgLost;
private ImageButton mImgFound;
private ImageButton mImgPublish;
private ImageButton mImgSettings; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); initView();
initEvent(); setSelect(1);
} private void initEvent()
{
mTabLost.setOnClickListener(this);
mTabFound.setOnClickListener(this);
mTabPublish.setOnClickListener(this);
mTabSettings.setOnClickListener(this);
} private void initView()
{
mViewPager = (ViewPager) findViewById(R.id.id_viewpager); mTabLost = (LinearLayout) findViewById(R.id.id_tab_lost);
mTabFound = (LinearLayout) findViewById(R.id.id_tab_found);
mTabPublish = (LinearLayout) findViewById(R.id.id_tab_publish);
mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings); mImgLost = (ImageButton) findViewById(R.id.id_tab_lost_img);
mImgFound = (ImageButton) findViewById(R.id.id_tab_found_img);
mImgPublish = (ImageButton) findViewById(R.id.id_tab_publish_img);
mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img); mFragments = new ArrayList<Fragment>();
Fragment mTab01 = new LostFragment();
Fragment mTab02 = new FoundFragment();
Fragment mTab03 = new PublishFragment();
Fragment mTab04 = new SettingFragment();
mFragments.add(mTab01);
mFragments.add(mTab02);
mFragments.add(mTab03);
mFragments.add(mTab04); mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
{ @Override
public int getCount()
{
return mFragments.size();
} @Override
public Fragment getItem(int arg0)
{
return mFragments.get(arg0);
}
};
mViewPager.setAdapter(mAdapter); mViewPager.setOnPageChangeListener(new OnPageChangeListener()
{ @Override
public void onPageSelected(int arg0)
{
int currentItem = mViewPager.getCurrentItem();
setTab(currentItem);
} @Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub } @Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub }
});
} @Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.id_tab_lost:
setSelect(0);
break;
case R.id.id_tab_found:
setSelect(1);
break;
case R.id.id_tab_publish:
setSelect(2);
break;
case R.id.id_tab_settings:
setSelect(3);
break; default:
break;
}
} private void setSelect(int i)
{
setTab(i);
mViewPager.setCurrentItem(i);
} private void setTab(int i)
{
resetImgs();
// 设置图片为亮色
// 切换内容区域
switch (i)
{
case 0:
mImgLost.setImageResource(R.drawable.lost);
break;
case 1:
mImgFound.setImageResource(R.drawable.found);
break;
case 2:
mImgPublish.setImageResource(R.drawable.tab_address_pressed);
break;
case 3:
mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
break;
}
} /**
* 切换图片至暗色
*/
private void resetImgs()
{
mImgLost.setImageResource(R.drawable.lost);
mImgFound.setImageResource(R.drawable.found);
mImgPublish.setImageResource(R.drawable.tab_address_normal);
mImgSettings.setImageResource(R.drawable.tab_settings_normal);
} }

6.四个Fragment

LostFragment.java:

package com.lostandfound.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class LostFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.tab01, container, false);
}
}

FoundFragment.java:

package com.lostandfound.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FoundFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.tab02, container, false);
}
}

PublishFragment.java:

package com.lostandfound.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class PublishFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.tab03, container, false);
}
}

SettingFragment.java:

package com.lostandfound.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class SettingFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.tab04, container, false);
}
}

最后执行实例:

能够实现ViewPager似的滑动翻页,同一时候还能够分开编码。

喜欢的朋友关注我!多谢您的支持。

Android实战简易教程-第三十四枪(基于ViewPager和FragmentPagerAdapter实现滑动通用Tab)的更多相关文章

  1. Android实战简易教程-第三十九枪(第三方短信验证平台Mob和验证码自己主动填入功能结合实例)

    用户注冊或者找回password时通常会用到短信验证功能.这里我们使用第三方的短信平台进行验证实例. 我们用到第三方短信验证平台是Mob,地址为:http://mob.com/ 一.注冊用户.获取SD ...

  2. Android实战简易教程-第四十枪(窃听风云之短信监听)

    近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...

  3. Android实战简易教程-第四十九枪(两种方式实现网络图片异步加载)

    加载图片属于比较耗时的工作,我们需要异步进行加载,异步加载有两种方式:1.通过AsyncTask类进行:2.通过Handler来实现,下面我们就来看一下如何通过这两种方式实现网络图片的异步加载. 一. ...

  4. Android实战简易教程-第四十五枪(SlideSwitch-好看又有用的开关button)

    开关button也是在项目中经经常使用到的控件,github上有开源的项目,我们研究下它的用法: 1.SlideButton.java: /* * Copyright (C) 2015 Quinn C ...

  5. Android实战简易教程-第六十六枪(server端搭建和server端Json数据交互)

    学习Android有一段时间了.对server端有非常深的好奇,决定对server端的实现进行一些研究,这里实现了一个简单的小样例,用于获取server端的json数据,样例非常easy,适合刚開始学 ...

  6. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

    接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...

  7. Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)

    接着上两篇文章.我们基于Bmob提供的API实现用户登录功能.总体看一下代码. 1.注冊页面xml: <RelativeLayout xmlns:android="http://sch ...

  8. Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)

    接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...

  9. Android实战简易教程-第二十八枪(Uri转String型实例)

    接上一篇文章.我们能够轻易的获取所选图片的uri,那么我们考虑怎样将获取的uri转换成String型的地址呢? 接下来我们通过实例来研究.布局文件和上篇(二十七枪)一致,我们就不再列出,直接看Main ...

随机推荐

  1. iOS 极光推送的集成以及一些集成后的狗血

    1.首先进入极光文档下载激光推送的SDk---传送门http://docs.jiguang.cn/jpush/client/iOS/ios_sdk/   将解压后的lib子文件夹(包含JPUSHSer ...

  2. 微信小程序入门(一)

    想必当你对官方文档了解地差不多的时候,一颗跃跃欲试的心就开始骚动了吧. 开发小程序之前的准备工作: 1).准备一个域名 2).准备一台云服务器 3).搭建小程序的后台,博主的小程序后台请求的的是自己写 ...

  3. #UnityTips# 2017.11.14

    hi,all.最近比较忙,所以更新也比较慢了. 今天就来和大家分享一个小Tip,它是关于UGUI的坑的. 使用过UGUI的朋友们都知道,Canvas的渲染方式有三种: Screen Space Ove ...

  4. Java多线程由易到难

    线程可以驱动任务,因此你需要一种描述任务的方式,这可以由Runnable接口来提供.要想定义任务,只需实现Runnable接口并编写run方法,使得该任务可以执行你的命令. public class ...

  5. RabbitMQ的简单应用

    虽然后台使用了读写分离技术,能够在一定程度上抗击高并发,但是如果并发量特别巨大时,主数据库不能同时处理高并发的请求,这时数据库容易宕机. 问题: 现在的问题是如何既能保证数据库正常运行,又能实现用户数 ...

  6. SSM :MyBatis与Spring的整合

    MyBatis与Spring的整合 一:Spring整合MyBatis的准备工作: (1.)在项目中加入Spring,ByBatis及整合相关的jar文件 (2.)建立开发目录结构,创建实体类 (3. ...

  7. [转载] Java学习之Hessian通信基础

    转载自http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.html 一.首先先说Hessian是什么?    Hessian:hessian是一个轻量级的r ...

  8. c++/cmake /Android NDK 动态链接库交叉编译笔记

    项目使用cmake管理,由于项目的需要,核心代码要求跨 Linux/Windows/Android 三平台.Windows和Linux都好说,但Android NDK费了一番功夫还是没有解决.临时的解 ...

  9. Python和Excel交互

    Python和Excel交互 使用的python包为XlsxWriter 下载的链接 https://pypi.python.org/pypi/XlsxWriter 初级的例子: def write_ ...

  10. 【JDK1.8】JDK1.8集合源码阅读——TreeMap(一)

    一.前言 在前面两篇随笔中,我们提到过,当HashMap的桶过大的时候,会自动将链表转化成红黑树结构,当时一笔带过,因为我们将留在本章中,针对TreeMap进行详细的了解. 二.TreeMap的继承关 ...