第一步 : 布局文件 activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#eee"
  6. android:orientation="vertical" >
  7. <include layout="@layout/top1" /> // 顶部的菜单
  8. <include layout="@layout/top2" /> // 顶部的聊天 , 发现 ,通讯录
  9.  
  10. <android.support.v4.view.ViewPager
  11. android:id="@+id/id_viewpager"
  12. android:layout_width="fill_parent"
  13. android:layout_height="0dp"
  14. android:layout_weight="1" >
  15. </android.support.v4.view.ViewPager>
  16.  
  17. </LinearLayout>

layout 文件下的top1

  1. <?xml version="1.0" encoding="utf-8"?> // top1
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6.  
  7. <RelativeLayout
  8.      android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:background="@drawable/abc_ab_bottom_solid_dark_holo" //头部的黑背景
  11. android:padding="12dip" >
  12.  
  13. <LinearLayout
  14.        android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentLeft="true"
  17. android:gravity="center"
  18. android:orientation="horizontal" >
  19.  
  20. <ImageView
  21. android:layout_width="30dp"
  22. android:layout_height="30dp"
  23. android:src="@drawable/actionbar_icon" />
  24.  
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_marginLeft="10dip"
  29. android:text="微信"
  30. android:textColor="@color/lightgray"
  31. android:textSize="18dp" />
  32. </LinearLayout>
  33.  
  34. <LinearLayout
  35.      android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_alignParentRight="true" //居右
  38. android:gravity="center"
  39. android:orientation="horizontal" >
  40.  
  41. <ImageView
  42. android:layout_width="30dp"
  43. android:layout_height="30dp"
  44. android:layout_marginRight="20dip"
  45. android:src="@drawable/actionbar_search_icon" />
  46.  
  47. <ImageView
  48. android:id="@+id/add"
  49. android:layout_width="30dp"
  50. android:layout_height="30dp"
  51. android:layout_marginRight="20dip"
  52. android:src="@drawable/actionbar_add_icon" />
  53.  
  54. <ImageView
  55. android:id="@+id/set"
  56. android:layout_width="30dp"
  57. android:layout_height="30dp"
  58. android:src="@drawable/actionbar_more_icon" />
  59. </LinearLayout>
  60. </RelativeLayout>
  61.  
  62. </LinearLayout>

ayout 文件下的top2 .xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6.  
  7. <LinearLayout
  8.      android:id="@+id/lllayout"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="horizontal" >
  12.  
  13. <LinearLayout
  14.        android:id="@+id/id_tab_liaotian_ly"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_weight="1"
  18. android:background="@drawable/guide_round"
  19. android:gravity="center"
  20. android:orientation="horizontal"
  21. android:padding="10dip" >
  22. <TextView
  23. android:id="@+id/id_liaotian"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:gravity="center"
  27. android:text="聊天"
  28. android:textColor="@color/green"
  29. android:textSize="15dip" />
  30. </LinearLayout>
  31.  
  32. <LinearLayout
  33. android:id="@+id/id_tab_faxian_ly"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:layout_weight="1"
  37. android:background="@drawable/guide_round" // 选择器
  38. android:clickable="true"
  39. android:gravity="center"
  40. android:orientation="horizontal"
  41. android:padding="10dip"
  42. android:saveEnabled="false" >
  43. <TextView
  44. android:id="@+id/id_faxian"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:gravity="center"
  48. android:text="发现"
  49. android:textColor="@color/black"
  50. android:textSize="15dip" />
  51. </LinearLayout>
  52.  
  53. <LinearLayout
  54.        android:id="@+id/id_tab_tongxunlu_ly"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:layout_weight="1"
  58. android:background="@drawable/guide_round"
  59. android:focusable="false"
  60. android:gravity="center"
  61. android:orientation="horizontal"
  62. android:padding="10dip" >
  63. <TextView
  64. android:id="@+id/id_tongxunlu"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:gravity="center"
  68. android:text="通讯录"
  69. android:textColor="@color/black"
  70. android:textSize="15dip" />
  71. </LinearLayout>
  72. </LinearLayout>
  73.  
  74. <ImageView
  75. android:id="@+id/id_tab_line"
  76. android:layout_width="200dp"
  77. android:layout_height="wrap_content"
  78. android:background="@drawable/vpi__tab_selected_pressed_holo" > //图片
  79. </ImageView>
  80.  
  81. </LinearLayout>
  1. 选择器 guide_round.xml
  1. <?xml version="1.0" encoding="UTF-8"?> //guide_round.xml
  2. <selector
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <item android:state_enabled="true" android:drawable="@drawable/abc_ab_share_pack_holo_dark" />
  5. <item android:state_enabled="false" android:drawable="@drawable/abc_cab_background_top_holo_light" />
  6. </selector>

