QQ侧滑删除SwipeLayout

3步骤第一步 SwipLayout代码
package itcas.com.myapplication.swipelayout;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;

/**
 * Created by fullcircle on 2018/3/1.
 */

public class SwipeLayout extends FrameLayout {

    private View contentView;
    private View deleteView;
    private ViewDragHelper dragHelper;
    private float downY;
    private float downX;
    private long startTime;
    private long downTime;

    public SwipeLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dragHelper = ViewDragHelper.create(this, new MyCallback());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentView = getChildAt(0);
        deleteView = getChildAt(1);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        contentView.layout(0,0,contentView.getMeasuredWidth(),contentView.getMeasuredHeight());
        deleteView.layout(contentView.getRight(),0,contentView.getRight()+deleteView.getMeasuredWidth(),deleteView.getMeasuredHeight());
    }

    private class MyCallback extends ViewDragHelper.Callback {
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return true;
        }
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if(child == contentView){
                if(left>0){
                    left = 0;
                }else if(left<-deleteView.getMeasuredWidth()){
                    left = -deleteView.getMeasuredWidth();
                }
            } else if(child == deleteView){
                if(left>contentView.getMeasuredWidth()){
                    left = contentView.getMeasuredHeight();
                }else if(left <(contentView.getMeasuredWidth()-deleteView.getMeasuredWidth())){
                    left = contentView.getMeasuredWidth()-deleteView.getMeasuredWidth();
                }
            }
            return left;
        }
        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            if(changedView == contentView){
                //deleteView.layout(contentView.getRight(),0,contentView.getRight()+deleteView.getMeasuredWidth(),deleteView.getMeasuredHeight());
                ViewCompat.offsetLeftAndRight(deleteView,dx);
            }else if(changedView == deleteView){
               // contentView.layout(deleteView.getLeft()-contentView.getMeasuredWidth(),0,deleteView.getLeft(),contentView.getMeasuredHeight());
                ViewCompat.offsetLeftAndRight(contentView,dx);
            }
            if(listener!=null){
                if(contentView.getLeft()==0){
                    listener.onClose(SwipeLayout.this);
                }else if(contentView.getLeft() == -deleteView.getMeasuredWidth()){
                    listener.onOpen(SwipeLayout.this);
                }
            }

        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            int left = contentView.getLeft();
            if(left<-deleteView.getMeasuredWidth()/2){
                openLayout();
            }else if(left>=-deleteView.getMeasuredWidth()/2){
                closeLayout();
            }
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return 1;
        }
    }

    public void closeLayout() {
        dragHelper.smoothSlideViewTo(contentView,0,0);
        ViewCompat.postInvalidateOnAnimation(this);
    }

    private void openLayout() {
        dragHelper.smoothSlideViewTo(contentView,-deleteView.getMeasuredWidth(),0);
        ViewCompat.postInvalidateOnAnimation(this);
    }

    @Override
    public void computeScroll() {
        if(dragHelper.continueSettling(true)){
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downY = event.getY();
                downX = event.getX();
                //记住按下的时间
                downTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(downX - event.getX());
                float dy = Math.abs(downY - event.getY());
                if(dx>dy){
                    getParent().requestDisallowInterceptTouchEvent(true);
                }

                break;
            case MotionEvent.ACTION_UP:
                //计算按下的时间 和 抬起的时间 的时间差 如果这段时间足够短就认为是一个点击事件
                long time = System.currentTimeMillis() - downTime;
                //计算移动的距离
                double tempX = Math.pow(downX - event.getX(), 2);
                double tempY = Math.pow(downY - event.getY(), 2);
                double distance = Math.sqrt(tempX + tempY);

                ViewConfiguration viewConfiguration = ViewConfiguration.get(getContext());
                if(time<ViewConfiguration.getLongPressTimeout()&& distance<viewConfiguration.getScaledTouchSlop()){
                   performClick();
                }
                break;
        }
        dragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = dragHelper.shouldInterceptTouchEvent(ev);
        return result;
    }

    public interface SwipeListener{
        void onOpen(SwipeLayout swipeLayout);
        void onClose(SwipeLayout swipeLayout);
    }

    private SwipeListener listener;

    public void setListener(SwipeListener listenr) {
        this.listener = listenr;
    }
}
3步骤第二步 ListView中item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.itheima.swipelayout.SwipeLayout
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/layout_content"/>
        <include layout="@layout/layout_delete"/>
    </com.itheima.swipelayout.SwipeLayout>

</LinearLayout>

content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:paddingLeft="15dp"
    android:background="#33666666"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    
    <ImageView android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@mipmap/head"/>
    
    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#99000000"
        android:id="@+id/tv_name"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:text="名称"/>

</LinearLayout>

delete

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:orientation="horizontal" >

    <TextView android:layout_width="100dp"
        android:layout_height="match_parent"
        android:textSize="18sp"
        android:textColor="#ffffff"
        android:gravity="center"
        android:background="#aa000000"
        android:text="Call"/>
    
    <TextView android:layout_width="100dp"
        android:layout_height="match_parent"
        android:textSize="18sp"
        android:textColor="#ffffff"
        android:id="@+id/tv_delete"
        android:gravity="center"
        android:background="#eeff0000"
        android:text="Delete"/>

</LinearLayout>

main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.itheima.swipelayout.MainActivity">

   <ListView
       android:id="@+id/list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:divider="@color/colorPrimary"
       android:dividerHeight="1dp"/>


</RelativeLayout>

3步骤第三步 使用
public class MainActivity extends AppCompatActivity {
    private SwipeLayout currentLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView list  = (ListView) findViewById(R.id.list);
        MyAdatper adatper = new MyAdatper();
        list.setAdapter(adatper);
        list.setOnScrollListener(new AbsListView.OnScrollListener() { //listView添加滚动监听,滚动时候关闭
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(currentLayout!=null){
                    currentLayout.closeLayout();
                    currentLayout = null;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

    }

    private class MyAdatper extends BaseAdapter{

        @Override
        public int getCount() {
            return Constant.NAMES.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = null;
            if(convertView == null){
                view = View.inflate(getApplicationContext(), R.layout.item, null);
            }else{
                view = convertView;
            }
            TextView textView = (TextView) view.findViewById(R.id.tv_name);
            textView.setText(Constant.NAMES[position]);
            SwipeLayout layout = (SwipeLayout) view.findViewById(R.id.swipeLayout);
            layout.setListener(new SwipeLayout.SwipeListener() {//swipeLayout添加监听,之前有打开的就关闭,并设置打开为当前的
                @Override
                public void onOpen(SwipeLayout swipeLayout) {
                    //判断之前是否有已经打开的 如果有 关闭
                    if(currentLayout != null && currentLayout != swipeLayout){
                        currentLayout.closeLayout();
                    }
                    //把当前打开的这个做一个记录
                    currentLayout = swipeLayout;
                }

                @Override
                public void onClose(SwipeLayout swipeLayout) {

                }
            });
            layout.setOnClickListener(new View.OnClickListener() { //swipelayout添加点击监听
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "点击"+position, Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值