自定义tab吸顶效果一(原理)
PS:问题:什么是吸顶,吸顶有什么作用,吸顶怎么使用?
在很多app商城中,介绍软件的时候就会使用吸顶效果,
吸顶有很多作用,一个最简单粗暴的作用就是,让用户知道此刻在浏览哪个模块,并可以选择另外的模块,不需要再滑到顶部,有时我们在查看一个软件的简介的时候上拉布局,导航栏还在,这里以App Store为例:如
吸顶该怎么用呢,这里有一个简单的实现方法,在这期间有一个问题,是说ScrollViewd的滑动监听不能检测布局距离的问题,我查了写资料,说是在6.0之前的sdk不支持,但是可以自己重写方法。反正重写很简单。
先上效果图:
实现原理:创建两个布局,这两个布局处于重叠状态,一个布局上面显示 背景为蓝色+导航栏+数据内容,注意这里的导航栏随着布局可移动,第二个布局显示导航栏但处于影藏状态,当滑动屏幕时,蓝色部分全部被拉上去后,第二个布局显示导航栏即可。
1:首先重写ScrollView里的滑动方法,可创建一个接口,来实现接口里的方法即可
public interface ScrollViewListener {
void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt);
}
MyScrollView1.java
package cn.views; import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ScrollView; /**
* Created by cMusketeer on 17/11/21.
*
* @author 刘志通
*/
public class MyScrollView1 extends ScrollView {
public ScrollViewListener scrollViewListener = null; public MyScrollView1(Context context) {
super(context);
} public MyScrollView1(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyScrollView1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyScrollView1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
} //上面都是自动生成的,下面为正文,是实现接口方法。
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
} @Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
} public interface ScrollViewListener {
void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt);
}
}
2:布局文件创建ceshilayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <cn.views.MyScrollView1
android:id="@+id/id_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"> <RelativeLayout
android:id="@+id/id_zong"
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:id="@+id/id_img"
android:layout_width="match_parent"
android:layout_height="300px"
android:layout_centerHorizontal="true"
android:background="@color/colorPrimary"
android:gravity="center"> <ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="false"
android:src="@mipmap/ic_launcher" />
</RelativeLayout> <RelativeLayout
android:id="@+id/wenzi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/id_img">
<include layout="@layout/bottomlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></include>
</RelativeLayout>
<TextView
android:layout_below="@id/wenzi"
android:textSize="20dp"
android:text=" Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。Android操作系统最初由Andy Rubin开发,主要支持手机。2005年8月由Google收购注资。2007年11月,Google与84家硬件制造商、软件开发商及电信营运商组建开放手机联盟共同研发改良Android系统。随后Google以Apache开源许可证的授权方式,发布了Android的源代码。第一部Android智能手机发布于2008年10月。Android逐渐扩展到平板电脑及其他领域上,如电视、数码相机、游戏机等。2011年第一季度,Android在全球的市场份额首次超过塞班系统,跃居全球第一。 2013年的第四季度,Android平台手机的全球市场份额已经达到78.1%。[1] 2013年09月24日谷歌开发的操作系统Android在迎来了5岁生日,全世界采用这款系统的设备数量已经达到10亿台。"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout> </cn.views.MyScrollView1> <RelativeLayout
android:id="@+id/id_xianshi"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<include layout="@layout/bottomlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></include>
</RelativeLayout> </RelativeLayout>
这里有个导航栏的布局,bottom layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#f5f4f4"
android:layout_height="51dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textSize="20dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:textColor="#3e66dc"
android:text="简介"/> <TextView
android:textColor="#50dc3e"
android:gravity="center"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:text="评论" />
<TextView
android:textColor="#50dc3e"
android:gravity="center"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:text="相关" /> </LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorPrimary"></View> </LinearLayout>
3:关键代码,然而也就那么几行,不过需要注意的是,在xml文件中,蓝色背景的部分为300px,如果是300dp,则要把dp转成px单位。
scrollView.setScrollViewListener(new MyScrollView1.ScrollViewListener() {
@Override
public void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt) {
//需要注意的是这里比较是px单位,如果是dp还要转成px。
if(t>=300){
tv_show.setVisibility(View.VISIBLE);
}else{
tv_show.setVisibility(View.GONE);
}
}
});
CeshiActivity.java总代码
package day1.cn.xiaohangjia; import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout; import cn.views.MyScrollView1; /**
* Created by cMusketeer on 17/11/21.
*
* @author 刘志通
*/
public class CeShiActivity extends AppCompatActivity { private RelativeLayout tv_show; @TargetApi(Build.VERSION_CODES.M)
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setContentView(R.layout.ceshilayout); MyScrollView1 scrollView= (MyScrollView1) findViewById(R.id.id_scrollview);
tv_show = (RelativeLayout) findViewById(R.id.id_xianshi);
scrollView.setScrollViewListener(new MyScrollView1.ScrollViewListener() { @Override
public void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt) {
//需要注意的是这里比较是px单位,如果是dp还要转成px。
if(t>=300){
tv_show.setVisibility(View.VISIBLE);
}else{
tv_show.setVisibility(View.GONE);
}
}
}); }
}
完。
自定义tab吸顶效果一(原理)的更多相关文章
- react.js中实现tab吸顶效果问题
在react项目开发中有一个需求是,页面滚动到tab所在位置时,tab要固定在顶部. 实现的思路其实很简单,就是判断当滚动距离scrollTop大于tab距离页面顶部距离offsetTop时,将tab ...
- [RN] React Native 头部 滑动吸顶效果的实现
React Native 头部 滑动吸顶效果的实现 效果如下图所示: 实现方法: 一.吸顶组件封装 StickyHeader .js import * as React from 'react'; i ...
- tabControl组件的吸顶效果
最开始,还没有使用better-scroll插件的时候,直接在class中设定了一定的position为sticky,设置一定的top达成了效果.但是,使用better-scroll组件后,这些属性就 ...
- Html吸顶效果
Html吸顶效果 一.HTML HTML中需要给div一个id <!DOCTYPE html> <html lang="en"> <head> ...
- 基于scroll的吸顶效果
本次要实现的是一种常见的网页效果,如下: 页面由头部,导航,主体内容三部分组成,当页面发生滚动时,头部逐渐隐藏,导航部分向上移动,直到导航部分距离浏览器顶部为零时,导航部分固定不动,保持吸顶效果,如下 ...
- better-scroll之吸顶效果巨坑挣扎中
今天和大家分享下better-scroll这款移动端用来解决各种滚动需求的插件(目前已经支持PC) 关于其中的API大家可以去官网看下 这里就给大家介绍几种常用的以及需要注意的点是什么 首先说一下b ...
- js之吸顶效果
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue 事件监听实现导航栏吸顶效果(页面滚动后定位)
Vue 事件监听实现导航栏吸顶效果(页面滚动后定位) Howie126313 关注 2017.11.19 15:05* 字数 100 阅读 3154评论 0喜欢 0 所说的吸顶效果就是在页面没有滑动之 ...
- js 实现吸顶效果 || 小程序的吸顶效果
小程序吸顶效果 <!--index.wxml--> <view class="container"> <view class='outside-img ...
随机推荐
- java二进制相关基础
转载请注明原创出处,谢谢! 说在前面 之前在JVM菜鸟进阶高手之路十(基础知识开场白)的时候简单提到了二进制相关问题,最近在看RocketMQ的源码的时候,发现涉及二进制的内容蛮多,jdk源码里面也是 ...
- [转] linux系统中如何进入退出vim编辑器,方法及区别
原文链接:http://www.cnblogs.com/crazylqy/p/5649860.html 在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触 ...
- 关于如何在微信里面让video不全屏播放
发现一个更简单的解决方法,加上一条属性即可.x5-video-player-type="h5″.通过设置这个属性,让video开启同层H5播放器.另外在X5内核里,video是单独的一个vi ...
- CentOS 7.2 配置mysql5.7
准备篇:一.配置防火墙,开启80端口.3306端口CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙.1.关闭firewall:systemctl stop f ...
- openstack pike 创建vxlan网络
#openstack pike 创建vxlan网络 openstack pike 集群高可用 安装部署 汇总 http://www.cnblogs.com/elvi/p/7613861.html # ...
- nyoj 63 小猴子下落 思维
nyoj 63 小猴子下落 题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=63 思路: 不需要用指针创建二叉树,也不需要用数组来模拟二叉 ...
- javascript 45种缓动效果BY司徒正美
javascript 45种缓动效果 参数 类型 说明 el element 必需,为页面元素 begin number 必需,开始的位置 change number 必需,要移动的距离 durati ...
- 框架原理第一讲,熟悉常用的设计方式.(以MFC框架讲解)
框架原理第一讲,熟悉常用的设计方式.(以MFC框架讲解) 一丶什么是框架,以及框架的作用 什么是框架? 框架,简而言之就是把东西封装好了,使用框架开发可以快速开发程序,例如MFC程序的双击写代码. 为 ...
- Mysql的安装和图形化界面的使用
访问mysql网址:https://dev.mysql.com/ 下面需要登录你的oracle账号进行下载就好~ 下载之后是一解压包形式存在的~ 解压之后的文件 这里我新建了my.ini的文件~将my ...
- 使用NPOI读取Excel数据到DataTable
如今XML文件的存储格式大行其道,可是也不是适用于全部情况,非常多单位的数据交换还是使用Excel的形式.这就使得我们须要读取Excel内的数据.载入到程序中进行处理.可是如何有效率的读取,如何使程序 ...