第二步 :  主界面的实现MainActivity

  1. public class MainActivity extends FragmentActivity {
  2. private ViewPager mViewPager;
  3. private FragmentPagerAdapter mAdapter;
  4. private List<Fragment> mFragments = new ArrayList<Fragment>();
  5.  
  6. /**
  7. * 顶部三个LinearLayout
  8. */
  9. private LinearLayout mTabLiaotian;
  10. private LinearLayout mTabFaxian;
  11. private LinearLayout mTabTongxunlun;
  12. /**
  13. * 顶部的三个TextView
  14. */
  15. private TextView mLiaotian;
  16. private TextView mFaxian;
  17. private TextView mTongxunlu;
  18.  
  19. /**
  20. * 分别为每个TabIndicator创建一个BadgeView
  21. */
  22. private BadgeView mBadgeViewforLiaotian;
  23. private BadgeView mBadgeViewforFaxian;
  24. private BadgeView mBadgeViewforTongxunlu;
  25.  
  26. /**
  27. * Tab的那个引导线
  28. */
  29. private ImageView mTabLine;
  30. /**
  31. * ViewPager的当前选中页
  32. */
  33. private int currentIndex;
  34. /**
  35. * 屏幕的宽度
  36. */
  37. private int screenWidth;
  38.  
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_main);
  43. mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
  44. initView();
  45. initTabLine();
  46. setListener();
  47.  
  48. /**
  49. * 初始化Adapter
  50. */
  51. mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
  52. @Override
  53. public int getCount() {
  54. return mFragments.size();
  55. }
  56.  
  57. @Override
  58. public Fragment getItem(int arg0) {
  59. return mFragments.get(arg0);
  60. }
  61. };
  62.  
  63. mViewPager.setAdapter(mAdapter);
  64.  
  65. /**
  66. * 设置监听
  67. */
  68. mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
  69.  
  70. @Override
  71. public void onPageSelected(int position) {
  72. // 重置所有TextView的字体颜色
  73. resetTextView();
  74. switch (position) {
  75. case 0: //聊天
  76.  
  77. mTabLiaotian.removeView(mBadgeViewforLiaotian);
  78. mBadgeViewforLiaotian.setBadgeCount(5);
  79. mTabLiaotian.addView(mBadgeViewforLiaotian);
  80. mLiaotian.setTextColor(getResources().getColor(
  81. R.color.green));
  82. break;
  83. case 1: //发现
  84. mFaxian.setTextColor(getResources().getColor(R.color.green));
  85. mTabFaxian.removeView(mBadgeViewforFaxian);
  86. mBadgeViewforFaxian.setBadgeCount(15);
  87. mTabFaxian.addView(mBadgeViewforFaxian);
  88. break;
  89. case 2: //通讯录
  90. mTongxunlu.setTextColor(getResources().getColor(
  91. R.color.green));
  92.  
  93. break;
  94. }
  95.  
  96. currentIndex = position; //设置当前页面的值
  97. }
  98.  
  99. @Override
  100. public void onPageScrolled(int position, float positionOffset,
  101. int positionOffsetPixels) {
  102. /**
  103. * 利用position和currentIndex判断用户的操作是哪一页往哪一页滑动
  104. * 然后改变根据positionOffset动态改变TabLine的leftMargin
  105. */
  106. if (currentIndex == 0 && position == 0)// 0->1
  107. {
  108. LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
  109. .getLayoutParams();
  110. lp.leftMargin = (int) (positionOffset
  111. * (screenWidth * 1.0 / 3) + currentIndex
  112. * (screenWidth / 3));
  113. mTabLine.setLayoutParams(lp);
  114.  
  115. } else if (currentIndex == 1 && position == 0) // 1->0
  116. {
  117. LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
  118. .getLayoutParams();
  119. lp.leftMargin = (int) (-(1 - positionOffset)
  120. * (screenWidth * 1.0 / 3) + currentIndex
  121. * (screenWidth / 3));
  122. mTabLine.setLayoutParams(lp);
  123.  
  124. } else if (currentIndex == 1 && position == 1) // 1->2
  125. {
  126. LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
  127. .getLayoutParams();
  128. lp.leftMargin = (int) (positionOffset
  129. * (screenWidth * 1.0 / 3) + currentIndex
  130. * (screenWidth / 3));
  131. mTabLine.setLayoutParams(lp);
  132. } else if (currentIndex == 2 && position == 1) // 2->1
  133. {
  134. LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
  135. .getLayoutParams();
  136. lp.leftMargin = (int) (-(1 - positionOffset)
  137. * (screenWidth * 1.0 / 3) + currentIndex
  138. * (screenWidth / 3));
  139. mTabLine.setLayoutParams(lp);
  140.  
  141. }
  142.  
  143. }
  144.  
  145. @Override
  146. public void onPageScrollStateChanged(int state) {
  147. }
  148. });
  149.  
  150. mViewPager.setCurrentItem(1); // 默认选中1
  151.  
  152. }
  153.  
  154. /**
  155. * 根据屏幕的宽度,初始化引导线的宽度
  156. */
  157. private void initTabLine() {
  158. mTabLine = (ImageView) findViewById(R.id.id_tab_line);
  159. DisplayMetrics outMetrics = new DisplayMetrics();
  160. getWindow().getWindowManager().getDefaultDisplay()
  161. .getMetrics(outMetrics);
  162. screenWidth = outMetrics.widthPixels;
  163. LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
  164. .getLayoutParams();
  165. lp.width = screenWidth / 3;
  166. mTabLine.setLayoutParams(lp);
  167. }
  168.  
  169. /**
  170. * 重置颜色
  171. */
  172. protected void resetTextView() {
  173. mLiaotian.setTextColor(getResources().getColor(R.color.black));
  174. mFaxian.setTextColor(getResources().getColor(R.color.black));
  175. mTongxunlu.setTextColor(getResources().getColor(R.color.black));
  176. }
  177.  
  178. /**
  179. * 初始化控件,初始化Fragment
  180. */
  181. private void initView() {
  182. mTabLiaotian = (LinearLayout) findViewById(R.id.id_tab_liaotian_ly);
  183. mTabFaxian = (LinearLayout) findViewById(R.id.id_tab_faxian_ly);
  184. mTabTongxunlun = (LinearLayout) findViewById(R.id.id_tab_tongxunlu_ly);
  185. // 聊天
  186. mLiaotian = (TextView) findViewById(R.id.id_liaotian);
  187. // 发现
  188. mFaxian = (TextView) findViewById(R.id.id_faxian);
  189. // 通讯录
  190. mTongxunlu = (TextView) findViewById(R.id.id_tongxunlu);
  191. MainTab01 tab01 = new MainTab01();
  192. MainTab02 tab02 = new MainTab02();
  193. MainTab03 tab03 = new MainTab03();
  194. mFragments.add(tab01); // 存 fragment 的集合
  195. mFragments.add(tab02);
  196. mFragments.add(tab03);
  197. // 发现BadgeView
  198. mBadgeViewforFaxian = new BadgeView(this);
  199. mBadgeViewforLiaotian = new BadgeView(this); // 聊天BadgeView
  200. mBadgeViewforTongxunlu = new BadgeView(this); // 通讯录BadgeView
  201. }
  202.  
  203. // 监听点击 上面的条目的 ,切换页面的事件
  204. private void setListener() {
  205.  
  206. mTabLiaotian.setOnClickListener(new OnClickListener() {
  207.  
  208. @Override
  209. public void onClick(View arg0) {
  210. mViewPager.setCurrentItem(0);
  211. }
  212. });
  213. mTabFaxian.setOnClickListener(new OnClickListener() {
  214.  
  215. @Override
  216. public void onClick(View arg0) {
  217. mViewPager.setCurrentItem(1);
  218. }
  219. });
  220. mTabTongxunlun.setOnClickListener(new OnClickListener() {
  221.  
  222. @Override
  223. public void onClick(View arg0) {
  224. mViewPager.setCurrentItem(2);
  225. }
  226. });
  227.  
  228. }
  229. }
  1. MainTab01.java , MainTab01.java , MainTab01.java 都一样
  1.  
  1. public class MainTab01 extends Fragment
  2. {
  3.  
  4. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  5. {
  6. View messageLayout = inflater.inflate(R.layout.main_tab_02, container, false);
  7. return messageLayout;
  8. }
  9.  
  10. }
  1. 最后界面效果:

