Android实战简易教程-第三十四枪(基于ViewPager和FragmentPagerAdapter实现滑动通用Tab)
上一段时间写过一篇文章《基于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)的更多相关文章
- Android实战简易教程-第三十九枪(第三方短信验证平台Mob和验证码自己主动填入功能结合实例)
用户注冊或者找回password时通常会用到短信验证功能.这里我们使用第三方的短信平台进行验证实例. 我们用到第三方短信验证平台是Mob,地址为:http://mob.com/ 一.注冊用户.获取SD ...
- Android实战简易教程-第四十枪(窃听风云之短信监听)
近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...
- Android实战简易教程-第四十九枪(两种方式实现网络图片异步加载)
加载图片属于比较耗时的工作,我们需要异步进行加载,异步加载有两种方式:1.通过AsyncTask类进行:2.通过Handler来实现,下面我们就来看一下如何通过这两种方式实现网络图片的异步加载. 一. ...
- Android实战简易教程-第四十五枪(SlideSwitch-好看又有用的开关button)
开关button也是在项目中经经常使用到的控件,github上有开源的项目,我们研究下它的用法: 1.SlideButton.java: /* * Copyright (C) 2015 Quinn C ...
- Android实战简易教程-第六十六枪(server端搭建和server端Json数据交互)
学习Android有一段时间了.对server端有非常深的好奇,决定对server端的实现进行一些研究,这里实现了一个简单的小样例,用于获取server端的json数据,样例非常easy,适合刚開始学 ...
- Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)
接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...
- Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)
接着上两篇文章.我们基于Bmob提供的API实现用户登录功能.总体看一下代码. 1.注冊页面xml: <RelativeLayout xmlns:android="http://sch ...
- Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)
接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...
- Android实战简易教程-第二十八枪(Uri转String型实例)
接上一篇文章.我们能够轻易的获取所选图片的uri,那么我们考虑怎样将获取的uri转换成String型的地址呢? 接下来我们通过实例来研究.布局文件和上篇(二十七枪)一致,我们就不再列出,直接看Main ...
随机推荐
- javascript 关于 this 作用域链
使用 function f() {} 或者 var f = function() {} 来定义的函数,this 是指向 全局对象 var a = { b: 1, c: funct ...
- struts2运行过程(图解)
.................................................................................................... ...
- 一个“”字引发的痛苦经历
(一篇老文章,还有点价值,特意整理了一下.由于涉及客户项目,已经进行了脱敏处理) 1 写在前面的话 虽然这个问题是有解决方案的,但我不建议大家提供给客户,理由见此. 2 问题描述 2010.10.12 ...
- (11.13)Java小知识!
今天想要与大家分享一下有关于构造方法的知识! 构造方法的定义与作用 构造方法是一种特殊类型的方法.当一个对象被创建的时候,构造方法用来初始化对象,也就是说构造方法其实是一个名词而不是动词,像我刚刚开始 ...
- Velocity(1)——初步入门
1.变量 (1)变量的定义: 1 #set($name = "hello") 说明:velocity中变量是弱类型的. 2 3 当使用#set 指令时,括在双引号中的字面字符串将解 ...
- maven---settings.xml配置
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
- C#PreviewKeyDown 与KeyDown 区别
PreviewKeyDown:在焦点位于此控件上的情况下,当有按键动作时发生(在 KeyDown 事件之前发生). 小注: 某些按键,比如 Tab.Return.Esc 和箭头键,通常会被某些控件忽略 ...
- C语言的第一个程序 “hello world!”
1,C语言的简介 C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. ...
- 最全的iOS数据存储方法
目的 项目准备运用的Core Data进行本地数据存储,本来打算只写一下Core Data的,不过既然说到了数据存储,干脆来个数据存储基础大总结!本文将对以下几个模块进行叙述. 沙盒 Plist Pr ...
- work 2013-07-19
今天,在现场进行了数据库的优化,将数据库的日志截断和压缩了 use 测试库backup log 测试库 with no_logdbcc shrinkfile (测试库_Data,1)dbcc shri ...