listView的item向左滑动实现删除

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

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();


截图


  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值