ViewPage+Fragment(仿微信切换带通知)的更多相关文章

  1. Android 高仿微信6.0主界面 带你玩转切换图标变色

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41087219,本文出自:[张鸿洋的博客] 1.概述 学习Android少不了模仿 ...

  2. Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...

  3. Android Studio精彩案例(三)《模仿微信ViewPage+Fragment实现方式二》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 写在前面的话:此专栏是博主在工作之余所写,每一篇文章尽可能写的思路清晰一些,属于博主的"精华"部分,不同于以往专栏 ...

  4. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  5. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  6. 转-Fragment+ViewPager组件(高仿微信界面)

    http://www.cnblogs.com/lichenwei/p/3982302.html 什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开 ...

  7. Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换

    一.问题描写叙述 本系列将结合案例应用,陆续向大家介绍一些Android典型界面的设计,首先说说tab导航,导航分为一层和两层(底部区块+区域内头部导航).主要实现方案有RadioGroup+View ...

  8. jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动

    jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动 http://www.17sucai.com/pins/demoshow/382

  9. Android仿微信界面--使用Fragment实现(慕课网笔记)

    1 效果图  这里我们没有实现滑动切换view的功能 2 具体实现: 2.1 布局文件:top.xml, bottom.xml,tab01.xml,tab02.xml,tab03.xml,tab04. ...

