Android带数字或红点的底部导航拦和联网等待加载动画

首先展示一下截图效果,下载地址在文章最后
一、Android带红点的底部导航拦
1.首先写底部导航栏的界面view_main_tab.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" android:orientation="horizontal" android:background="#27282c" > <RelativeLayout android:id="@+id/rl_1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1"> <RadioButton android:id="@+id/rb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:button="@null" android:background="@null" android:checked="true" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_home" android:gravity="center" android:text="首页" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_1" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_1" android:layout_alignTop="@id/rb_1" android:layout_marginTop="-6dp" android:layout_marginRight="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1" android:focusable="true"> <RadioButton android:id="@+id/rb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:button="@null" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_goods_divide" android:gravity="center" android:text="商品" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_2" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_2" android:layout_alignTop="@id/rb_2" android:layout_marginTop="-6dp" android:layout_marginRight="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1"> <RadioButton android:id="@+id/rb_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:button="@null" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_stock_list" android:gravity="center" android:text="进货单" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_3" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_3" android:layout_alignTop="@id/rb_3" android:layout_marginTop="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1"> <RadioButton android:id="@+id/rb_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:button="@null" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_member" android:gravity="center" android:text="会员" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_4" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_4" android:layout_alignTop="@id/rb_4" android:layout_marginTop="-6dp" android:layout_marginRight="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> </LinearLayout>
2.修改底部导航栏的数字,在MainActivity中
/**
* -1:表示没有新消息
* -2:表示新消息用红点的方式显示
* 0-99:直接显示数字
* >=100:用99+显示
*/
private void messageTips(int num, TextView tv) {
if(num==-1){
tv.setVisibility(View.GONE);
}else if(num==-2){
tv.setVisibility(View.VISIBLE);
tv.setText("");
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
layoutParams.height= DensityUtil.dip2px(this,10);
layoutParams.width= DensityUtil.dip2px(this,10);
tv.setLayoutParams(layoutParams);
}else if(num>=0&&num<=99){
tv.setVisibility(View.VISIBLE);
tv.setText(num+"");
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
layoutParams.height= DensityUtil.dip2px(this,16);
layoutParams.width= DensityUtil.dip2px(this,16);
tv.setLayoutParams(layoutParams);
}else if(num>=100){
tv.setVisibility(View.VISIBLE);
tv.setText("99+");
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
layoutParams.height= DensityUtil.dip2px(this,16);
layoutParams.width= DensityUtil.dip2px(this,16);
tv.setTextSize(DensityUtil.sp2px(this,3));
tv.setLayoutParams(layoutParams);
}else{
tv.setVisibility(View.GONE);
}
}
3.需要在fragment中修改MainActivity中的底部导航拦,所以,要在MainActivity中,写一些公用的方法。
/**
* 在oneFragment中更新,底部导航栏的数字
* @param num
*/
public void updateOne(int num){
messageTips(num,tv_1);
}
/**
* 在TwoFragment中更新,底部导航栏的数字
* @param num
*/
public void updateTwo(int num){
messageTips(num,tv_2);
}
/**
* 在ThreeFragment中更新,底部导航栏的数字
* @param num
*/
public void updateThree(int num){
messageTips(num,tv_3);
}
/**
* 在FourFragment中更新,底部导航栏的数字
* @param num
*/
public void updateFour(int num){
messageTips(num,tv_4);
}
4.在fragment中修改底部导航拦,得到主页面,调用主页面的修改方法。
mActivity = (MainActivity) getActivity(); number++; mActivity.updateTwo(number);
二、activity加载动画。
1.activity中的加载动画,要写一个BaseActivity。布局如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_base" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.hrobbie.loadingproject.activity.BaseActivity"> <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentInsetStart="0.0dp" android:background="@color/colorPrimary" app:layout_scrollFlags="enterAlways|scroll" app:popupTheme="@style/AppTheme.PopupOverlay" /> <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/loading_anim"/> </FrameLayout> </LinearLayout>
注意:id为fl_content的FrameLayout的布局里,包含了一个loading_anim的布局,这就是加载布局。加载布局,里面氛围三个线性布局,分别是:加载中布局,加载错误布局,没有数据布局,其中加载失败布局,还需要点击重新加载。内容如下:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!--加载中--> <LinearLayout android:id="@+id/ll_loading" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="90dp" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/iv_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loading_everyday" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="正在为您开启干货推荐.." android:textColor="@color/colorTitle" android:textSize="14sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="7dp" android:text="看的越多,推荐越准" android:textColor="@color/colorSubtitle" android:textSize="12sp" android:visibility="visible" /> </LinearLayout> <!--加载失败--> <LinearLayout android:id="@+id/ll_error_refresh" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="gone"> <ImageView android:id="@+id/img_err" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/load_err" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="加载失败,点击重试" android:textSize="15sp" /> </LinearLayout> <!--加载失败--> <LinearLayout android:id="@+id/ll_no_data" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="gone"> <ImageView android:id="@+id/img_no_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/load_err" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="sorry,没有您想要的数据" android:textSize="15sp" /> </LinearLayout> </FrameLayout>
2.Baseactivity的代码太多,讲一下主要的,重写setContentView方法,把新布局放入id为fl_content的布局中,调用getWindow()。setContentView(rootView);剩下的就跟普通个activity操作一样了。
@Override
public void setContentView(@LayoutRes int layoutResID) {
View rootView = LayoutInflater.from(this).inflate(R.layout.activity_base,null,false);
addView = LayoutInflater.from(this).inflate(layoutResID, null, false);
//content
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
addView.setLayoutParams(params);
fl_content = (FrameLayout) rootView.findViewById(R.id.fl_content);
fl_content.addView(addView);
getWindow().setContentView(rootView);
initView();
showLoading();
}
3.新的activity只需集成BaseActivity,当需要加载成功是,调用loadSuccess()方放,加载失败时调用loadError(),失败后重新加载,需要调用reLoading()重新加载,并调用onRefresh()重新加载数据。如果没有数据调用noData()
三、fragment中加载动画,把加载布局,放入fragment中,我暂时没有好的办法提出BaseFragment进行统一加载。有一些注意事项。
1.viewpager进行布局加载时,最好能够预加载一个屏幕的数据。
vp_main.setOffscreenPageLimit(3);//最好是一屏能显示的fragment数-1。
2.在BaseFragment重写setUserVisibleHint方法,当fragment可见时,才联网加载数据。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getUserVisibleHint()){
isVisible=true;
onVisible();
}else {
isVisible=false;
onInvisible();
}
}
3.fragment继承BaseFragment需要在onViewCreated中调用一下联网加载方法,因为,setUserVisibleHint执行比较靠前,页面还没有添加到布局,就加载数据,会造成填充数据失败,需要当页面完全添加到布局中,再联网请求。
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mActivity= (MainActivity) getActivity();
showLoading();
lazyLoad();
}
下载地址:LoadingProject_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# android
# 小红点提示
# 消息提醒红点
# 加载等待动画
# android实现加载动画对话框
# Android 自定义加载动画Dialog弹窗效果的示例代码
# android自定义波浪加载动画的实现代码
# Android 使用 Path 实现搜索动态加载动画效果
# Android自定义带加载动画效果的环状进度条
# Android自定义view利用Xfermode实现动态文字加载动画
# Android Glide图片加载(加载监听、加载动画)
# Android仿支付宝笑脸刷新加载动画的实现代码
# Android实现跳动的小球加载动画效果
# Android实现仿微软系统加载动画效果
# 加载
# 下载地址
# 重写
# 新消息
# 还没有
# 加载中
# 太多
# 这就是
# 为您
# 只需
# 要在
# 如果没有
# 还需要
# 越多
# 暂时没有
# 首页
# 就跟
# 您想
# 大家多多
# 要写
相关文章:
宝塔新建站点为何无法访问?如何排查?
建站上传速度慢?如何优化加速网站加载效率?
香港服务器如何优化才能显著提升网站加载速度?
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
5种Android数据存储方式汇总
表情包在线制作网站免费,表情包怎么弄?
创业网站制作流程,创业网站可靠吗?
如何在橙子建站上传落地页?操作指南详解
如何用虚拟主机快速搭建网站?详细步骤解析
如何快速生成橙子建站落地页链接?
如何在IIS7上新建站点并设置安全权限?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
企业宣传片制作网站有哪些,传媒公司怎么找企业宣传片项目?
ppt制作免费网站有哪些,ppt模板免费下载网站?
如何在云主机上快速搭建多站点网站?
如何快速搭建高效可靠的建站解决方案?
无锡制作网站公司有哪些,无锡优八网络科技有限公司介绍?
如何快速搭建高效WAP手机网站?
建站之星展会模板:智能建站与自助搭建高效解决方案
Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解
c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】
宁波自助建站系统如何快速打造专业企业网站?
如何快速搭建个人网站并优化SEO?
网站制作模板下载什么软件,ppt模板免费下载网站?
如何在万网ECS上快速搭建专属网站?
一键网站制作软件,义乌购一件代发流程?
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
如何选择香港主机高效搭建外贸独立站?
建站主机如何安装配置?新手必看操作指南
建站主机选购指南:核心配置与性价比推荐解析
制作网站的基本流程,设计网站的软件是什么?
视频网站制作教程,怎么样制作优酷网的小视频?
建站主机选哪种环境更利于SEO优化?
建站之星×万网:智能建站系统+自助建站平台一键生成
制作销售网站教学视频,销售网站有哪些?
Swift中循环语句中的转移语句 break 和 continue
如何解决VPS建站LNMP环境配置常见问题?
深圳企业网站制作设计,在深圳如何网上全流程注册公司?
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
在线制作视频网站免费,都有哪些好的动漫网站?
重庆市网站制作公司,重庆招聘网站哪个好?
广德云建站网站建设方案与建站流程优化指南
如何设计高效校园网站?
广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?
上海网站制作网站建设公司,建筑电工证网上查询系统入口?
做企业网站制作流程,企业网站制作基本流程有哪些?
焦点电影公司作品,电影焦点结局是什么?
Swift中switch语句区间和元组模式匹配
宝塔新建站点报错如何解决?
*请认真填写需求信息,我们会在24小时内与您取得联系。