ANDROID development-BOUNCELISTVIEW similar to IPHONE Elasticity

Source: Internet
Author: User

I continued to look into Android's new Overscroll functionality introduced in Gingerbread and discovered some more interesting things. the functionality to make a view scroll beyond its limits and then bounce back (almost exactly like iOS) is sort of built
Into the framework, but just hidden. i'm not sure exactly why it has been built like it has been, but I will give a few guesses after an explanation of what is actually going on, but first: DEMO!

So What's There and What isn' t?

I'm gglad you asked... If we look into the ViewConfiguration class 'source we
Find two variables of interest: OVERSCROLL_DISTANCE and OVERFLING_DISTANCE. these two variables tell the framework how much a view shocould be able to scroll beyond its limits. they are hard coded in and there are no methods available to set your own m
Ones. OVERSCROLL_DISTANCE is set to 0 (!?) And OVERFLING_DISTANCE is set to 4.

For those that don't know, the ViewConfiguration class holds a set of values that
Android uses to store the default timeouts/distances etc for certain UI behaviours. it also does some internal scaling and calculations based on screen density etc. if you're interested, have a look at the source

So with OVERSCROLL_DISTANCE set to 0, the view will never move beyond its limits, but you can do something fairly simple to achieve this behaviour.

In complicated terms, just extend the view you wish to use normally, (e.g. ListView) and override the overScrollBy method.
Then within theoverScrollBy method
Body, simply call the super. overScrollBy but with your own values for maxOverScrollX and/or maxOverScrollY. If you're gonna do this, make sure you scale your values based on the screen density.

Confused? Have a code sample:

And now just use that custom view wherever you wocould normally have used the standard view!

import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.widget.ListView;public class BounceListView extends ListView{    private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;        private Context mContext;private int mMaxYOverscrollDistance;public BounceListView(Context context){super(context);mContext = context;initBounceListView();}public BounceListView(Context context, AttributeSet attrs){super(context, attrs);mContext = context;initBounceListView();}public BounceListView(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);mContext = context;initBounceListView();}private void initBounceListView(){//get the density of the screen and do some maths with it on the max overscroll distance//variable so that you get similar behaviors no matter what the screen sizefinal DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();        final float density = metrics.density;        mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);}@Overrideprotected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent){ //This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance; return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);  }}

Import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; public class BounceListViewActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); BounceListView mBounceLv = (BounceListView) findViewById (R. id. list); mBounceLv. setAdapter (new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, getData ();} private List <String> getData () {List <String> data = new ArrayList <String> (); data. add ("test data 1"); data. add ("Test data 2"); data. add ("Test data 3"); data. add ("test data 4"); data. add ("test data 5"); data. add ("test data 6"); data. add ("test data 7"); data. add ("test data 8"); data. add ("Test data 9"); data. add ("Test data 10"); data. add ("Test data 11"); data. add ("Test data 12"); data. add ("Test data 13"); return data ;}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    />    <com.thinkfeed.bouncelistview.BounceListView        android:id="@+id/list"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        /></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.thinkfeed.bouncelistview"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="10" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".BounceListViewActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

As promised, some guesses as to why this is happening. My first thought is that the developers had no intention of exposing this functionality and that
It is simply meant to be used for the small springback you get after an overfling (remember where OVERFLING_DISTANCE was set to 4 ). but then is that was the case why set OVERSCROLL_DISTANCE to 0, why not just not include it if that was the case? Maybe they
Are planning something in the future? But if it was intended to be used, then why not create methods that let you set the overscroll distances for your views? Who knows...

From: http://www.cnblogs.com/thinkfeed/archive/2011/09/19/2181042.html

Related Article

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.