Android超簡單實現listview上下拉伸回彈動畫效果
阿新 • • 發佈:2019-02-05
超簡單實現listview上下拉伸回彈動畫效果
希望能幫到大家,共同進步
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.ListView;
public class MyListView extends ListView {
private Context mContext;
private int mMaxYOverscrllDistance;
//回彈距離
private int MAX_Y = 200;
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
initBounceListView();
}
public MyListView(Context context, AttributeSet attrs) {
super (context, attrs);
this.mContext = context;
initBounceListView();
}
public MyListView(Context context) {
super(context);
this.mContext = context;
initBounceListView();
}
private void initBounceListView(){
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
float desity = metrics.density;
//允許超出邊界高度的最大距離
mMaxYOverscrllDistance = (int) (desity*MAX_Y);
}
@SuppressLint("NewApi")
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// 當listview滑動超出範圍的時候,回撥該方法,並提供引數做處理
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
scrollRangeY, maxOverScrollX, mMaxYOverscrllDistance, isTouchEvent);
}
}
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(linearLayout);
MyListView listview = new MyListView(this);
//設定上拉的顯示圖片
listview.setOverscrollHeader(getResources().getDrawable(R.drawable.ic_launcher));
linearLayout.addView(listview, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
new String[]{
"a","b","c","d","e","f","g","h","l","n","a","b","c","d","e","f","g","h","l","n"
});
listview.setAdapter(adapter);
}
-就是這麼簡單,就是這麼任性實現listview上下拉伸回彈效果