随机推荐

  1. spring-cloud 实现更新配置不用重启服务 @FreshScope

    继续前面搭建的spring cloud. 这里是基于rabbitMQ搭建的,首先需要在电脑上安装rabbitMQ. 在client端和server端分别加上如下依赖 compile group: 'o ...

  2. 声明:关于该博客部分Java等方向知识参考来源的说明

    [声明] 该博客部分代码是通过学习黑马程序员(传智播客)视频后,参考毕向东.张孝祥.杨中科等老师的公开课视频中讲解的代码,再结合自己的理解,自己手敲上去的,一方面加深自己的理解和方便以后自己用到的时候 ...

  3. 尽量不要在viewWillDisappear:方法中移除通知

    1.iOS7新增加了导航控制器侧滑手势,当触发侧滑返回时,会调用系统的viewWillDisappear:方法,取消侧滑返回时又会调用viewWillAppear:方法.   2.在做手势和通知等一系 ...

  4. 20145229吴姗珊《JAVA程序设计》第一周学习总结

    教材学习内容总结 第一章 JAVA 平台概论 1.JAVA不仅仅是一门程序设计语言,还是标准规范 2.1995年5月23日被公认为JAVA的诞生日 3.J2SE包含了JDK和JAVA程序语言 4.三大 ...

  5. LINQ 学习路程 -- 查询语法 LINQ Query Syntax

    1.查询语法 Query Syntax: from <range variable> in <IEnumerable<T> or IQueryable<T> ...

  6. 算法(Algorithms)第4版 练习 1.3.31

    双向链表实现: //1.3.31 package com.qiusongde.linkedlist; public class DoublyLinkedList<Item> { priva ...

  7. bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 优先队列+dij

    1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1768  Solv ...

  8. jQuery 开发一个简易插件

    jQuery 开发一个简易插件 //主要内容 $.changeCss = function(options){ var defaults = { color:'blue', ele:'text', f ...

  9. C++(四)— 字符串、数字翻转3种方法

    1.使用algorithm中的reverse函数,string类型字符建议使用. #include <iostream> #include <string> #include ...

  10. JS使用模板快速填充HTML控件数据

    function formatTemplate(dta, tmpl) { var format = { name: function(x) { return x ; } }; return tmpl. ...