zoukankan      html  css  js  c++  java
  • 模仿QQ左滑删除

    需求:

    1.左滑删除

    2.向左滑动距离超过一半的时候让它自动滑开,向右滑动超过一半的时候自动隐藏

    3.一次只允许滑开一个item

    还有,根本不需要自定义view来实现,谨防入坑

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scrollbars="none">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">
    
            <LinearLayout
                android:id="@+id/browse_record_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
    
                <ImageView
                    android:id="@+id/browse_record_image"
                    android:layout_width="128dp"
                    android:layout_height="78dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/banner_zhibo" />
    
                <TextView
                    android:id="@+id/browse_record_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="10dp"
                    android:layout_toRightOf="@id/browse_record_image"
                    android:ellipsize="end"
                    android:includeFontPadding="false"
                    android:lineSpacingExtra="0dp"
                    android:lines="2"
                    android:text="空中读书会"
                    android:textColor="@color/c32"
                    android:textSize="15sp" />
            </LinearLayout>
    
            <TextView
                android:id="@+id/browse_delete"
                android:layout_width="65dp"
                android:layout_height="match_parent"
                android:background="@color/red"
                android:gravity="center"
                android:text="删除"
                android:textColor="@color/white"
                android:textSize="16sp" />
    
        </LinearLayout>
    
    </HorizontalScrollView>

    adapter的getview中:

    //隐藏删除按钮
    ViewGroup.LayoutParams params = holder.browse_record_layout.getLayoutParams();
    int screenW = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
    params.width = screenW;
    
    //自动滑开和自动隐藏效果
    holder.scrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    // 手抬起的时候判断滑动距离
                    int slideDistance = holder.scrollView.getScrollX();// 获取向左滑动的距离,是一个非负数,px
                    int delete = holder.browse_delete.getWidth();// 删除按钮的宽度,单位px
                    // 当滑动距离大于删除按钮宽度的一半时,就自动滑动到最左边,完全显示删除按钮,所谓的最左边就是滑动距离等于删除按钮宽度
                    // 当滑动距离小于删除按钮宽度的一半时,就隐藏删除按钮,即滑动距离等于0的位置
                    if (slideDistance >= delete / 2) {
                        holder.scrollView.scrollTo(delete, 0);
                        currentSlideView = holder.scrollView;// 保存当前滑开的item
                    } else {
                        holder.scrollView.scrollTo(0, 0);
                        currentSlideView = null;// 清空
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 滑动的时候判断当前有没有其它滑开的item,有的话就隐藏
                    if (currentSlideView != holder.scrollView) {
                        autoHide();
                    }
                    break;
                default:
                    break;
            }
    
            return true;
        }
    });
    
    //删除
    holder.browse_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //删除一条记录
            list.remove(position);
            autoHide();
            notifyDataSetChanged();
        }
    });

     

  • 相关阅读:
    Pandas DataFrame操作
    Pandas Series数据结构基本操作
    Locally weighted regression algorithm
    Python Numpy线性代数操作
    Python Numpy 矩阵级基本操作(2)
    分布式锁1 Java常用技术方案(转)
    设计模式
    设计模式
    设计模式
    Lock和synchronized比较详解(转)
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5900818.html
Copyright © 2011-2022 走看看