BounceListViewActivity.java
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import com.example.bouncelistview.BounceListView;
public class BounceListViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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("abc");
data.add("dsdsd");
data.add("dsds");
data.add("dff");
data.add("hghg");
data.add("jhj");
data.add("kjk");
data.add("abrerec");
data.add("afdfbc");
data.add("fdf");
data.add("abdsdc");
data.add("sds");
data.add("wewe");
data.add("sfsf");
data.add("dfggrfg");
data.add("fdfdvdvd");
data.add("vdvdv");
data.add("abdvdvc");
data.add("abdvdvc");
data.add("dvdvdv");
data.add("bbbfb");
data.add("avdvdvbc");
data.add("vdvdv");
return data;
}
}
BounceListView.java
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
// size
final DisplayMetrics metrics = mContext.getResources()
.getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}
@Override
protected 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);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<com.example.bouncelistview.BounceListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
No comments:
Post a Comment