listView的item向左滑动实现删除

  • Post author:
  • Post category:其他

有一种删除功能是这样的:

ListView的item向左滑动,实现删除功能

注释很清楚,简单明了

1.自定义LeftDeleteView.java继承HorizontalScrollView.java

public class LeftDeleteView extends HorizontalScrollView {

    private int start;//开始滑动的位置
    private int end;//结束滑动的位置
    private VelocityTracker velocityTracker;

    public LeftDeleteView(Context context) {
        super(context);
    }

    public LeftDeleteView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LeftDeleteView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                start = (int) event.getX();//得到相对于屏幕左上角的x坐标值
                if (velocityTracker == null) {
                    velocityTracker = VelocityTracker.obtain();//获得VelocityTracker类实例
                }
                break;
            case MotionEvent.ACTION_UP:
                end = (int) event.getX();
                int width = 140;
                if (start > end) {//如果手指按下的X坐标大于手指抬起的X坐标
                    if (getScrollX() > width / 2 //得到x轴的按下和抬起的距离
                            || velocityTracker.getXVelocity() > 600) {//获得横向滑动的速度
                            // 注意一下:但是使用getXVelocity()之前请先调用computeCurrentVelocity(int)来初始化速率的单位
                            // 这里留下一个疑问:velocityTracker.getXVelocity()得到的是0.0????请高手指教
                        smoothScrollTo(width, 0);
                    } else {
                        smoothScrollTo(0, 0);
                    }
                }
                if (start < end) {
                    if (getScrollX() < width / 2 || velocityTracker.getXVelocity() > 600) {
                        smoothScrollTo(width, 0);
                    } else {
                        smoothScrollTo(0, 0);
                    }
                }
                velocityTracker.clear();
                break;
            case MotionEvent.ACTION_MOVE:
                velocityTracker.addMovement(event);//将滑动事件加入到VelocityTracker类实例中
                break;
        }
        return super.onTouchEvent(event);
    }
}

2.布局:LeftDeleteView.xml在布局的跟布局使用自定义的

<?xml version="1.0" encoding="utf-8"?>
<com.example.administrator.myapplication.LeftDeleteView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f1f1f1"
    android:orientation="vertical"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <View
                android:id="@+id/view_bg"
                android:layout_width="360dp"
                android:layout_height="80dp"
                android:background="#ffffff"
                />

            <TextView
                android:id="@+id/tv_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Hello World!"/>
        </FrameLayout>

        <TextView
            android:id="@+id/text_delete"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="#ff0000"
            android:gravity="center"
            android:text="删除"
            android:textColor="#ffffff"
            android:textSize="35px"/>
    </LinearLayout>
</com.example.administrator.myapplication.LeftDeleteView>

3.在你想用这个布局的地方。得到布局,通过布局再findViewById找到控件,就可以使用了。

4.删除ListView的item控件这个功能的实现,当你在BaseAdapter得到布局后,可以在Activity里写一个public的删除ListView数据的方法

5.您在BaseAdapter构造方法得到的有context变量,通过context调用您刚刚在activity里写的删除ListView数据的方法

6.刷新数据     adapter.notifyDataSetChanged();

截图


版权声明:本文为u012123938原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。