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

本文介绍如何通过监听ListView的滑动事件来实现类似京东、同程APP的顶部选项栏隐藏和显示特效。关键在于使用setOnTouchListener监听滑动方向,并应用位移动画。适合安卓开发初学者参考。

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的滑动监听动画实现搞定 很好理解对吧.
我是西域黄老板,安卓一年新人,希望大家多多支持.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值