Article From:https://www.cnblogs.com/ganchuanpu/p/6613784.html

There is a ListView that requires a floating sort of sorting and sorting at the bottom of the screen.

1、The finger pulldown is hidden, and the slide shows up.

2、If nothing is done, 2S will be displayed automatically.

3、Slide to the bottom and always show.

First look at the effect map:

To achieve the above effect, the actual principle is as follows:

 1、Fixed a BottomView at the top of the screen, XML layout best uses RelativeLayout (the bottom BottomView is not ListView footView, this is independent of footView, think why? )

 2、Then customize the ListView control to listen to the onTouchEvent event, mainly to monitor the slipping and skid events of the fingers, and to implement onScrollListener, to monitor whether to slide to the bottom and top.

3、 ListViewIn the monitor event, the bottomView is displayed and hidden, so ListView provides an interface to set the content of the bottom bootomView, and then you can control the bottomView and add the animation effect.

The next step is how to achieve this effect concretely.

1。The contents of the bottom BottomView are as follows: the contents of this XML file are customized and defined according to the content requirements of each project, bottom_view.xml: in my example.

<?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="/>Such< Button android:layout_height= 40dpAndroid:layout_width= wrAp_contentAndroid:layout_weight= 1Android:text= />Such< Button android:layout_height= 40dpAndroid:layout_width= wrap_content    Android:layout_weight= "1"Android:text= "sifting" />Such< /LinearLayout>

2、main.xmlas follows

<?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、Custom ListView control 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;  
  
/** 
 * The bottom View automatically concealment and disappear listview (other ListView can inherit the class, such as CtripBottomRefreshListView class, etc.)ItItPublic class BottoMFloatListView, extends, ListView, implements, OnScrollListener, {SuchPublic View mBottomBar;ItPrivate 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 =BottomFloatListViewSuchPublic BottomFloatListView Context (context)This (contex)T, null);Super.setOnScrollListener (this);    }  SuchPublic BottomFloatListView (ConText context, AttributeSet attrs)This (context, attrs, 0);Super.setOnScrollLiStener (this);    }  SuchPublic BottomFloatListView Context (context), AttributeSet AttributeSet attrs, int int dEfStyle)Super (context, attrs, defStyle)Super.setOnScrollListener (this);It   }  Such@OverridePublic void setAdapter (ListAdapter adapter)Super.setAdaptEr (adapter);    }  Such@OverridePublic void onScroll (AbsListView view, int int firstVisibl)EItem, int, visibleItemCount, int, totalItemCount)SuchShowBottomViewOnBottom (visibleItemCo)Unt, totalItemCount, firstVisibleItem);Such    }  Such@OverridePublic void onScrollStatEChanged (AbsListView view, int scrollState)SuchHideBottomViewOnScrollStateChanged (view)ScrollState)Such    }  Such@OverridePublic Boolean onTouchEvent (MotionEvent EV)Such""Float y = ev.getY ();Float x = ev.getX ();Log.d ("FloatListVi")"Ew", "onTouchEvent", "onTouchEvent + +", "+ + X + +".Int action ev.getAction = (&); MotionEvent.ACTION_MASKSwitch (action)Case MotionEvent.ACTION_DOWN:""Action_down (y);BreakCase MotionEvent.ACTION_MOVE:""MDeltaY, int (y) mMotionYBIsMoved, trueOn / / MobileWhen you want to remove the message showing bottomView,MHandler.removeCallbacks (showBottomBarRunnable);      The action_down incident was made up, because sometimes action_down incident did not execute.Action_down (y);""Break;Case MotionEvent.ACTION_UP:BIsMoved, falseSuchBIsDown, falseIf bIsMoved (& & bIsDown!)""If you don't do anything on the screen, you should show bottomView after 2S.MHandler.postDelayed (showBottomBa)RRunnable, 2000);As a resultIf mDeltaY (< 0)""HideBottomBar ();ElseShowBottomBar ()As a resultSuchBIsMoved, falseBreak"" }  SuchReturn (super.onTouchEvent) (EV);    }              Private void action_down (Float y)MMotionY = y;BIsDown = true;Log.d (TAG, action) downExeced ");MHandler.removeCallbacks (showBottomBarRunnable);    }  SuchA / * *""When you slide to the top, you need to hide bottomView@param view@param scrollState""Private void hideBottomViewOnScrollStateChanged (AbsListView view, int scrollState)MCurrentScrollStateScrollStateIf (view! =null)If (view.getFirstVisiblePosition ()) = (0) & &Amp; scrollState = = = SCROLL_STATE_IDLE)HideBottomBar ()Log.D (TAG, "hide bottom view");""""         }  SuchA / * *Display bottom floating bar""Public void showBottomBar ()SuchIf mBottomBar (null = & & mBottomB)Ar.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 (translateAnimati)On)TranslateAnimation.setAnimationListener (New Animation.AnimationListener)It@OverridePublic void onAnimationStart (Animation animation)     ""Such@OverridePublic void onAnimationRepeat AnimatioN animation)As a resultSuch@OverridePublic void onAnImationEnd (Animation animation)MBottomBar.setVisibility (View.VISIBLE);Such""""""    }  SuchA / * *Hidden floating bottom column""PRIvate void hideBottomBar ()""If mBottomBar (null = & & mBottomBar.getVi)Sibility () = = = = View.VISIBLE)Animation translateAnimation new = TranslateAnimation mBOttomBar.getLeft (), mBottomBar.getLeft (), 0, 30TranslateAnimation.setDuration (300);SuchTranslateAnimation.setInterpolator (New OvershootInterpolator 0.6f)MBOttomBar.startAnimation (translateAnimation);TranslateAnimation.setAnimationListener (n)EW Animation.AnimationListener@OverridePublic void onAnimaTionStart (Animation animation)As a resultSuch@Override""Public void onAnimationRepeat AnimationAs a resultSuch@OveRridePublic void onAnimationEnd (Animation animation)MBotTomBar.setVisibility (View.GONE);As a result""""    }  Such    / * * *When you slide to the bottom, you can directly show bottomView.@param visibleItemCount@param totalItemCount    @param firstVisibleItem""Private void showBottomViewOnBottom (int visibleItemCount)Int totalItemCount, int int firstVisibleItem""Log.d (TAG, "visible" bottem item)(count:) "firstVisibleItem: + +", "firstVisibleItem + + firstVisibleItem + + oldFirstVisibleItem:" + + oldFirstVisibleItem + +MBottomBar)If (getLastVisiblePosition ()) = totalItemCount, -1, &, &, mCurrentSCrollState = SCROLL_STATE_IDLE)ShowBottomBar ()""    }  SuchPrivate, Runnable, showBottomBarRunnable, new = Runnable ()Such@OverridePubliC void run ()ShowBottomBar ();""Such    };  SuchA / * *V will need to hide the displayIew afferent""@param bottomBar""Public void setBottomBar (ViewGroup bottomBar)ItThis.mBottomBar = bottomBar;    }  Such}

4、The main interface test Activity, MainActivity code is as follows

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("Test data "I";""Return data;    }  }

ViewGroup bottomView = (ViewGroup)findViewById(R.id.bottombar) ;  
mBottomFloatListView.setBottomBar(bottomView); 
The introduction of bottomView at the bottom into ListView enables ListView to have the function of automatically hiding and disappearing the underlying View.  

  

  

  

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *