类似QQ删除ListView(二)

传承者(Inheritors)打造共同进步生态圈!!!
转载:不知出处了。。。。

三段论:
准备资源,自定义ListView,布局使用。

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:paddingLeft="10dp"
        android:paddingTop="20dp"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/delete"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:background="#FFFF0000"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:textColor="@android:color/white"
        android:paddingRight="20dp"
        android:text="删除" />
</LinearLayout>
自定义ListView
public class SlidingDeleteListView2 extends ListView {
    private int mScreenWidth;//屏幕的宽度
    private int mDownX,mDownY;//按下点的x,y值
    private int mDeleteBtnWidth;//删除按钮的宽度
    private  boolean isDeteteShown;//删除按钮是否正常显示

    private ViewGroup mPointChild;//当前处理的item
    private LinearLayout.LayoutParams mLayoutParams;

    public SlidingDeleteListView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取屏幕宽度
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
    }

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

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


    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
       switch (ev.getAction()){
           case MotionEvent.ACTION_DOWN:
               performActionDown(ev);
               break;
           case MotionEvent.ACTION_MOVE:
               performActionMove(ev);
               break;
           case MotionEvent.ACTION_UP:
               performActionUp(ev);
               break;

       }
        return super.onTouchEvent(ev);
    }



    private boolean performActionMove(MotionEvent ev) {
        int nowX = (int) ev.getX();
        int nowY = (int) ev.getY();

        if(Math.abs(nowX - mDownX) >Math.abs(nowY - mDownY)){
            //如果向 左滑动
            if(nowX<mDownX){
                //计算偏移距离
                int scroll = (nowX - mDownX)/2;
                //如果大于了删除的俺就宽度,则最大为删除按钮的宽度
                if(-scroll>= mDeleteBtnWidth){
                    scroll = -mDeleteBtnWidth;
                }

                //重新设置leftMargin
                mLayoutParams.leftMargin = scroll;
                mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);

            }


            return true;
        }
       return super.onTouchEvent(ev);
    }

    private void performActionUp(MotionEvent ev) {
        //偏移量大于button的一半,则显示button
        //否则回复默认
        if(-mLayoutParams.leftMargin >= mDeleteBtnWidth/2){
            mLayoutParams.leftMargin = -mDeleteBtnWidth;
            isDeteteShown = true;
        }else{
            turnToNormal();
        }
        mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);

    }

    private void performActionDown(MotionEvent ev) {

        if(isDeteteShown){
            turnToNormal();
        }
        mDownX = (int) ev.getX();
        mDownY = (int) ev.getY();

        //获取当前点的item
        mPointChild = (ViewGroup)getChildAt(pointToPosition(mDownX,mDownY) - getFirstVisiblePosition()) ;
      //获取删除按钮的宽度
        mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width;

        mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0).getLayoutParams();
        mLayoutParams.width = mScreenWidth;
        mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);

    }

    /**
     * 变为正常状态
     */
    public void turnToNormal() {
        mLayoutParams.leftMargin = 0;
        mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
        isDeteteShown = false;
    }


}
在activity中
public class MainActivity extends Activity {

    private SlidingDeleteListView2 mListview2;
    private List<String> mDatas;
    private ArrayAdapter<String> mAdapter;


    @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"));

        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mDatas);





        mListview2 = (SlidingDeleteListView2) findViewById(R.id.list);
        mListview2.setAdapter(new MyAdapter());
        mListview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this,mDatas.get(position),Toast.LENGTH_SHORT).show();

            }
        });
    }

    class MyAdapter extends BaseAdapter{

        public int getCount() {
            return mDatas.size();
        }

        public Object getItem(int position) {
            return mDatas.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if(null == convertView) {
                convertView = View.inflate(MainActivity.this, R.layout.listview_item2, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.tv);
            TextView delete = (TextView) convertView.findViewById(R.id.delete);

            tv.setText(mDatas.get(position));

            final int pos = position;
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDatas.remove(pos);
                    notifyDataSetChanged();
                    mListview2.turnToNormal();
                }
            });

            return convertView;
        }
    }

}

效果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值