QQ5.0列表滑动删除的简单实现

来源:互联网 发布:mac虚拟机cad打开慢 编辑:程序博客网 时间:2024/05/08 07:37

自定义一个view 继承LinearLayout:

代码:

public class SwipeLayout extends LinearLayout {    private ViewDragHelper viewDragHelper;    private View contentView;    private View actionView;    private int dragDistance;    private final double AUTO_OPEN_SPEED_LIMIT = 800.0;    private int draggedX;    public SwipeLayout(Context context) {        this(context, null);    }    public SwipeLayout(Context context, AttributeSet attrs) {        this(context, attrs, -1);    }    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        viewDragHelper = ViewDragHelper.create(this, new DragHelperCallback());    }    @Override    protected void onFinishInflate() {        contentView = getChildAt(0);        actionView = getChildAt(1);        actionView.setVisibility(GONE);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        dragDistance = actionView.getMeasuredWidth();    }    private class DragHelperCallback extends ViewDragHelper.Callback {        @Override        public boolean tryCaptureView(View view, int i) {            return view == contentView || view == actionView;        }        @Override        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {            draggedX = left;            if (changedView == contentView) {                actionView.offsetLeftAndRight(dx);            } else {                contentView.offsetLeftAndRight(dx);            }            if (actionView.getVisibility() == View.GONE) {                actionView.setVisibility(View.VISIBLE);            }            invalidate();        }        @Override        public int clampViewPositionHorizontal(View child, int left, int dx) {            if (child == contentView) {                final int leftBound = getPaddingLeft();                final int minLeftBound = -leftBound - dragDistance;                final int newLeft = Math.min(Math.max(minLeftBound, left), 0);                return newLeft;            } else {                final int minLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() - dragDistance;                final int maxLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() + getPaddingRight();                final int newLeft = Math.min(Math.max(left, minLeftBound), maxLeftBound);                return newLeft;            }        }        @Override        public int getViewHorizontalDragRange(View child) {            return dragDistance;        }        @Override        public void onViewReleased(View releasedChild, float xvel, float yvel) {            super.onViewReleased(releasedChild, xvel, yvel);            boolean settleToOpen = false;            if (xvel > AUTO_OPEN_SPEED_LIMIT) {                settleToOpen = false;            } else if (xvel < -AUTO_OPEN_SPEED_LIMIT) {                settleToOpen = true;            } else if (draggedX <= -dragDistance / 2) {                settleToOpen = true;            } else if (draggedX > -dragDistance / 2) {                settleToOpen = false;            }            final int settleDestX = settleToOpen ? -dragDistance : 0;            viewDragHelper.smoothSlideViewTo(contentView, settleDestX, 0);            ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);        }    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        if(viewDragHelper.shouldInterceptTouchEvent(ev)) {            return true;        }        return super.onInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        viewDragHelper.processTouchEvent(event);        return true;    }    @Override    public void computeScroll() {        super.computeScroll();        if(viewDragHelper.continueSettling(true)) {            ViewCompat.postInvalidateOnAnimation(this);        }    }}
2:Mainactivity代码:

public class MainActivity extends AppCompatActivity {    private RecyclerView rlv;    private ArrayList<String> list;    private RecyclerView.LayoutManager LayoutManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        initData();        initAdapter();    }    /**     * 控件     */    private void initView() {        rlv = (RecyclerView) findViewById(R.id.rlv);    }    /**     * 数据     */    private void initData() {        list = new ArrayList<>();        for (int i = 0; i < 50; i++) {            list.add("5.0新特性测试" + i);        }    }    /**     * 适配器     */    private void initAdapter() {        ItemAdapter ItemAdapter = new ItemAdapter(this, list);        LayoutManager = new GridLayoutManager(this, 1);        rlv.setLayoutManager(LayoutManager);        rlv.setAdapter(ItemAdapter);    }}3:主布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <android.support.v7.widget.RecyclerView        android:id="@+id/rlv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>
4:Item布局:
<com.lhz.swipelayoutexample.SwipeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="200dp" android:layout_width="match_parent" > <LinearLayout android:background="#ffffff" android:layout_height="50dp" android:layout_width="match_parent"> <TextView android:id="@+id/item_tv" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingLeft="20dp" android:text="@string/hello_world" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_height="50dp" android:layout_width="150dp"> <RelativeLayout android:background="#ff0000" android:clickable="true" android:id="@+id/delete_button" android:layout_height="50dp" android:layout_width="50dp"> <View android:background="@drawable/trash" android:layout_centerInParent="true" android:layout_height="28dp" android:layout_width="28dp" /> </RelativeLayout> <RelativeLayout android:background="#c2c2c2" android:clickable="true" android:id="@+id/view_button" android:layout_height="50dp" android:layout_width="50dp"> <View android:background="@drawable/magnifier" android:layout_centerInParent="true" android:layout_height="28dp" android:layout_width="28dp" /> </RelativeLayout> <RelativeLayout android:background="#aaffff" android:clickable="true" android:id="@+id/star_button" android:layout_height="50dp" android:layout_width="50dp"> <View android:background="@drawable/star" android:layout_centerInParent="true" android:layout_height="28dp" android:layout_width="28dp" /> </RelativeLayout> </LinearLayout></com.lhz.swipelayoutexample.SwipeLayout>
5:适配器:
package com.github.lzyzsd.swipelayoutexample;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import com.github.lzyzsd.viewdraghelperdemo.R;import java.util.ArrayList;/** *lhz * 2016.4.21 */public class ItemAdapter extends RecyclerView.Adapter implements View.OnClickListener {    private ArrayList<String> list;    private Context Context;    public ItemAdapter(Context Context, ArrayList<String> list) {        this.Context = Context;        this.list = list;    }    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View inflate = View.inflate(Context, R.layout.item_list, null);        Demo Demo = new Demo(inflate);        return Demo;    }    @Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {        Demo Demo = (ItemAdapter.Demo) holder;        Demo.item_tv.setText(list.get(position));        Demo.delete_button.setOnClickListener(this);        Demo.view_button.setOnClickListener(this);        Demo.star_button.setOnClickListener(this);    }    @Override    public int getItemCount() {        return list.size();    }    /**     * 后面逻辑自己处理     * @param v     */    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.delete_button:                Toast.makeText(Context, "delete_button", Toast.LENGTH_SHORT).show();                break;            case R.id.view_button:                Toast.makeText(Context, "view_button", Toast.LENGTH_SHORT).show();                break;            case R.id.star_button:                Toast.makeText(Context, "star_button", Toast.LENGTH_SHORT).show();                break;        }    }    private class Demo extends RecyclerView.ViewHolder {        private final RelativeLayout star_button;        private final RelativeLayout view_button;        private final RelativeLayout delete_button;        private final TextView item_tv;        public Demo(View itemView) {            super(itemView);            delete_button = (RelativeLayout) itemView.findViewById(R.id.delete_button);            view_button = (RelativeLayout) itemView.findViewById(R.id.view_button);            star_button = (RelativeLayout) itemView.findViewById(R.id.star_button);            item_tv = (TextView) itemView.findViewById(R.id.item_tv);        }    }}

有问题可以发我邮箱1668126018@qq.com

3 0
原创粉丝点击