前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >listview的上滑下滑监听,上下滑监听隐藏顶部选项栏的实例

listview的上滑下滑监听,上下滑监听隐藏顶部选项栏的实例

作者头像
砸漏
发布2020-10-29 22:42:03
1K0
发布2020-10-29 22:42:03
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

listview的上滑下滑监听,来隐藏和显示顶部选项栏的特效,京东 同程等APP的资源列表都有此特效.

两个重点:

①listview的setOnTouchListener监听方法

当滑动的Y位置减去按下的Y位置大于最小滑动距离时则为向下滑动

反之,当按下的Y位置减去滑动的Y位置大于最小滑动距离则为向上滑动

②位移动画

就只要这两点需要注意的,直接上代码,注释很清楚。

package com.example.android_topbar_gone;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private RelativeLayout top_rl;
private ListView listview;
private List<Map<String, Object  list = new ArrayList<Map<String,Object  ();
private int mTouchShop;//最小滑动距离
protected float mFirstY;//触摸下去的位置
protected float mCurrentY;//滑动时Y的位置
protected int direction;//判断是否上滑或者下滑的标志
protected boolean mShow;//判断是否执行了上滑动画
private Animator mAnimator;//动画属性
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化id
setViews();
//加载listview
setListView();
}
/**
* 初始化id
*/
private void setViews() {
top_rl = (RelativeLayout) findViewById(R.id.rl_ttt);
listview = (ListView) findViewById(R.id.listview);
}
/**
* 加载listview
*/
private void setListView() {
View header = View.inflate(this, R.layout.headview, null);//自定义一个头布局和顶部执行动画的布局等高就行
listview.addHeaderView(header);//加载头布局
//获得一个最小滑动距离
mTouchShop = ViewConfiguration.get(this).getScaledTouchSlop();//系统级别的一个属性,判断用户的最小滑动距离的,可查看源码为16
//给集合添加数据
for (int i = 0; i < 40; i++) {
Map<String, Object map = new HashMap<String, Object ();
map.put("str", "第"+i+"个item");
list.add(map);
}
String a[] = {"str"};
int b[] = {R.id.tv01};
//simpleadapter加载集合数据
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, a, b);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {//listview的点击方法
@Override
public void onItemClick(AdapterView<?  arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, list.get(arg2-1).get("str")+"", 0).show();//-1是因为加载的头布局
}
});
listview.setOnTouchListener(new OnTouchListener() {//listview的触摸事件
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mFirstY = event.getY();//按下时获取位置
break;
case MotionEvent.ACTION_MOVE:
mCurrentY = event.getY();//得到滑动的位置
if(mCurrentY - mFirstY   mTouchShop){//滑动的位置减去按下的位置大于最小滑动距离 则表示向下滑动
direction = 0;//down
}else if(mFirstY - mCurrentY   mTouchShop){//反之向上滑动
direction = 1;//up
}
if(direction == 1){//判断如果是向上滑动 则执行向上滑动的动画
if(mShow){//判断动画是否执行了 执行了则改变状态
//执行往上滑动的动画
tolbarAnim(1);
mShow = !mShow;
}
}else if(direction == 0){//判断如果是向下滑动 则执行向下滑动的动画
if(!mShow){//判断动画是否执行了 执行了则改变状态
//执行往下滑动的动画
tolbarAnim(0);
mShow = !mShow;
}
}
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
}
private void tolbarAnim(int flag){
if(mAnimator != null && mAnimator.isRunning()){//判断动画存在 如果启动了,则先关闭
mAnimator.cancel();
}
if(flag == 0){
mAnimator = ObjectAnimator.ofFloat(top_rl, "translationY", top_rl.getTranslationY(),0);//从当前位置位移到0位置
}else{
mAnimator = ObjectAnimator.ofFloat(top_rl, "translationY", top_rl.getTranslationY(),-top_rl.getHeight());//从当前位置移动到布局负高度的wiz
}
mAnimator.start();//执行动画
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"  
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:divider="@null"
android:listSelector="@android:color/transparent"
android:scrollbars="@null"  
</ListView 
<RelativeLayout 
android:id="@+id/rl_ttt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#39caab"
</RelativeLayout 
</RelativeLayout 
<?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"  
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"  
</RelativeLayout 
</RelativeLayout 
<?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"  
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#eeeeee"  
<TextView
android:id="@+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="18dp"
android:text="第一个item"
android:textColor="#646464"
android:textSize="14dp" / 
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentBottom="true"
android:background="#d2d2d2" / 
</RelativeLayout 
</RelativeLayout 

一个listview的滑动监听动画实现搞定 很好理解对吧。

以上这篇listview的上滑下滑监听,上下滑监听隐藏顶部选项栏的实例就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档