浅谈ListView滑动隐藏显示ToolBar
引言
在App日益追求体验的时代,优秀的用户体验往往会使产品脱颖而出。今天我们就来介绍一种简单的滑动ListView来显示或者隐藏ToolBar的功能。
布局文件
下面我们来看一下这个主界面的布局文件。在这个布局文件中,主要是一个ListView控件和一个ToolBar控件。布局如下:
<?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"> <ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:divider="#abcdee"
android:dividerHeight="1px"
android:id="@+id/listView"> </ListView> <!--ToolBar-->
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4097e6"
android:id="@+id/toolBar"> </android.support.v7.widget.Toolbar> </RelativeLayout>
主界面代码
实现思路:
让一个布局显示或者隐藏并且带有动画效果,我们可以通过属性动画来实现。实现这个效果的关键就是监听ListView的各种滑动事件,我们肯定需要借助View的OnTouchListener接口来监听各种状态。注意点:
由于增加了一个ToolBar,我们需要为ListView添加一个HeadView,防止ToolBar挡住ListView的第一个Item。
下面看代码实现:
package com.research.gong.android_view_research; import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { private ListView listView;
String[] datas = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10",
"A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20"};
private float scaledTouchSlop;
private float firstY = 0;
private Toolbar toolbar;
private ObjectAnimator animtor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolBar);
listView = (ListView) findViewById(R.id.listView);
/**
* 添加一个HeadView避免第一个Item被ToolBar遮挡
* 必须在setAdapter之前进行设置
*/
initHeadView();
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datas));
//判断认为是滑动的最小距离(乘以系数调整滑动灵敏度)
scaledTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop()*3.0f;
/**
* 设置触摸事件
*/
listView.setOnTouchListener(new View.OnTouchListener() {
private float currentY;
private int direction=-1;
private boolean mShow = true; @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
firstY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
currentY = event.getY();
//向下滑动
if (currentY - firstY > scaledTouchSlop) {
direction = 0;
}
//向上滑动
else if (firstY - currentY > scaledTouchSlop) {
direction = 1;
}
//如果是向上滑动,并且ToolBar是显示的,就隐藏ToolBar
if (direction == 1) {
if (mShow) {
toobarAnim(1);
mShow = !mShow;
}
} else if (direction == 0) {
if (!mShow) {
toobarAnim(0);
mShow = !mShow;
}
}
break;
case MotionEvent.ACTION_UP:
break;
}
return false;//注意此处不能返回true,因为如果返回true,onTouchEvent就无法执行,导致的后果是ListView无法滑动
}
});
} /**
* 设置头布局,注意:这个头布局的高度要和ToolBar的高度一致
*/
public void initHeadView() {
View view = new View(this);
//abc_action_bar_default_height_material获取系统ActionBar的高度
AbsListView.LayoutParams params = new AbsListView.LayoutParams
(AbsListView.LayoutParams.MATCH_PARENT,
(int) getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
view.setLayoutParams(params);
listView.addHeaderView(view);
} /**
* ToolBar显示隐藏动画
* @param direction
*/
public void toobarAnim(int direction) {
//开始新的动画之前要先取消以前的动画
if (animtor != null && animtor.isRunning()) {
animtor.cancel();
}
//toolbar.getTranslationY()获取的是Toolbar距离自己顶部的距离
float translationY=toolbar.getTranslationY();
if (direction == 0) {
animtor = ObjectAnimator.ofFloat(toolbar, "translationY", translationY, 0);
} else if (direction == 1) {
animtor = ObjectAnimator.ofFloat(toolbar, "translationY", translationY, -toolbar.getHeight());
}
animtor.start();
}
}
相信代码中注释已经解释的很详细了。唯一需要注意的是:scaledTouchSlop值默认获取的是Android系统能识别的最小滑动距离。我们通过乘以相关系数,可以适当的调整滑动的灵敏度。
浅谈ListView滑动隐藏显示ToolBar的更多相关文章
- 安卓开发_浅谈ListView(SimpleAdapter数组适配器)
安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...
- 安卓开发_浅谈ListView(自定义适配器)
ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果 有这样一个Demo ...
- Android项目实战(二十):浅谈ListView悬浮头部展现效果
先看下效果:需求是 滑动列表 ,其中一部分视图(粉丝数,关注数这一部分)在滑动到顶端的时候不消失,而是停留在整个界面头部. 我们先分析要解决的问题: 1.如何实现列表ListView顶部视图跟随Lis ...
- Android -- Toolbar跟随ListView滑动隐藏和显现
布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- 安卓开发_浅谈ListView之分页列表
前言: 在开发的过程中,有时候我们需要从网络解析一些数据,比如最近的一些新闻,我们需要把这些数据用ListView显示出来. 因为是解析一个网络数据源,这样将会一下子将所有的数据解析出来,当数据源数据 ...
- 安卓开发_浅谈ListView(ArrayAdapter数组适配器)
列表视图(ListView)以垂直的形式列出需要显示的列表项. 实现过程:新建适配器->添加数据源到适配器->视图加载适配器 在安卓中,有两种方法可以在屏幕中添加列表视图 1.直接用Lis ...
- 浅谈RecyclerView(完美替代ListView,GridView)
Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...
- ListView的HeaderView包含的GridView滑动隐藏后无法点击问题分析
目录 1 现象 2 问题分析 2.1 滑动前 2.2 滑动后 2.3 mDataChanged赋值为true的位置 2.3 GridView直接作为ListView的HeaderView为什么可以滑动 ...
- 原生JS实现全屏切换以及导航栏滑动隐藏及显示——重构前
思路分析: 向后滚动鼠标滚轮,页面向下全屏切换:向前滚动滚轮,页面向上全屏切换.切换过程为动画效果. 第一屏时,导航栏固定在页面顶部,切换到第二屏时,导航条向左滑动隐藏.切换回第一屏时,导航栏向右滑动 ...
随机推荐
- html 概念
HTML 超文本标记语言,标准通用标记语言下的一个应用.http://baike.baidu.com/link?url=RYF4Pj7VUPifcXatU7OJLGRljIgkp4MjzkspARor ...
- Java基础知识笔记(八:集合类)
目录 1 集合类简介 2 List介绍及简单使用 2.1 LinkedList介绍及简单使用 2.2 ArrayList介绍及简单使用 2.3 Vector介绍及简单使用 2.3.1 S ...
- css content之counter-reset、content-increment
万万没想到,写了快三年前端,有不会用的css,居然还有完全没听过.见过的css属性,而且还是CSS2的内容! 关于counter-reset.content-increment两个属性的详解可以参看张 ...
- 前端小菜鸟的Mobile之旅---开篇
背景:前段时间有幸参与了公司一个基于H5的手机APP项目,(我们用的React+ES6+Webpack+Cordova开发),由此开始接触一些关于H5开发手机APP方面的知识,下面Shar ...
- mac 键盘映射 karabiner
mac 键盘映射 karabiner 今天在vim编辑的时候觉得用mac的方向键有点麻烦 需要移动我的小右手,然后就搜个映射方案. 百度出来了 karabiner. 官网 安装什么的就不说了, 安完了 ...
- python下如何安装biopython
本来是自学python,后来又了解到有biopython这个包,将想安装下来,结果折腾了我一上午...终于安装成了,哈哈哈,功夫不负有心啊 过程如下: 1.首先去http://biopython.or ...
- Lind.DDD.RedisClient~对StackExchange.Redis调用者的封装及多路复用技术
回到目录 两雄争霸 使用StackExchange.Redis的原因是因为它开源,免费,而对于商业化的ServiceStack.Redis,它将一步步被前者取代,开源将是一种趋势,商业化也值得被我们尊 ...
- commons configuration管理项目的配置文件
Commons Confifutation commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件. ...
- 通过一个实例重新认识引用类型,值类型,数组,堆栈,ref
昨天在写代码时候遇到了一个问题,百思不得其解,感觉颠覆了自己对C#基础知识的认知,因为具体的情境涉及公司代码不便放出,我在这里举个例子,先上整个测试所有的代码,然后一一讲解我的思考过程: using ...
- 关于List的ConcurrentModificationException
对ArrayList的操作我们可以通过索引象来访问,也可以通过Iterator来访问,只要不对ArrayList结构上进行修改都不会造成ConcurrentModificationException, ...