欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

listview的上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項(xiàng)欄的實(shí)例

 更新時(shí)間:2018年01月04日 14:31:18   作者:西域黃老板  
下面小編就為大家分享一篇listview的上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項(xiàng)欄的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

listview的上滑下滑監(jiān)聽,來(lái)隱藏和顯示頂部選項(xiàng)欄的特效,京東 同程等APP的資源列表都有此特效.

兩個(gè)重點(diǎn):

①listview的setOnTouchListener監(jiān)聽方法

當(dāng)滑動(dòng)的Y位置減去按下的Y位置大于最小滑動(dòng)距離時(shí)則為向下滑動(dòng)

反之,當(dāng)按下的Y位置減去滑動(dòng)的Y位置大于最小滑動(dòng)距離則為向上滑動(dòng)

②位移動(dòng)畫

就只要這兩點(diǎn)需要注意的,直接上代碼,注釋很清楚。

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;//最小滑動(dòng)距離
  protected float mFirstY;//觸摸下去的位置
  protected float mCurrentY;//滑動(dòng)時(shí)Y的位置
  protected int direction;//判斷是否上滑或者下滑的標(biāo)志
  protected boolean mShow;//判斷是否執(zhí)行了上滑動(dòng)畫
  private Animator mAnimator;//動(dòng)畫屬性
  @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);//自定義一個(gè)頭布局和頂部執(zhí)行動(dòng)畫的布局等高就行
    listview.addHeaderView(header);//加載頭布局
    //獲得一個(gè)最小滑動(dòng)距離
    mTouchShop = ViewConfiguration.get(this).getScaledTouchSlop();//系統(tǒng)級(jí)別的一個(gè)屬性,判斷用戶的最小滑動(dòng)距離的,可查看源碼為16
    //給集合添加數(shù)據(jù)
    for (int i = 0; i < 40; i++) {
      Map<String, Object>map = new HashMap<String, Object>();
      map.put("str", "第"+i+"個(gè)item");
      list.add(map);
    }
    String a[] = {"str"};
    int b[] = {R.id.tv01};
    //simpleadapter加載集合數(shù)據(jù)
    SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, a, b);
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {//listview的點(diǎn)擊方法
      @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是因?yàn)榧虞d的頭布局
      }
    });
    listview.setOnTouchListener(new OnTouchListener() {//listview的觸摸事件
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          mFirstY = event.getY();//按下時(shí)獲取位置
          break;
        case MotionEvent.ACTION_MOVE:
          mCurrentY = event.getY();//得到滑動(dòng)的位置
          if(mCurrentY - mFirstY > mTouchShop){//滑動(dòng)的位置減去按下的位置大于最小滑動(dòng)距離 則表示向下滑動(dòng)
            direction = 0;//down
          }else if(mFirstY - mCurrentY > mTouchShop){//反之向上滑動(dòng)
            direction = 1;//up
          }
          if(direction == 1){//判斷如果是向上滑動(dòng) 則執(zhí)行向上滑動(dòng)的動(dòng)畫
            if(mShow){//判斷動(dòng)畫是否執(zhí)行了 執(zhí)行了則改變狀態(tài)
              //執(zhí)行往上滑動(dòng)的動(dòng)畫
              tolbarAnim(1);
              mShow = !mShow;
            }
          }else if(direction == 0){//判斷如果是向下滑動(dòng) 則執(zhí)行向下滑動(dòng)的動(dòng)畫
            if(!mShow){//判斷動(dòng)畫是否執(zhí)行了 執(zhí)行了則改變狀態(tài)
              //執(zhí)行往下滑動(dòng)的動(dòng)畫
              tolbarAnim(0);
              mShow = !mShow;
            }
          }
          break;
        case MotionEvent.ACTION_UP:
          break;
        }
        return false;
      }
    });
  }

  private void tolbarAnim(int flag){
    if(mAnimator != null && mAnimator.isRunning()){//判斷動(dòng)畫存在 如果啟動(dòng)了,則先關(guān)閉
      mAnimator.cancel();
    }
    if(flag == 0){
      mAnimator = ObjectAnimator.ofFloat(top_rl, "translationY", top_rl.getTranslationY(),0);//從當(dāng)前位置位移到0位置
    }else{
      mAnimator = ObjectAnimator.ofFloat(top_rl, "translationY", top_rl.getTranslationY(),-top_rl.getHeight());//從當(dāng)前位置移動(dòng)到布局負(fù)高度的wiz
    }
    mAnimator.start();//執(zhí)行動(dòng)畫
  }

}
<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="第一個(gè)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>

