如何在android項目中里ListView隱藏底部View

這篇文章將為大家詳細講解有關(guān)如何在android項目中里ListView隱藏底部View,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計,成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè),網(wǎng)站設(shè)計,企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為成百上千服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!

1。底部BottomView的內(nèi)容如下,這個XML文件的內(nèi)容是自定義的,根據(jù)各項目的內(nè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="價格" /> 

  

  <Button android:layout_height="40dp" 

       android:layout_width="wrap_content" 

       android:layout_weight="1" 

       android:text="好評" /> 

  

  <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自動隱藏和消失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; 

         //移動的時候,要移除掉顯示bottomView的消息 

         mHandler.removeCallbacks(showBottomBarRunnable); 

         //補齊action_down事件,因為有的時候,action_down 事件沒有執(zhí)行 

         action_down(y); 

         break; 

       case MotionEvent.ACTION_UP: 

         bIsMoved = false; 

         bIsDown = false; 

         if (!bIsMoved && !bIsDown) { 

           // 如果屏幕上什么沒做,則過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); 

  } 

  

  /** 

   * 滑動到頂部時,要隱藏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"); 

       } 

    } 

   

  } 

  

  /** 

   * 顯示底部浮動欄 

   */ 

  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); 

        } 

      }); 

    } 

  } 

  

  /** 

   * 隱藏浮動底部欄 

   */ 

  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); 

        } 

      }); 

    } 

  } 

  

  /** 

   * 滑動到底部時直接顯示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、主界面測試的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("測試數(shù)據(jù)" + i); 

    } 

    return data; 

  } 

}
ViewGroup bottomView = (ViewGroup)findViewById(R.id.bottombar) ; 
mBottomFloatListView.setBottomBar(bottomView);

將底部的bottomView傳入到ListView中,就可以讓ListView具有底部View自動隱藏和消失的功能?!?/p>

關(guān)于如何在android項目中里ListView隱藏底部View就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

本文名稱:如何在android項目中里ListView隱藏底部View
轉(zhuǎn)載來源:http://bm7419.com/article30/jdssso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、App開發(fā)微信小程序、品牌網(wǎng)站設(shè)計、網(wǎng)站設(shè)計、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司