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 customize android to scroll up and down and rebound scollView

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "android how to customize scroll up and down rebound scollView". In daily operation, I believe many people have doubts about how to customize android scroll up and down rebound scollView. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "android how to customize scroll up and down rebound scollView". Next, please follow the editor to study!

This is a custom view, using this view to nest the view that you want to bounce back up and down in the xml layout

The layout can be scrolled and bounced up and down. The custom view code is as follows:

Package com.loopfire.meitaotao.view.scrollView; import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.TranslateAnimation;import android.widget.ScrollView; / * bounce scollView * * @ author Administrator * * / public class MyScrollView extends ScrollView {private View inner; private float y Private Rect normal = new Rect (); private boolean animationFinish = true; public MyScrollView (Context context) {super (context);} public MyScrollView (Context context, AttributeSet attrs) {super (context, attrs);} @ Override protected void onFinishInflate () {if (getChildCount () > 0) {inner = getChildAt (0) } @ Override public boolean onInterceptTouchEvent (MotionEvent ev) {return super.onInterceptTouchEvent (ev);} @ Override public boolean onTouchEvent (MotionEvent ev) {if (inner = = null) {return super.onTouchEvent (ev);} else {commOnTouchEvent (ev);} return super.onTouchEvent (ev) } private void commOnTouchEvent (MotionEvent ev) {if (animationFinish) {int action = ev.getAction (); switch (action) {case MotionEvent.ACTION_DOWN: / / System.out.println ("ACTION_DOWN"); y = ev.getY (); super.onTouchEvent (ev); break Case MotionEvent.ACTION_UP: / / System.out.println ("ACTION_UP"); y = 0; if (isNeedAnimation ()) {animation ();} super.onTouchEvent (ev); break Case MotionEvent.ACTION_MOVE: / / System.out.println ("ACTION_MOVE"); final float preY = y = 0? Ev.getY (): y; float nowY = ev.getY (); int deltaY = (int) (preY-nowY); / / scroll / / scrollBy (0, deltaY); y = nowY / / when you scroll to the top or bottom, it will not scroll again. Then move the layout if (isNeedMove ()) {if (normal.isEmpty ()) {/ / save the normal layout location normal.set (inner.getLeft (), inner.getTop (), inner.getRight (), inner.getBottom () } / / Mobile layout inner.layout (inner.getLeft (), inner.getTop ()-deltaY / 2, inner.getRight (), inner.getBottom ()-deltaY / 2);} else {super.onTouchEvent (ev);} break Default: break;} / / enable Animation Mobile private void animation () {/ / enable Mobile Animation TranslateAnimation ta = new TranslateAnimation (0,0,0, normal.top-inner.getTop ()); ta.setDuration Ta.setAnimationListener (new AnimationListener () {@ Override public void onAnimationStart (Animation animation) {animationFinish = false;} @ Override public void onAnimationRepeat (Animation animation) {} @ Override public void onAnimationEnd (Animation animation) {inner.clearAnimation () / / set back to the normal layout location inner.layout (normal.left, normal.top, normal.right, normal.bottom); normal.setEmpty (); animationFinish = true;}}); inner.startAnimation (ta) } / / whether to open the animation private boolean isNeedAnimation () {return! normal.isEmpty ();} / / whether to move the layout private boolean isNeedMove () {int offset = inner.getMeasuredHeight ()-getHeight (); int scrollY = getScrollY (); if (scrollY = = 0 | | scrollY = = offset) {return true;} return false }}

The use in xml is as follows:

Then the included textview can scroll up and down and bounce back.

At this point, the study of "how to customize android to scroll up and down and bounce scollView" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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