一個(gè)listview的滑動(dòng)監(jiān)聽動(dòng)畫實(shí)現(xiàn)搞定 很好理解對(duì)吧。

以上這篇listview的上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項(xiàng)欄的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 保存ListView上次的滾動(dòng)條的位置實(shí)例(必看)

    保存ListView上次的滾動(dòng)條的位置實(shí)例(必看)

    下面小編就為大家?guī)?lái)一篇保存ListView上次的滾動(dòng)條的位置實(shí)例(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • android使用TextView實(shí)現(xiàn)跑馬燈效果

    android使用TextView實(shí)現(xiàn)跑馬燈效果

    這篇文章主要為大家詳細(xì)介紹了android使用TextView實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android SDK在線更新鏡像服務(wù)器大全

    Android SDK在線更新鏡像服務(wù)器大全

    由于一些原因,Google相關(guān)很多服務(wù)都無(wú)法訪問(wèn),所以在很多時(shí)候我們SDK也無(wú)法升級(jí),當(dāng)然通過(guò)技術(shù)手段肯定可以解決,但是比較麻煩,而且下載速度也不怎么樣
    2015-10-10
  • Android應(yīng)用關(guān)閉的情況以及識(shí)別方法詳解

    Android應(yīng)用關(guān)閉的情況以及識(shí)別方法詳解

    對(duì)于現(xiàn)在的安卓手機(jī)而言,很多功能都是在逐步完善的,這篇文章主要給大家介紹了關(guān)于Android應(yīng)用關(guān)閉的情況以及識(shí)別的相關(guān)資料,文章通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Android使用個(gè)推實(shí)現(xiàn)三方應(yīng)用的推送功能

    Android使用個(gè)推實(shí)現(xiàn)三方應(yīng)用的推送功能

    這篇文章主要為大家詳細(xì)介紹了Android使用個(gè)推實(shí)現(xiàn)三方應(yīng)用的推送功能,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android實(shí)現(xiàn)京東秒殺界面

    Android實(shí)現(xiàn)京東秒殺界面

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)京東秒殺界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 詳解Android開發(fā)中硬件加速支持的使用方法

    詳解Android開發(fā)中硬件加速支持的使用方法

    這篇文章主要介紹了Android應(yīng)用開發(fā)中硬件加速支持的使用方法,主要針對(duì)圖形繪制時(shí)的硬件加速與OpenGL調(diào)用,需要的朋友可以參考下
    2016-02-02
  • Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解

    Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解

    這篇文章主要介紹了Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-09-09
  • Android SpringAnimation彈性動(dòng)畫解析

    Android SpringAnimation彈性動(dòng)畫解析

    這篇文章主要為大家詳細(xì)介紹了Android SpringAnimation彈性動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 基于android示例程序(bitmapfun) 高效加載圖片讓人無(wú)語(yǔ)地方

    基于android示例程序(bitmapfun) 高效加載圖片讓人無(wú)語(yǔ)地方

    嘗試了使用git上的一個(gè)開源項(xiàng)目afinal(bitmapfun的封裝版)來(lái)加載圖片,但是在測(cè)試的時(shí)候發(fā)現(xiàn)了一個(gè)問(wèn)題,新的圖片加載器(bitmapfun)比之前用的ImageDownloader要慢很多,特別是在網(wǎng)絡(luò)狀況不好的時(shí)候,那簡(jiǎn)直是太讓人無(wú)語(yǔ)了
    2013-04-04

最新評(píng)論