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 is the interactive design: When the ListView pulls to the top or bottom, the ListView will be like a rubber band. The ListView renders a damping effect. When the user releases the ListView, the ListView self-actively bounces like a rubber band back to the top/bottom reset.
This interactive effect is implemented on Android, and the Method Overscrollby () that overrides the ListView can be implemented.
A demonstration example is given, which is implemented step by step.


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 controls the ability to pull the ListView away from the top or bottom distance. 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);}}



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

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.