listview侧滑删除

侧滑listview 代码实现

public class QQListView extends ListView {
    //用户滑动的最小距离
    private int touchSlop;
    //是否响应滑动
    private boolean isSliding;
    //手指按下时的x坐标
    private int xDown;
    //手指按下时的y坐标
    private int yDown;
    //手指移动时的x坐标
    private int xMove;
    //手指移动时的y坐标
    private int yMove;
    private LayoutInflater mInflater;
    private PopupWindow mPopupWindow;
    private int mPopupWindowHeight;
    private int mPopupWindowWidht;
    //删除按钮
    private Button mBtnDel;

    //当前手指触摸的View
    private View mCurrentView;
    //  当前手指触摸的位置
    private int mCurrentViewPos;

    //删除回调函数
    private OnDelListener mListener;

    public interface OnDelListener {
        public void onClick(int position);
    }

    public void setOnDelListener(OnDelListener listener) {
        mListener=listener;
    }


    public QQListView(Context context) {
        this(context, null);
    }

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

    public QQListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mInflater=LayoutInflater.from(getContext());
        touchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop();
        View view=mInflater.inflate(R.layout.view_delete, null);
        mBtnDel=(Button) view.findViewById(R.id.id_item_btn);
        mPopupWindow=new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        //先调用下measure,否则拿不到宽和高
        mPopupWindow.getContentView().measure(0, 0);
        mPopupWindowHeight=mPopupWindow.getContentView().getMeasuredHeight();
        mPopupWindowWidht=mPopupWindow.getContentView().getMeasuredWidth();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action=ev.getAction();
        int x=(int) ev.getX();
        int y=(int) ev.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                xDown=x;
                yDown=y;
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    dismissPopWindow();
                    return false;
                }

                mCurrentViewPos=pointToPosition(xDown, yDown);
                View view=getChildAt(mCurrentViewPos - getFirstVisiblePosition());
                mCurrentView=view;
                break;

            case MotionEvent.ACTION_MOVE:
                xMove=x;
                yMove=y;
                int dx=xMove - xDown;
                int dy=yMove - yDown;
                //判断是否是从右到左的滑动
                if (xMove < xDown && Math.abs(dx) > touchSlop && Math.abs(y) < touchSlop) {
                    isSliding=true;
                }
                break;

        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action=ev.getAction();
        if (isSliding) {
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    int[] location=new int[2];
                    // 获得当前item的位置x与y
                    mCurrentView.getLocationOnScreen(location);
                    // 设置popupWindow的动画
                    mPopupWindow.setAnimationStyle(R.style.popwindow_delete_btn_anim_style);
                    mPopupWindow.update();
                    mPopupWindow.showAtLocation(mCurrentView, Gravity.LEFT | Gravity.TOP
                            , location[0] + mCurrentView.getWidth()
                            , location[1] + mCurrentView.getHeight() / 2
                                    - mPopupWindowHeight / 2);
                    mBtnDel.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mListener != null){
                                mListener.onClick(mCurrentViewPos);
                                mPopupWindow.dismiss();
                            }
                        }
                    });
                    break;
                case MotionEvent.ACTION_UP:
                    isSliding=false;
                    break;
            }
            // 相应滑动期间屏幕itemClick事件,避免发生冲突
            return true;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 隐藏popupWindow
     */
    public void dismissPopWindow() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }
}

布局实现

<?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="wrap_content"
              android:orientation="vertical">

    <Button
        android:id="@+id/id_item_btn"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:background="@drawable/side_nav_bar"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:singleLine="true"
        android:text="删除"
        android:textColor="#ffffff"
        />
</LinearLayout>

如何使用

public class QQActivtiy extends Activity {
    private QQListView mListView;
    private ArrayAdapter<String> mAdapter;
    private List<String> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qq);
        mListView = (QQListView) findViewById(R.id.id_listview);
        // 不要直接Arrays.asList
        mDatas = new ArrayList<String>(Arrays.asList("HelloWorld", "Welcome", "Java", "Android", "Servlet", "Struts",
                "Hibernate", "Spring", "HTML5", "Javascript", "Lucene"));
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
        mListView.setAdapter(mAdapter);

        mListView.setOnDelListener(new QQListView.OnDelListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(QQActivtiy.this, position + " : " + mAdapter.getItem(position), 1).show();
                mAdapter.remove(mAdapter.getItem(position));
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Toast.makeText(QQActivtiy.this, position + " : " + mAdapter.getItem(position), 1).show();
            }
        });
    }
}

布局实现

<?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">

    <com.android.rxjava.Drag.QQListView
        android:id="@+id/id_listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </com.android.rxjava.Drag.QQListView>

</LinearLayout>

side_nav_bar.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="135"
        android:centerColor="#4CAF50"
        android:endColor="#2E7D32"
        android:startColor="#81C784"
        android:type="linear" />
</shape>

d_delete_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_style_five_focused" android:state_focused="true"></item>
    <item android:drawable="@drawable/btn_style_five_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/btn_style_five_normal"></item>

</selector>

style

<style name="popwindow_delete_btn_anim_style">
        <item name="android:windowEnterAnimation">@anim/delete_btn_show</item>
        <!-- 指定显示的动画xml -->
        <item name="android:windowExitAnimation">@anim/delete_btn_hide</item>
        <!-- 指定消失的动画xml -->
    </style>

删除动画delete_btn_hide.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="200"

        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="100%"
        android:pivotY="50%"
        android:toXScale="0.0"
        android:toYScale="1.0">


    </scale>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值