Add damping to ScrollView, but how does one add damping to ListView?

Source: Internet
Author: User

During the project, he slide a lot in a long ScrollView according to the customer's needs. He wants to make the ScrollView slide slowly, adds damping.

Although I did not study it, I also studied how to add damping to ListView. I didn't understand it. There are still many problems when using ListView and ScrollView together, so I didn't know it, if you have any experts, you can give me a answer.

The following describes how to implement ScrollView:

Package com. text. ui. widget; import android. content. context; import android. util. attributeSet; import android. widget. scrollView;/*** fast/slow sliding ScrollView **/public class SlowScrollView extends ScrollView {public SlowScrollView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public SlowScrollView (Context context, AttributeSet attrs) {super (context, attrs);} public SlowScrollView (Context context) {super (context );} @ Overridepublic void fling (int velocityY) {super. fling (velocityY/4 );}}


Define a ScrollView class by yourself, override the fling method, and change the increase or decrease of velocityY to a slow or multiple to accelerate when implementing the parent class method. The damping is added.

I am also trying to use Listview. One method is to use the listview method to overwrite and change the increase or decrease margin. (I don't know how it works ?)

package com.test.ui.widget;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.ListView;import android.widget.Scroller;/** *  *  * @author over1 *  */public class SerarchListView extends ListView {public mScroller mscroller;private Context context;public class mScroller extends Scroller {public mScroller(Context context) {super(context);}@Overridepublic void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) {// TODO Auto-generated method stubsuper.fling(startX, startY, velocityX, velocityY / 4, minX, maxX, minY, maxY);}}public SerarchListView(Context context) {super(context);this.context = context;}public SerarchListView(Context context, AttributeSet attrs) {super(context, attrs);}public SerarchListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic void onScrollChanged(int l, int t, int oldl, int oldt) {// TODO Auto-generated method stubsuper.scrollBy(l, t);super.scrollTo(((oldl - l) / 10 + l), ((oldt - t) / 10 + t));super.onScrollChanged(l, t, ((oldl - l) / 10 + l), ((oldt - t) / 10 + t));}@Overridepublic boolean onTouchEvent(MotionEvent ev) {int act = ev.getAction();if (act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL) {//scroll back  }//mscroller = new mScroller(context);//if (mscroller.computeScrollOffset()) {//scrollTo(mscroller.getCurrX(), mscroller.getCurrY() + (mscroller.getFinalY() - mscroller.getCurrY()) / 4);//postInvalidate();//}return super.onTouchEvent(ev);}}


Another method is to add a listener to the listview object: (use a Layout)

Mscroller = new mScroller (this); lay = (LinearLayout) findViewById (R. id. listslow); lay = new LinearLayout (this) {@ Overridepublic void addView (View child) {// TODO Auto-generated method stubLinearLayout lay1 = (LinearLayout) findViewById (R. id. listslow); super. addView (lay1) ;}@ Overridepublic void computeScroll () {scrollTo (mscroller. getCurrX (), mscroller. getCurrY () + (mscroller. getFinalY ()-mscroller. getCurrY ()/100) ;}; // mListView. setOnScrollListener (new ListView. onScrollListener () {// @ Override // public void onScrollStateChanged (AbsListView paramAbsListView, int paramInt) {// 0 when the screen stops scrolling; 1 When the screen is rolling and the user's touch or finger is still on the screen; // due to the user's operation, the screen produces inertial slide for 2 // switch (paramInt) {// case AbsListView. onScrollListener. SCROLL_STATE_IDLE: // mBusy = false; // break; // case AbsListView. onScrollListener. SCROLL_STATE_TOUCH_SCROLL: // mBusy = true; // break; // case AbsListView. onScrollListener. SCROLL_STATE_FLING: // mBusy = true; // this. onScroll (paramAbsListView, mListView. getLastVisiblePosition (), (mListView //. getLastVisiblePosition ()-mListView. getFirstVisiblePosition () // + mListView. getLastVisiblePosition (), mListView. getCount (); // break; //} // @ Override // public void onScroll (AbsListView paramAbsListView, int firstVisibleItem, int visibleItemCount, // int totalItemCount) {//}); load ();} public class mScroller extends Scroller {public mScroller (Context context) {super (context );} @ Overridepublic void fling (int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) {// TODO Auto-generated method stubsuper. fling (startX, startY, velocityX, velocityY/4, minX, maxX, minY, maxY );}}

What else can I ask?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.