Realization method of Android ListView elastic effect
- 2021-07-18 09:02:31
- OfStack
There are many different ways to realize the elastic effect of ListView in Android. There are also many ways to search on the Internet. Below, two ways to realize the elastic effect of ListView that are often used in projects are posted (basically they can be used) for your reference:
The first one is relatively simple and easy to understand, but dynamically changes the movable distance of ListView on Y axis. The code is as follows:
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.ListView;
/**
* Elasticity ListView .
* @author E
*/
public class FlexiListView extends ListView{
// Initial pullable Y Axial distance
private static final int MAX_Y_OVERSCROLL_DISTANCE = 100;
// Context environment
private Context mContext;
// Actual pull up and down Y Distance on the axis
private int mMaxYOverscrollDistance;
public FlexiListView(Context context){
super(context);
mContext = context;
initBounceListView();
}
public FlexiListView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initBounceListView();
}
public FlexiListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
initBounceListView();
}
private void initBounceListView(){
final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// The essence of implementation is that it changes dynamically here maxOverScrollY Value of
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);
}
}
The second method combines gestures to achieve the elastic effect of ListView. Here, more extensions can be made according to gestures. The code is as follows:
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ListView;
/**
* Having elastic effect ListView . Mainly to implement the parent class dispatchTouchEvent Methods and OnGestureListener Medium onScroll Method.
* @author E
*/
public class FlexibleListView extends ListView implements OnGestureListener{
private Context context = null;
private boolean outBound = false;
private int distance;
private int firstOut;
public FlexibleListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public FlexibleListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public FlexibleListView(Context context) {
super(context);
this.context = context;
}
GestureDetector lisGestureDetector = new GestureDetector(context, this);
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int act = event.getAction();
if ((act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL)
&& outBound) {
outBound = false;
// scroll back
}
if (!lisGestureDetector.onTouchEvent(event)) {
outBound = false;
} else {
outBound = true;
}
Rect rect = new Rect();
getLocalVisibleRect(rect);
TranslateAnimation am = new TranslateAnimation( 0, 0, -rect.top, 0);
am.setDuration(300);
startAnimation(am);
scrollTo(0, 0);
return super.dispatchTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
int firstPos = getFirstVisiblePosition();
int lastPos = getLastVisiblePosition();
int itemCount = getCount();
// outbound Top
if (outBound && firstPos != 0 && lastPos != (itemCount - 1)) {
scrollTo(0, 0);
return false;
}
View firstView = getChildAt(firstPos);
if (!outBound)
firstOut = (int) e2.getRawY();
if (firstView != null&& (outBound || (firstPos == 0
&& firstView.getTop() == 0 && distanceY < 0))) {
// Record the length of each slide
distance = firstOut - (int) e2.getRawY();
scrollTo(0, distance / 2);
return true;
}
// outbound Bottom
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
}
The above two commonly used Android ListView elastic effect of the implementation method, sorted out, I hope to help everyone!