android自定義ListView實(shí)現(xiàn)底部View自動(dòng)隱藏和消失的功能
有這樣一個(gè)ListView,要求在屏幕底部有一個(gè)篩選排序的浮動(dòng)框:
1、手指下拉隱藏,上滑顯示 ;
2、如果沒(méi)做任何操作,2S之后,要自動(dòng)顯示;
3、滑動(dòng)到最底部,始終顯示。
首先看其效果圖:

實(shí)現(xiàn)上述效果,其實(shí)現(xiàn)原理如下:
1、在屏幕頂部固定一個(gè)BottomView,XML布局最好使用RelativeLayout(底部的BottomView并不是 ListView的footView,這個(gè)是和footView獨(dú)立的,想想為什么?)
2、然后自定義ListView控件,監(jiān)聽(tīng)onTouchEvent事件,主要是監(jiān)聽(tīng)手指下滑和上滑事件,同時(shí)實(shí)現(xiàn)onScrollListener,監(jiān)聽(tīng)是否滑動(dòng)到最底部和最頂部
3、 ListView監(jiān)聽(tīng)事件中,控制bottomView的顯示和隱藏,所以ListView提供一個(gè)接口,設(shè)置底部bootomView的內(nèi)容,然后獲之后,就可以對(duì)bottomView進(jìn)行控制,同時(shí)加上動(dòng)畫效果。
接下來(lái)看是如何的具體實(shí)現(xiàn)這種效果:
1。底部BottomView的內(nèi)容如下,這個(gè)XML文件的內(nèi)容是自定義的,根據(jù)各項(xiàng)目的內(nèi)容需求來(lái)定義的,我例子中bottom_view.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_layout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#cbcbcb"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button android:layout_height="40dp"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="價(jià)格" />
<Button android:layout_height="40dp"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="好評(píng)" />
<Button android:layout_height="40dp"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="篩選" />
</LinearLayout>
2、main.xml如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.BottomFloatListView.BottomFloatListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none"
/>
<include
android:id="@+id/bottombar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="@layout/bottom_view"
>
</include>
</RelativeLayout>
3、自定義ListView控件BottomFloatListView
package com.example.BottomFloatListView;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.*;
import android.widget.AbsListView.OnScrollListener;
/**
* 底部View自動(dòng)隱藏和消失listview(其他ListView可以繼承該類,如CtripBottomRefreshListView類等)
**/
public class BottomFloatListView extends ListView implements OnScrollListener {
public View mBottomBar;
private int mCurrentScrollState;
private boolean bIsMoved = false;
private boolean bIsDown = false;
private int mDeltaY;
private float mMotionY;
private int oldFirstVisibleItem = 0;
private Handler mHandler = new Handler();
private static final String TAG = "BottomFloatListView";
public BottomFloatListView(Context context) {
this(context, null);
super.setOnScrollListener(this);
}
public BottomFloatListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
super.setOnScrollListener(this);
}
public BottomFloatListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setOnScrollListener(this);
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
showBottomViewOnBottom(visibleItemCount, totalItemCount, firstVisibleItem);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
hideBottomViewOnScrollStateChanged(view, scrollState);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
float y = ev.getY();
float x = ev.getX();
Log.d("FloatListView", "onTouchEvent" + "" + x + "" + y);
int action = ev.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN:
action_down(y);
break;
case MotionEvent.ACTION_MOVE:
mDeltaY = (int) (y - mMotionY);
bIsMoved = true;
//移動(dòng)的時(shí)候,要移除掉顯示bottomView的消息
mHandler.removeCallbacks(showBottomBarRunnable);
//補(bǔ)齊action_down事件,因?yàn)橛械臅r(shí)候,action_down 事件沒(méi)有執(zhí)行
action_down(y);
break;
case MotionEvent.ACTION_UP:
bIsMoved = false;
bIsDown = false;
if (!bIsMoved && !bIsDown) {
// 如果屏幕上什么沒(méi)做,則過(guò)2s之后要顯示bottomView
mHandler.postDelayed(showBottomBarRunnable, 2000);
}
if (mDeltaY < 0) { //下滑影藏
hideBottomBar();
} else { //上滑顯示
showBottomBar();
}
bIsMoved = false;
break;
}
return super.onTouchEvent(ev);
}
private void action_down(float y){
mMotionY = y;
bIsDown = true;
Log.d(TAG, "action down execed");
mHandler.removeCallbacks(showBottomBarRunnable);
}
/**
* 滑動(dòng)到頂部時(shí),要隱藏bottomView
* @param view
* @param scrollState
*/
private void hideBottomViewOnScrollStateChanged(AbsListView view, int scrollState) {
mCurrentScrollState = scrollState;
if(view!=null){
if (view.getFirstVisiblePosition() == 0 && scrollState == SCROLL_STATE_IDLE) {
hideBottomBar();
Log.d(TAG, "hide bottom view");
}
}
}
/**
* 顯示底部浮動(dòng)欄
*/
public void showBottomBar() {
if (mBottomBar != null && mBottomBar.getVisibility() == View.GONE) {
mBottomBar.setVisibility(View.INVISIBLE);
Animation translateAnimation = new TranslateAnimation(mBottomBar.getLeft(), mBottomBar.getLeft(),30, 0);
translateAnimation.setDuration(300);
translateAnimation.setInterpolator(new OvershootInterpolator(0.6f));
mBottomBar.startAnimation(translateAnimation);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mBottomBar.setVisibility(View.VISIBLE);
}
});
}
}
/**
* 隱藏浮動(dòng)底部欄
*/
private void hideBottomBar() {
if (mBottomBar != null && mBottomBar.getVisibility() == View.VISIBLE) {
Animation translateAnimation = new TranslateAnimation(mBottomBar.getLeft(), mBottomBar.getLeft(), 0, 30);
translateAnimation.setDuration(300);
translateAnimation.setInterpolator(new OvershootInterpolator(0.6f));
mBottomBar.startAnimation(translateAnimation);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mBottomBar.setVisibility(View.GONE);
}
});
}
}
/**
* 滑動(dòng)到底部時(shí)直接顯示bottomView
* @param visibleItemCount
* @param totalItemCount
* @param firstVisibleItem
*/
private void showBottomViewOnBottom(int visibleItemCount, int totalItemCount, int firstVisibleItem) {
Log.d(TAG, "visible bottem item count:" + "firstVisibleItem:" + firstVisibleItem + "oldFirstVisibleItem:" + oldFirstVisibleItem + mBottomBar);
if(getLastVisiblePosition() == totalItemCount -1 && mCurrentScrollState != SCROLL_STATE_IDLE){
showBottomBar();
}
}
private Runnable showBottomBarRunnable = new Runnable() {
@Override
public void run() {
showBottomBar();
}
};
/**
* 將需要隱藏顯示的view傳入
*
* @param bottomBar
*/
public void setBottomBar(ViewGroup bottomBar) {
this.mBottomBar = bottomBar;
}
}
4、主界面測(cè)試的Activity,MainActivity代碼如下
public class MainActivity extends Activity {
private BottomFloatListView mBottomFloatListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBottomFloatListView = (BottomFloatListView)findViewById(R.id.listView) ;
mBottomFloatListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
ViewGroup bottomView = (ViewGroup)findViewById(R.id.bottombar) ;
mBottomFloatListView.setBottomBar(bottomView);
}
private List<String> getData(){
List<String> data = new ArrayList<String>();
for(int i = 0; i <100; i++) {
data.add("測(cè)試數(shù)據(jù)" + i);
}
return data;
}
}
ViewGroup bottomView = (ViewGroup)findViewById(R.id.bottombar) ; mBottomFloatListView.setBottomBar(bottomView);
將底部的bottomView傳入到ListView中,就可以讓ListView具有底部View自動(dòng)隱藏和消失的功能。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android listview優(yōu)化幾種寫法詳細(xì)介紹
- android開發(fā)教程之listview使用方法
- android開發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
- Android自定義Adapter的ListView的思路及代碼
- Android下拉刷新ListView——RTPullListView(demo)
- Android之帶group指示器的ExpandableListView(自寫)
- Android ListView的item背景色設(shè)置和item點(diǎn)擊無(wú)響應(yīng)的解決方法
- android中ListView多次刷新重復(fù)執(zhí)行g(shù)etView的解決方法
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
- android二級(jí)listview列表實(shí)現(xiàn)代碼
相關(guān)文章
iOS中給UITableView的側(cè)滑刪除增加多個(gè)按鈕的實(shí)現(xiàn)方法
在項(xiàng)目中遇到這樣一個(gè)需求,cell的側(cè)滑刪除默認(rèn)只有一個(gè)刪除按鈕, 給側(cè)滑添加多個(gè)按鈕, '刪除', '置頂', '更多'.怎么實(shí)現(xiàn)呢?下面小編給大家分享iOS中給UITableView的側(cè)滑刪除增加多個(gè)按鈕的實(shí)現(xiàn)方法,一起看看吧2017-02-02
Android開發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能示例
這篇文章主要介紹了Android開發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能,涉及Android基于Handler操作圖片的相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2017-09-09
Android消息機(jī)制Handler的工作過(guò)程詳解
這篇文章主要為大家詳細(xì)介紹了Android消息機(jī)制Handler的工作過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
textView 添加超鏈接(兩種實(shí)現(xiàn)方式)
在textView添加超鏈接,有兩種方式,第一種通過(guò)HTML格式化你的網(wǎng)址,一種是設(shè)置autolink,讓系統(tǒng)自動(dòng)識(shí)別超鏈接,下面為大家介紹下這兩種方法的實(shí)現(xiàn)2013-06-06
Android入門之SubMenu的實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)SubMenu子菜單的效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,感興趣的可以了解一下2022-11-11
android特賣列表倒計(jì)時(shí)卡頓問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了android特賣列表倒計(jì)時(shí)卡頓問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09

