Android ListView is pulled to the top/bottom, resilience and reset like a rubber band, androidlistview

Source: Internet
Author: User
Android ListView is pulled to the top/bottom, resilience and reset like a rubber band, androidlistview


"Android ListView is pulled to the top/bottom, resilience and reset like a rubber band"

When the Android ListView is pulled to the top or bottom, a "flashing" effect will appear in the gap between the top/bottom edges, indicating that the ListView has reached the top/bottom and can no longer move.
 This is an interactive design where the Android native ListView is pulled to the top/bottom. There are many options for interaction design. Android 5.0 changed the interactive design of ListView into "a rippling halo".
Among the many interactive design schemes, one of the interactive design is like this: When the ListView is pulled to the top or bottom, the ListView will be like a rubber band, and the ListView will show a damping effect. When the user releases the ListView, the ListView will automatically be like a rubber band. 
 To achieve the above-mentioned interactive effect on Android, the method overScrollBy() can be achieved by rewriting ListView.
Now give an example, step by step implementation.
First, I need to write a ZhangPhilListView inherited from Android 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 distance that the ListView can be pulled out of the top or bottom.
private static final int MAX_OVERSCROLL_Y = 200;

private Context mContext;
private int newMaxOverScrollY;

public ZhangPhilListView(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); this.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 needs to add @SuppressLint("NewApi").
@SuppressLint("NewApi")
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, 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 using Android's ListView:

<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 {

@Override
protected 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 statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.
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.