ViewPage+Fragment(仿微信切换带通知)
第一步 : 布局文件 activity_main.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:background="#eee"
- android:orientation="vertical" >
- <include layout="@layout/top1" /> // 顶部的菜单
- <include layout="@layout/top2" /> // 顶部的聊天 , 发现 ,通讯录
- <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>
- </LinearLayout>
layout 文件下的top1
- <?xml version="1.0" encoding="utf-8"?> // top1
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/abc_ab_bottom_solid_dark_holo" //头部的黑背景
- android:padding="12dip" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:gravity="center"
- android:orientation="horizontal" >
- <ImageView
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:src="@drawable/actionbar_icon" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dip"
- android:text="微信"
- android:textColor="@color/lightgray"
- android:textSize="18dp" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true" //居右
- android:gravity="center"
- android:orientation="horizontal" >
- <ImageView
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_marginRight="20dip"
- android:src="@drawable/actionbar_search_icon" />
- <ImageView
- android:id="@+id/add"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_marginRight="20dip"
- android:src="@drawable/actionbar_add_icon" />
- <ImageView
- android:id="@+id/set"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:src="@drawable/actionbar_more_icon" />
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
ayout 文件下的top2 .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="wrap_content"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/lllayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <LinearLayout
- android:id="@+id/id_tab_liaotian_ly"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@drawable/guide_round"
- android:gravity="center"
- android:orientation="horizontal"
- android:padding="10dip" >
- <TextView
- android:id="@+id/id_liaotian"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="聊天"
- android:textColor="@color/green"
- android:textSize="15dip" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/id_tab_faxian_ly"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@drawable/guide_round" // 选择器
- android:clickable="true"
- android:gravity="center"
- android:orientation="horizontal"
- android:padding="10dip"
- android:saveEnabled="false" >
- <TextView
- android:id="@+id/id_faxian"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="发现"
- android:textColor="@color/black"
- android:textSize="15dip" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/id_tab_tongxunlu_ly"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@drawable/guide_round"
- android:focusable="false"
- android:gravity="center"
- android:orientation="horizontal"
- android:padding="10dip" >
- <TextView
- android:id="@+id/id_tongxunlu"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="通讯录"
- android:textColor="@color/black"
- android:textSize="15dip" />
- </LinearLayout>
- </LinearLayout>
- <ImageView
- android:id="@+id/id_tab_line"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:background="@drawable/vpi__tab_selected_pressed_holo" > //图片
- </ImageView>
- </LinearLayout>
- 选择器 guide_round.xml
- <?xml version="1.0" encoding="UTF-8"?> //guide_round.xml
- <selector
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_enabled="true" android:drawable="@drawable/abc_ab_share_pack_holo_dark" />
- <item android:state_enabled="false" android:drawable="@drawable/abc_cab_background_top_holo_light" />
- </selector>
第二步 : 主界面的实现MainActivity
- public class MainActivity extends FragmentActivity {
- private ViewPager mViewPager;
- private FragmentPagerAdapter mAdapter;
- private List<Fragment> mFragments = new ArrayList<Fragment>();
- /**
- * 顶部三个LinearLayout
- */
- private LinearLayout mTabLiaotian;
- private LinearLayout mTabFaxian;
- private LinearLayout mTabTongxunlun;
- /**
- * 顶部的三个TextView
- */
- private TextView mLiaotian;
- private TextView mFaxian;
- private TextView mTongxunlu;
- /**
- * 分别为每个TabIndicator创建一个BadgeView
- */
- private BadgeView mBadgeViewforLiaotian;
- private BadgeView mBadgeViewforFaxian;
- private BadgeView mBadgeViewforTongxunlu;
- /**
- * Tab的那个引导线
- */
- private ImageView mTabLine;
- /**
- * ViewPager的当前选中页
- */
- private int currentIndex;
- /**
- * 屏幕的宽度
- */
- private int screenWidth;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
- initView();
- initTabLine();
- setListener();
- /**
- * 初始化Adapter
- */
- 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 position) {
- // 重置所有TextView的字体颜色
- resetTextView();
- switch (position) {
- case 0: //聊天
- mTabLiaotian.removeView(mBadgeViewforLiaotian);
- mBadgeViewforLiaotian.setBadgeCount(5);
- mTabLiaotian.addView(mBadgeViewforLiaotian);
- mLiaotian.setTextColor(getResources().getColor(
- R.color.green));
- break;
- case 1: //发现
- mFaxian.setTextColor(getResources().getColor(R.color.green));
- mTabFaxian.removeView(mBadgeViewforFaxian);
- mBadgeViewforFaxian.setBadgeCount(15);
- mTabFaxian.addView(mBadgeViewforFaxian);
- break;
- case 2: //通讯录
- mTongxunlu.setTextColor(getResources().getColor(
- R.color.green));
- break;
- }
- currentIndex = position; //设置当前页面的值
- }
- @Override
- public void onPageScrolled(int position, float positionOffset,
- int positionOffsetPixels) {
- /**
- * 利用position和currentIndex判断用户的操作是哪一页往哪一页滑动
- * 然后改变根据positionOffset动态改变TabLine的leftMargin
- */
- if (currentIndex == 0 && position == 0)// 0->1
- {
- LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
- .getLayoutParams();
- lp.leftMargin = (int) (positionOffset
- * (screenWidth * 1.0 / 3) + currentIndex
- * (screenWidth / 3));
- mTabLine.setLayoutParams(lp);
- } else if (currentIndex == 1 && position == 0) // 1->0
- {
- LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
- .getLayoutParams();
- lp.leftMargin = (int) (-(1 - positionOffset)
- * (screenWidth * 1.0 / 3) + currentIndex
- * (screenWidth / 3));
- mTabLine.setLayoutParams(lp);
- } else if (currentIndex == 1 && position == 1) // 1->2
- {
- LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
- .getLayoutParams();
- lp.leftMargin = (int) (positionOffset
- * (screenWidth * 1.0 / 3) + currentIndex
- * (screenWidth / 3));
- mTabLine.setLayoutParams(lp);
- } else if (currentIndex == 2 && position == 1) // 2->1
- {
- LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
- .getLayoutParams();
- lp.leftMargin = (int) (-(1 - positionOffset)
- * (screenWidth * 1.0 / 3) + currentIndex
- * (screenWidth / 3));
- mTabLine.setLayoutParams(lp);
- }
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- }
- });
- mViewPager.setCurrentItem(1); // 默认选中1
- }
- /**
- * 根据屏幕的宽度,初始化引导线的宽度
- */
- private void initTabLine() {
- mTabLine = (ImageView) findViewById(R.id.id_tab_line);
- DisplayMetrics outMetrics = new DisplayMetrics();
- getWindow().getWindowManager().getDefaultDisplay()
- .getMetrics(outMetrics);
- screenWidth = outMetrics.widthPixels;
- LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
- .getLayoutParams();
- lp.width = screenWidth / 3;
- mTabLine.setLayoutParams(lp);
- }
- /**
- * 重置颜色
- */
- protected void resetTextView() {
- mLiaotian.setTextColor(getResources().getColor(R.color.black));
- mFaxian.setTextColor(getResources().getColor(R.color.black));
- mTongxunlu.setTextColor(getResources().getColor(R.color.black));
- }
- /**
- * 初始化控件,初始化Fragment
- */
- private void initView() {
- mTabLiaotian = (LinearLayout) findViewById(R.id.id_tab_liaotian_ly);
- mTabFaxian = (LinearLayout) findViewById(R.id.id_tab_faxian_ly);
- mTabTongxunlun = (LinearLayout) findViewById(R.id.id_tab_tongxunlu_ly);
- // 聊天
- mLiaotian = (TextView) findViewById(R.id.id_liaotian);
- // 发现
- mFaxian = (TextView) findViewById(R.id.id_faxian);
- // 通讯录
- mTongxunlu = (TextView) findViewById(R.id.id_tongxunlu);
- MainTab01 tab01 = new MainTab01();
- MainTab02 tab02 = new MainTab02();
- MainTab03 tab03 = new MainTab03();
- mFragments.add(tab01); // 存 fragment 的集合
- mFragments.add(tab02);
- mFragments.add(tab03);
- // 发现BadgeView
- mBadgeViewforFaxian = new BadgeView(this);
- mBadgeViewforLiaotian = new BadgeView(this); // 聊天BadgeView
- mBadgeViewforTongxunlu = new BadgeView(this); // 通讯录BadgeView
- }
- // 监听点击 上面的条目的 ,切换页面的事件
- private void setListener() {
- mTabLiaotian.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- mViewPager.setCurrentItem(0);
- }
- });
- mTabFaxian.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- mViewPager.setCurrentItem(1);
- }
- });
- mTabTongxunlun.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- mViewPager.setCurrentItem(2);
- }
- });
- }
- }
- MainTab01.java , MainTab01.java , MainTab01.java 都一样
- public class MainTab01 extends Fragment
- {
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
- {
- View messageLayout = inflater.inflate(R.layout.main_tab_02, container, false);
- return messageLayout;
- }
- }
- 最后界面效果:
ViewPage+Fragment(仿微信切换带通知)的更多相关文章
- Android 高仿微信6.0主界面 带你玩转切换图标变色
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41087219,本文出自:[张鸿洋的博客] 1.概述 学习Android少不了模仿 ...
- Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...
- Android Studio精彩案例(三)《模仿微信ViewPage+Fragment实现方式二》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 写在前面的话:此专栏是博主在工作之余所写,每一篇文章尽可能写的思路清晰一些,属于博主的"精华"部分,不同于以往专栏 ...
- Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像
此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...
- 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)
什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...
- 转-Fragment+ViewPager组件(高仿微信界面)
http://www.cnblogs.com/lichenwei/p/3982302.html 什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开 ...
- Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换
一.问题描写叙述 本系列将结合案例应用,陆续向大家介绍一些Android典型界面的设计,首先说说tab导航,导航分为一层和两层(底部区块+区域内头部导航).主要实现方案有RadioGroup+View ...
- jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动
jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动 http://www.17sucai.com/pins/demoshow/382
- Android仿微信界面--使用Fragment实现(慕课网笔记)
1 效果图 这里我们没有实现滑动切换view的功能 2 具体实现: 2.1 布局文件:top.xml, bottom.xml,tab01.xml,tab02.xml,tab03.xml,tab04. ...
随机推荐
- spring-cloud 实现更新配置不用重启服务 @FreshScope
继续前面搭建的spring cloud. 这里是基于rabbitMQ搭建的,首先需要在电脑上安装rabbitMQ. 在client端和server端分别加上如下依赖 compile group: 'o ...
- 声明:关于该博客部分Java等方向知识参考来源的说明
[声明] 该博客部分代码是通过学习黑马程序员(传智播客)视频后,参考毕向东.张孝祥.杨中科等老师的公开课视频中讲解的代码,再结合自己的理解,自己手敲上去的,一方面加深自己的理解和方便以后自己用到的时候 ...
- 尽量不要在viewWillDisappear:方法中移除通知
1.iOS7新增加了导航控制器侧滑手势,当触发侧滑返回时,会调用系统的viewWillDisappear:方法,取消侧滑返回时又会调用viewWillAppear:方法. 2.在做手势和通知等一系 ...
- 20145229吴姗珊《JAVA程序设计》第一周学习总结
教材学习内容总结 第一章 JAVA 平台概论 1.JAVA不仅仅是一门程序设计语言,还是标准规范 2.1995年5月23日被公认为JAVA的诞生日 3.J2SE包含了JDK和JAVA程序语言 4.三大 ...
- LINQ 学习路程 -- 查询语法 LINQ Query Syntax
1.查询语法 Query Syntax: from <range variable> in <IEnumerable<T> or IQueryable<T> ...
- 算法(Algorithms)第4版 练习 1.3.31
双向链表实现: //1.3.31 package com.qiusongde.linkedlist; public class DoublyLinkedList<Item> { priva ...
- bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 优先队列+dij
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1768 Solv ...
- jQuery 开发一个简易插件
jQuery 开发一个简易插件 //主要内容 $.changeCss = function(options){ var defaults = { color:'blue', ele:'text', f ...
- C++(四)— 字符串、数字翻转3种方法
1.使用algorithm中的reverse函数,string类型字符建议使用. #include <iostream> #include <string> #include ...
- JS使用模板快速填充HTML控件数据
function formatTemplate(dta, tmpl) { var format = { name: function(x) { return x ; } }; return tmpl. ...