Android ListView pulls to the top/bottom, like a rubber band resilient rebound reset

Source: Internet
Author: User



"Android ListView pulls to the top/bottom, like a rubber band resilient rebound reset"

The android itself's ListView pulls to the top or bottom will have a "flash" effect on the top/bottom edge gap, suggesting that the ListView is already at the top/bottom and cannot be moved anymore.
This is an interactive design that Android native ListView pulls to the top/bottom. There are many options for interactive design. Android 5.0 transforms this interactive design of the ListView into a "rippling halo".
Among the many interactive designs, one interaction design is this: When the ListView pulls to the top or bottom, the ListView will look like a rubber band, and the ListView has a damping effect, and when the user releases the ListView, The ListView automatically bounces back to the top/bottom as elastic as a rubber band.
This interactive effect is achieved on Android, and the Method Overscrollby () that overrides the ListView can be implemented.
Now give an example, step by step implementation.
First, I need to write a zhangphillistview that inherits from Android's ListView:

Package Zhangphil.listview;import Android.annotation.suppresslint;import Android.content.context;import Android.util.attributeset;import Android.util.displaymetrics;import Android.widget.listview;public Class Zhangphillistview extends ListView {//This value control can pull the ListView out of the distance from the top or bottom. private static final int max_overscroll_y = 200;private Context mcontext;private int newmaxoverscrolly;public Zhangphilli Stview (Context context) {super (context); this.mcontext = Context;init ();} Public Zhangphillistview (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext = Context;init ();} /* Public Zhangphillistview (context context, AttributeSet attrs, int * defstyle) {Super (context, attrs, Defstyle); . Mcontext = Context; * Init (); } */private void init () {Displaymetrics metrics = mcontext.getresources (). Getdisplaymetrics (); float density = metrics.density;newmaxoverscrolly = (int) (density * max_overscroll_y);} The most critical place. Support to SDK8 need to add @suppresslint ("Newapi"). @SuppressLint ("Newapi") @Overrideprotected boolean Overscrollby (int deltax, int deltay, int scrollx,int scrolly, int scrollrangex, int scrollrangey,int ma XOVERSCROLLX, int maxoverscrolly, Boolean istouchevent) {return Super.overscrollby (DeltaX, DeltaY, Scrollx, ScrollY, Scrollrangex, Scrollrangey, MAXOVERSCROLLX, newmaxoverscrolly,istouchevent);}}


Then use it directly in my layout file activity_main.xml like a ListView using Android:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:orientation=" vertical ">        < Zhangphil.listview.ZhangPhilListView        android:id= "@+id/listview"        android:layout_width= "Match_parent"        android:layout_height= "match_parent"/>    </LinearLayout>


Test:

Package Zhangphil.listview;import Android.support.v7.app.actionbaractivity;import Android.widget.ArrayAdapter; Import Android.widget.listview;import Android.os.bundle;public class Mainactivity extends Actionbaractivity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); ListView LV = (ListView) Findviewbyid (R.id.listview);//test data set. string[] data = new String[50];for (int i = 0; i < data.length; i++) {Data[i] = i + "";} arrayadapter<string> adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1, Android. R.ID.TEXT1, data); Lv.setadapter (adapter);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android ListView pulls to the top/bottom, like a rubber band resilient rebound reset

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.