前言
根據(jù)啟艦大大 的博客所學習的滑動刪除。
簡介
Scoller類是scrollTo的補充,他沒有scrollTo的功能,它的功能就是根據(jù)傳進去的參數(shù)計算位置的,只是計算,并不會去移動位置,如果要移動還是要調(diào)用scrollTo的,這里只是為了讓滑動更加的流暢一點。
相關函數(shù)
- 構造函數(shù)
public Scroller (Context context)
public Scroller (Context context, Interpolator interpolator)
-interpolator 插值器,可以參考這里
- startScroll方法
public void startScroll(int startX, int startY, int dx, int dy, int duration)
public void startScroll(int startX, int startY, int dx, int dy)
- startX :開始移動的X坐標
- startY: 開始移動的Y坐標
- dx: 沿X軸移動坐標,為正時,子控件左移,為負時,子控件右移。
- dy:沿Y軸移動坐標,為正時,子控件上移,為負時,子控件下移。
- duration: 整個移動過程,所耗費時長
第二個構造函數(shù)中,沒有duration這個參數(shù),系統(tǒng)會使用默認的時長:250毫秒
舉個栗子:
mScroller = new Scroller(context,new LinearInterpolator(context,null));
mScroller.startScroll(0,0,200,0,500);
這個代碼就是說用500毫秒的時間從(0,0)的位置,沿X軸反方向移動200,Y軸不動;
但這個函數(shù)并不會移動,而是模擬計算,調(diào)用了這個函數(shù)之后,它就會在scroller內(nèi)部用一個線程來計算從(0,0)的位置,沿X軸反方向移動200,Y軸不動;每一毫秒的位置,用戶可以通過scroller.getCurrX()、scroller.getCurrY()來獲取,當前應該在的位置。注意,我用的“應該”。因為scroller只是根據(jù)插值器,指定的時間,距離;算出當前所在的X軸坐標,Y軸坐標。但對圖像并沒有做任何操作!!!!!!要想移動圖像,就必須使用scrollTo()!!!
簡易的小左滑,完善滑動,讓其更加自然,會面會繼續(xù)完善的哦~
代碼如下:
public class DelView extends LinearLayout {
private Context context;
Scroller scroller;
int delWidth;
public DelView(Context context) {
this(context, null);
}
public DelView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initScroll();
}
private void initScroll() {
scroller = new Scroller(context, new LinearInterpolator(context, null));
}
/**
* 獲取刪除view的長度
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (getChildCount() > 1) {
View del = getChildAt(1);
measureChild(del, widthMeasureSpec, heightMeasureSpec);
delWidth = del.getMeasuredWidth();
}
}
int lastX;
@Override
public boolean onTouchEvent(MotionEvent event) {
int scrollX = getScrollX();
int newScrooll;
if (event.getAction() == MotionEvent.ACTION_MOVE) {
newScrooll = (int) (scrollX + lastX - event.getX());
if (newScrooll < 0) newScrooll = 0;
else if (newScrooll > delWidth) newScrooll = delWidth;
scrollTo(newScrooll, 0);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
if (scrollX > delWidth / 2)
newScrooll = delWidth;
else
newScrooll = 0;
scroller.startScroll(scrollX, 0, newScrooll - scrollX, 0);
//用invalidate()來重繪,由于我們這里有Scroller,所以在重繪時就會走到VIEW的computeScroll()函數(shù)
invalidate();
}
lastX = (int) event.getX();
return true;
}
@Override
public void computeScroll() {
//computeScrollOffset()來判斷當前Scroller的狀態(tài)——是否已經(jīng)計算移動位置結束,
// 如果結束返回FALSE,如果還在計算就返回TRUE
if (scroller.computeScrollOffset()) {
scrollTo(scroller.getCurrX(), scroller.getCurrY());
//如果仍在計算,就調(diào)用invalidate,使之重新進入computeScroll方法
invalidate();
}
}
}
<com.crossroads.demo.demo.DelView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:id="@+id/itemroot"
>
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="ITEM"
/>
<TextView
android:id="@+id/del"
android:layout_width="60dp"
android:layout_height="100dp"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:text="刪除"
/>
</com.crossroads.demo.demo.DelView>
del = findViewById(R.id.del);
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "刪除", Toast.LENGTH_SHORT).show();
}
});