Android 滑动删除viewGroup

来源:互联网 发布:合工大网络视频课 编辑:程序博客网 时间:2024/06/05 20:20

MainActivity

package zenglei.com.myapplication;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {    private ListView mLvContent;    private ArrayList<String> mList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mLvContent = (ListView) findViewById(R.id.lv_content);        mList = new ArrayList<>();        for (int i = 0; i < 30; i++) {            mList.add("item .. " + i);        }        mLvContent.setAdapter(new MyAdapter());    }    private class MyAdapter extends BaseAdapter{        @Override        public int getCount() {            return mList.size();        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            final ViewHolder holder;            if(convertView == null){                convertView = View.inflate(MainActivity.this,R.layout.item_delete,null);                holder = new ViewHolder(convertView);                //设置标记                convertView.setTag(holder);            }else{                holder = (ViewHolder) convertView.getTag();            }            //设置数据            final String data = mList.get(position);            holder.tvContent.setText(data);            //删除事件            holder.tvDelete.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    //移除数据                    mList.remove(data);                    //通知更新界面                    notifyDataSetChanged();                    //重新布局                    if(holder.svSweep.isOpen()){                        //重新布局                        holder.svSweep.requestLayout();                    }                }            });            return convertView;        }        class ViewHolder {            TextView tvContent;            TextView tvDelete;            SweepView svSweep;            public ViewHolder(View view){                tvContent = (TextView) view.findViewById(R.id.tv_content);                tvDelete = (TextView) view.findViewById(R.id.tv_delete);                svSweep = (SweepView) view.findViewById(R.id.sv_sweep);            }        }        @Override        public Object getItem(int position) {            return null;        }        @Override        public long getItemId(int position) {            return 0;        }    }}


SweepView

package zenglei.com.myapplication;import android.content.Context;import android.support.v4.widget.ViewDragHelper;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import static android.content.ContentValues.TAG;/** * Created by jonny on 2016/10/11 0011. */public class SweepView extends ViewGroup {    private View mLeftView;    private View mRightView;    private ViewDragHelper mHelper;    public SweepView(Context context) {        //        super(context);        this(context, null);    }    public SweepView(Context context, AttributeSet attrs) {        super(context, attrs);        //处理触摸事件的工具类        mHelper = ViewDragHelper.create(this, mCb);    }    private ViewDragHelper.Callback mCb = new ViewDragHelper.Callback() {        //让控件可以滑动        @Override        public boolean tryCaptureView(View child, int pointerId) {            return true;        }        //返回孩子的左侧位置        @Override        public int clampViewPositionHorizontal(View child, int left, int dx) {            if (child == mLeftView) {                //左边临界点,边界                int maxLeft = 0;                int minLeft = -mRightView.getWidth();                Log.d(TAG, "clampViewPositionHorizontal: " + left);                if (left > maxLeft) {                    left = maxLeft;                } else if (left < minLeft) {                    left = minLeft;                }            }            return left;        }        //滑动后,布局孩子位置        @Override        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {            Log.d(TAG, "onViewPositionChanged: " + mLeftView.getLeft() + ".." + dx + ".." + mLeftView.getRight());            // 判断当前拖动的是哪个孩子,如果是左边,那么我们需要布局右边孩子,            if (changedView == mLeftView) {                //布局右侧                mRightView.layout(mRightView.getLeft() + dx, 0, mRightView.getRight() + dx, mRightView.getBottom());            } else if (changedView == mRightView) {                //布局左侧孩子                mLeftView.layout(mLeftView.getLeft() + dx, 0, mLeftView.getRight() + dx, mLeftView.getBottom());            }            //            super.onViewPositionChanged(changedView, left, top, dx, dy);        }        //当手指松开的处理        @Override        public void onViewReleased(View releasedChild, float xvel, float yvel) {            //计算临界点            int linX = mLeftView.getWidth() - mRightView.getWidth()/2;            //当滑动的位置大于,左侧控件的右边x左边+ 右侧控件的一般,右侧控件出来            if(mRightView.getLeft() > linX){                // 显示右侧画出屏幕                int finalLeft = mLeftView.getWidth();                mHelper.smoothSlideViewTo(mRightView,finalLeft,0);                //重新绘制                invalidate();                isOpen = false;            }else{                // 让右侧先出来                int finaleft = mLeftView.getWidth() - mRightView.getWidth();                mHelper.smoothSlideViewTo(mRightView,finaleft,0);                invalidate();                isOpen = true;            }            super.onViewReleased(releasedChild, xvel, yvel);        }    };    private boolean isOpen = false;    public boolean isOpen(){        return  isOpen;    }    @Override    public void computeScroll() {        if(mHelper.continueSettling(true)){            //重新绘制            invalidate();        }        //        super.computeScroll();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        // 测量孩子        measureChildren(widthMeasureSpec, heightMeasureSpec);        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        //获取两个孩子        mLeftView = getChildAt(0);        mRightView = getChildAt(1);        //左边孩子宽度        int leftW = mLeftView.getMeasuredWidth();        Log.d(TAG, "onLayout: " + leftW + ".." + mLeftView.getMeasuredWidth());        //布局左边孩子        mLeftView.layout(0, 0, leftW, mLeftView.getMeasuredHeight());        //布局右边孩子        mRightView.layout(mLeftView.getMeasuredWidth(), 0, leftW + mRightView.getMeasuredWidth(), mRightView.getMeasuredHeight());    }    @Override    public boolean onTouchEvent(MotionEvent event) {        mHelper.processTouchEvent(event);        return true; //自己消费,自己处理    }}
Xml-----item_delete

<?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="match_parent"              android:orientation="vertical">    <zenglei.com.myapplication.SweepView        android:id="@+id/sv_sweep"        android:layout_width="match_parent"        android:layout_height="80dp"        >        <TextView            android:id="@+id/tv_content"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="#66000000"/>        <TextView            android:id="@+id/tv_delete"            android:layout_width="100dp"            android:layout_height="match_parent"            android:background="#ff0000"            android:gravity="center"            android:text="删除"            android:textSize="18sp"/>    </zenglei.com.myapplication.SweepView></LinearLayout>
activity_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="zenglei.com.myapplication.MainActivity">    <ListView        android:id="@+id/lv_content"        android:layout_width="match_parent"        android:layout_height="match_parent"></ListView></RelativeLayout>




0 0