Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize scrollview that can bounce up and down by android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The main content of this article is to explain "android how to achieve scrollview that can be bounced up and down". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how android can achieve scrollview that can be bounced up and down".

1. Create a new MyScrollView and inherit ScrollView. You can intercept and handle sliding events through the event distribution mechanism.

two。 Override the event distribution intercept event onInterceptTouchEvent method to calculate whether to intercept events

/ / intercept: whether the parent view intercepts the child view successfully or not depends on the return value of the method. Return value true: blocked successfully. On the contrary, the coordinates of the last y-axis operation of the failed private int lastY;// private Rect normal = new Rect (); / / whether the left, upper, right and lower private boolean isFinishAnimation = true;// of the critical state is animated to end private int lastX, downX, downY; @ Override public boolean onInterceptTouchEvent (MotionEvent ev) {boolean isIntercept = false; int eventX = (int) ev.getX () Int eventY = (int) ev.getY (); switch (ev.getAction ()) {case MotionEvent.ACTION_DOWN: lastX = downX = eventX; lastY = downY = eventY; break Case MotionEvent.ACTION_MOVE: / / get horizontal and vertical movement distance int absX = Math.abs (eventX-downX); int absY = Math.abs (eventY-downY); if (absY > absX & & absY > = dp2px (10)) {isIntercept = true / / execute intercept} lastX = eventX; lastY = eventY; break;} return isIntercept;}

3. Get the sub-view of scrollview for easy operation

/ / get the child view @ Override protected void onFinishInflate () {super.onFinishInflate (); if (getChildCount () > 0) {childView = getChildAt (0);}}

4. Calculate whether the translation animation is required

Private boolean isNeedMove () {int childMeasuredHeight = childView.getMeasuredHeight (); / / get the height of the child view int scrollViewMeasuredHeight = this.getMeasuredHeight (); / / get the height of the layout Log.e ("TAG", "childMeasuredHeight =" + childMeasuredHeight); Log.e ("TAG", "scrollViewMeasuredHeight =" + scrollViewMeasuredHeight); int dy = childMeasuredHeight-scrollViewMeasuredHeight;//dy > = 0 int scrollY = this.getScrollY () / / get the user's offset in the y-axis direction (top + bottom -) if (scrollY = dy) {return true;// handles} / / others in the critical range according to our custom MyScrollView, return false. That is to say, return false;} is still handled in the same way as ScrollView.

5. Determine whether you need to pan the animation

/ / determine whether translation animation private boolean isNeedAnimation () {return! normal.isEmpty ();} needs to be performed.

6. Now that we have done the event interception, we need to rewrite the ontouchevent to respond to the event

@ Override public boolean onTouchEvent (MotionEvent ev) {if (childView = = null | |! isFinishAnimation) {return super.onTouchEvent (ev);} int eventY = (int) ev.getY (); / / get the current y-axis coordinate switch (ev.getAction ()) {case MotionEvent.ACTION_DOWN: lastY = eventY; break Case MotionEvent.ACTION_MOVE: int dy = eventY-lastY / / small amount of movement if (isNeedMove ()) {if (normal.isEmpty ()) {/ / records the left, upper, right and lower normal.set of the critical state of childView (childView.getLeft (), childView.getTop (), childView.getRight (), childView.getBottom ()) } / / relayout childView.layout (childView.getLeft (), childView.getTop () + dy / 2, childView.getRight (), childView.getBottom () + dy / 2);} lastY = eventY;// reassign break Case MotionEvent.ACTION_UP: if (isNeedAnimation ()) {/ / use the translation animation int translateY = childView.getBottom ()-normal.bottom; TranslateAnimation translateAnimation = new TranslateAnimation (0,0,0,-translateY); translateAnimation.setDuration (200C); / / translateAnimation.setFillAfter (true) / / stay at the final position translateAnimation.setAnimationListener (new Animation.AnimationListener () {@ Override public void onAnimationStart (Animation animation) {isFinishAnimation = false) } @ Override public void onAnimationEnd (Animation animation) {isFinishAnimation = true; childView.clearAnimation () / / clear animation / / relayout childView.layout (normal.left, normal.top, normal.right, normal.bottom); / / clear normal data normal.setEmpty () } @ Override public void onAnimationRepeat (Animation animation) {}}); / / start the animation childView.startAnimation (translateAnimation);} break } return super.onTouchEvent (ev);}

In this way, the core part of the whole view has been completed, and the view can be nested into the defined scrollview to achieve the sliding rebound effect of the page.

At this point, I believe you have a deeper understanding of "android how to achieve scrollview that can be bounced up and down". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report