Customize View to achieve an elastic sliding effect,for your reference,The specific content is as follows
Implementation principle
Measure all sub-Views in onMeasure()
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Measure all child Views
int count = getChildCount();
for (int i = 0; i <count; i++) {
View childView = getChildAt(i);
measureChild( childView, widthMeasureSpec, heightMeasureSpec);
}
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
onLayout(), will All sub-Views are arranged in order of position
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Set the height of ViewGroup and arrange all child Views
int childCount = getChildCount();
MarginLayoutParams params = ( MarginLayoutParams) getLayoutParams();
params.height = mScreenHeight * childCount;
for (int i = 0; i <childCount; i+& #43;) {
View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
// Place each ChildView at the specified position
childView.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight);
}
}
}
handle sliding in onTouchEvent()
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastY = ( int) event.getY();
mStart = getScrollY();
return true;
case MotionEvent.ACTION_MOVE:
if (!mScroller.isFinished()) {
// Terminate sliding
mScroller.abortAnimation();
}
int offsetY = (int) (mLastY – event.getY());
Log.d(TAG, “onTouchEvent: getScrollY: ” + getScrollY());
Log.d(TAG, “onTouchEvent: offsetY ” + offsetY);
// Reach the top, use offset to judge direction
if (getScrollY () + offsetY <0) { // The current Y position of the slide
offsetY = 0;
}
// Bottom reached
if (getScrollY() > getHeight() – mScreenHeight && offsetY > 0) {
offsetY = 0;
}
}
scrollBy(0, offsetY);
// After sliding, reset the LastY position
mLastY = (int) event.getY();
break;
case MotionEvent.ACTION_UP:
mEnd = getScrollY();
int distance = mEnd – mStart;
if (distance > 0) { // slide up
if (distance <mScreenHeight / 3) {
Log.d(TAG , “onTouchEvent: distance <screen/3");
// Return to the original position
mScroller.startScroll(0, getScrollY(), 0, -distance);
// p>
} else {
// Scroll to the rest of the screen
mScroller.startScroll(0, getScrollY(), 0, mScreenHeight – distance);
}
} else { // slide down
if (-distance <mScreenHeight / 3) {
mScroller.startScroll(0, getScrollY(), 0, -distance);
} else {
mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight – distance);
p>}
}
postInvalidate();
}
return super.onTouchEvent(event);
p>}
The ACTION_UP code is for elastic sliding
case MotionEvent.ACTION_UP:
mEnd = getScrollY();
mEnd = getScrollY();
p>
int distance = mEnd – mStart;
if (distance > 0) { // slide up
if (distance <mScreenHeight / 3) {
Log.d(TAG, “onTouchEvent: distance <screen/3");
// Return to the original position
mScroller.startScroll(0, getScrollY (), 0, -distance);
} else {
// Scroll to the rest of the screen
mScroller.startScroll(0, getScrollY() , 0, mScreenHeight – distance);
}
} else { // slide down
if (-distance <mScreenHeight / 3) {
} p>
mScroller.startScroll(0, getScrollY(), 0, -distance);
} else {
mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight – distance);
}
}
postInvalidate();
Complete code
public class ScrollViewGroup extends ViewGroup {
private static final String TAG = “ScrollView”;
private Scroller mScroller;
private int mScreenHeight; // window Height
private int mLastY;
private int mStart;
private int mEnd;
public ScrollViewGroup(Context context) {
p>
this(context, null);
}
public ScrollViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mScroller = new Scroller(context);
//Get screen height
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE );
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
mScreenHeight = metrics.heightPixels;
Log.d (TAG, “ScrollViewGroup: ScreenHeight ” + mScreenHeight);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Measure all child Views
int count = getChildCount();
for (int i = 0; i <count; i++) {
View childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
p>
}
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b) {
// Set the height of ViewGroup, arrange all child Views
int childCount = getChildCount();
MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
params.height = mScreenHeight * childCount;
for (int i = 0; i <childCount; i++) {
View childView = getChildAt(i);
if ( childView.getVisibility() != View.GONE) {
// Place each ChildView at the specified position
childView.layout(l, i * mScreenHeight, r , (i + 1) * mScreenHeight);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastY = (int) event.getY();
mStart = getScrollY();
return true;
case MotionEvent.ACTION_MOVE :
if (!mScroller.isFinished()) {
// Terminate sliding
mScroller.abortAnimation();
}
int offsetY = (int) (mLastY – event.getY());
Log.d(TAG, “onTouchEvent: getScrollY: ” + getScrollY ());
Log.d(TAG, “onTouchEvent: offsetY ” + offsetY);
// reach the top, use offset to judge the direction
if (getScrollY() + offsetY <0) { // The current Y position that has been slid
offsetY = 0;
}
// Reach the bottom
if (getScrollY() > getHeight() – mScreenHeight && offsetY > 0) {
offsetY = 0;
}
scrollBy(0, offsetY);
// After sliding, reset the LastY position
mLastY = (int) event .getY();
break;
case MotionEvent.ACTION_UP:
mEnd = getScrollY();
int distance = mEnd – mStart;
if (distance > 0) { // slide up
if (distance <mScreenHeight / 3) {
Log.d(TAG, “onTouchEvent: distance <screen/3");
// Return to the original position
mScroller.startScroll(0, getScrollY(), 0, – distance);
} else {
// Scroll to the rest of the screen
mScroller.startScroll(0, getScrollY(), 0, mScreenHeight – distance );
}
} else { // slide down
if (-distance <mScreenHeight / 3) {
mScroller .startScroll(0, getScrollY(), 0, -distance);
} else {
mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight – distance);
}
}
postInvalidate();
}
return super.onTouchEvent(event);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
}
}
The above is the whole content of this article , I hope it will be helpful to your study,