类似QQ删除的ListView(一)

传承者(Inheritors)打造共同进步生态圈!!!

转载:http://blog.csdn.net/lmj623565791/article/details/22961279

三段论:

首先准备资源,自定义ListView,使用ListView

准备的资源:在anim文件下定义两个文件delete_btn_show.xml 和delete_btn_hide.xml

delete_btn_hide.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="false"
    android:duration="200"
    >
<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:toXScale="1.0"
    android:pivotX="100%"
    android:pivotY="50%"
    >

</scale>
</set>
delete_btn_hide.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:fillAfter="false"
    android:duration="200"
    >
<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.0"
    android:toYScale="0.0"
    android:pivotX="100%"
    android:pivotY="50%"
    >
</scale>
</set>
delete_button.xml
<?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">
    <Button
        android:id="@+id/id_item_btn"
        android:layout_width="60dp"
        android:singleLine="true"
        android:layout_height="50dp"
        android:text="删除"
        android:background="#ff0000"
        android:textColor="#ffffff"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        />

</LinearLayout>
自定义ListView
public class SlidingDeleteListView extends ListView {

    private static  final  String TAG = SlidingDeleteListView.class.getSimpleName();
    private LayoutInflater mInflater;
    private int touchSlop;//用户滑动的最小距离
    private PopupWindow mPopupWindow;
    private int mPoupWindowHeight;
    private int mPoupWindowWidth;
    private int xDown;
    private int yDown;
    private int mCurrentViewPos;
    private View mCurrentViewView;
    private int xMove;
    private int yMove;
    private boolean isSliding;
    private Button mDelBtn;

    /**
     * 为删除按钮提供一个回调接口
     */
    private DelButtonClickListener mListener;

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


    public SlidingDeleteListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mInflater = LayoutInflater.from(context);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        View view = mInflater.inflate(R.layout.delete_button, null);
        mDelBtn = (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);
        mPoupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight();
        mPoupWindowWidth = 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;

                /**
                 * 如果当前popupWindow显示,则直接隐藏,然后屏蔽ListView的touch事件的下传
                 *
                 */
                if(mPopupWindow.isShowing()){
                    mPopupWindow.dismiss();
                    return false;
                }
                //获得当前的手指按下时的item的位置
                mCurrentViewPos = pointToPosition(xDown, yDown);
                //获得当前手指按下时的item
                View view = getChildAt(mCurrentViewPos - getFirstVisiblePosition());
                mCurrentViewView = 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(dy)<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
                    mCurrentViewView.getLocationOnScreen(location);
                    //设置popupWindow的动画
                //    mPopupWindow.setAnimationStyle();
                    mPopupWindow.update();
                    mPopupWindow.showAtLocation(mCurrentViewView, Gravity.LEFT|Gravity.TOP,location[0]+mCurrentViewView.getWidth(),location[1]+mCurrentViewView.getHeight()/2-mPoupWindowHeight/2);

                    //设置删除按钮的回调
                    mDelBtn.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(mListener != null){
                                    mListener.clickHappend(mCurrentViewPos);
                                      mPopupWindow.dismiss();
                            }
                        }
                    });

                    break;
                case MotionEvent.ACTION_UP:
                    isSliding = false;
            }
        }

        return super.onTouchEvent(ev);
    }

    public void setDelButtonClickListener(DelButtonClickListener listener){
        mListener = listener;
    }

    interface DelButtonClickListener{
          void  clickHappend(int position);
    }
}
在布局文件中使用
<?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="match_parent">
<com.example.administrator.testapplication.SlidingDeleteListView
    android:id="@+id/id_listveiw"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
</com.example.administrator.testapplication.SlidingDeleteListView>

</LinearLayout>
在Activity中使用
public class MainActivity extends Activity {
    String TAG = MainActivity.class.getSimpleName();
    //==com.example.administrator.testapplication.MainActivity
    String TAG1 = MainActivity.class.getName();
    //==com.example.administrator.testapplication.MainActivity
    String TAG2 = MainActivity.class.getCanonicalName();

    private SlidingDeleteListView mListview;

    private List<String> mDatas;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sliding_delete_listview2);

        mDatas = new ArrayList<>(Arrays.asList("Hello","Java","Android","Php","servlet","JavaSript","Hibernate", "Spring", "HTML5", "Javascript", "Lucene"));

        mListview = (SlidingDeleteListView) findViewById(R.id.id_listveiw);
        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mDatas);
        mListview.setAdapter(mAdapter);

        mListview.setDelButtonClickListener(new SlidingDeleteListView.DelButtonClickListener() {
            @Override
            public void clickHappend(int position) {
                Toast.makeText(MainActivity.this, position + " : " + mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
                mAdapter.remove(mAdapter.getItem(position));
            }
        });

        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, position + " : " + mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
            }
        });
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值