上代码
public class SwipeLayout extends FrameLayout {
private static final String TAG = "SwipeLayout";
private ViewDragHelper dragHelper;
private View content,delete;
private boolean result;
public SwipeLayout(Context context) {
this(context, null);
}
public SwipeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
dragHelper = ViewDragHelper.create(this, callback);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
content = getChildAt(0);
delete = getChildAt(1);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// super.onLayout(changed, left, top, right, bottom);
content.layout(0,0,content.getMeasuredWidth(),content.getMeasuredHeight());
delete.layout(content.getRight(),0,content.getRight()+delete.getMeasuredWidth(),
delete.getMeasuredHeight());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
result = dragHelper.shouldInterceptTouchEvent(ev);
return result;
}
float downX,downY;
long downTime;//按下的时间
@Override
public boolean onTouchEvent(MotionEvent event) {
//让dragHelper帮助我们处理触摸事件
dragHelper.processTouchEvent(event);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
downTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
float moveX = event.getX();
float moveY = event.getY();
//1.计算移动的距离
float dx = moveX - downX;
float dy = moveY - downY;
//2.判断移动的方向
if(Math.abs(dx) > Math.abs(dy)){
//说明偏向于x方向,那么我们就认为用户是想滑动条目,则请求listview不要拦截了
requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
float deltaX = event.getX() - downX;
float deltaY = event.getY() - downY;
//计算按下抬起的距离,其实就是斜边了
float distance = (float) Math.sqrt(Math.pow(deltaX, 2)+Math.pow(deltaY, 2));
//计算按下抬起的时间
long duration = System.currentTimeMillis() - downTime;
//如果时间小于500,并且距离小于8px,则认为是点击事件
if(duration< ViewConfiguration.getLongPressTimeout() && distance0){
left = 0;
}else if(left<-delete.getMeasuredWidth()){
left = -delete.getMeasuredWidth();
}
}else if(child==delete){
if(left>content.getMeasuredWidth()){
left = content.getMeasuredWidth();
}else if(left<(content.getMeasuredWidth()-delete.getMeasuredWidth())){
left = (content.getMeasuredWidth()-delete.getMeasuredWidth());
}
}
return left;
}
/**
* 伴随移动
* @param changedView
* @param left
* @param top
* @param dx
* @param dy
*/
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
super.onViewPositionChanged(changedView, left, top, dx, dy);
//如果滑动的是content,那就让delete进行移动
if(changedView==content){
ViewCompat.offsetLeftAndRight(delete,dx);
}else if(changedView==delete){
ViewCompat.offsetLeftAndRight(content,dx);
}
//回调接口的方法
if(listener!=null){
if(content.getLeft()==0){
listener.onClose(SwipeLayout.this);
}else if(content.getLeft()==-delete.getMeasuredWidth()){
listener.onOpen(SwipeLayout.this);
}
}
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if(content.getLeft()<-delete.getMeasuredWidth()/2){
//open
dragHelper.smoothSlideViewTo(content, -delete.getMeasuredWidth(), 0);
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}else {
//close
dragHelper.smoothSlideViewTo(content, 0, 0);
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}
}
};
public void close(){
dragHelper.smoothSlideViewTo(content, 0, 0);
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}
@Override
public void computeScroll() {
super.computeScroll();
if(dragHelper.continueSettling(true)){
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}
}
private OnSwipeListener listener;
public void setOnSwipeListener(OnSwipeListener listener){
this.listener = listener;
}
public interface OnSwipeListener{
void onOpen(SwipeLayout layout);
void onClose(SwipeLayout layout);
}
}
布局
content
delete
mainactivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@BindView(R.id.listview)
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
listview.setAdapter(new MyAdapter());
//监听listview滑动
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//当滑动listview的时候关闭已经打开的
if(openedLayout!=null){
openedLayout.close();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "aaaaaa", Toast.LENGTH_SHORT).show();
}
});
}
SwipeLayout openedLayout = null;//用来记录当前已经打开的SwipeLayout
class MyAdapter extends BaseAdapter implements SwipeLayout.OnSwipeListener{
@Override
public int getCount() {
return Constant.NAMES.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
MyHolder myHolder = null;
if(convertView== null){
convertView = View.inflate(parent.getContext(), R.layout.adapter_list, null);
myHolder = new MyHolder(convertView);
convertView.setTag(myHolder);
}else {
myHolder = (MyHolder) convertView.getTag();
}
//绑定数据
myHolder.tvName.setText(Constant.NAMES[position]);
myHolder.swipeLayout.setOnSwipeListener(this);
myHolder.swipeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, Constant.NAMES[position], Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public void onOpen(SwipeLayout layout) {
//先关闭已经打开了的
if(openedLayout!=null && openedLayout!=layout){
openedLayout.close();
}
openedLayout = layout;
}
@Override
public void onClose(SwipeLayout layout) {
//将已经打开的置为null
if(openedLayout==layout){
openedLayout = null;
}
}
}
static class MyHolder {
@BindView(R.id.tv_name)
TextView tvName;
@BindView(R.id.tv_delete)
TextView tvDelete;
@BindView(R.id.swipeLayout)
SwipeLayout swipeLayout;
MyHolder(View view) {
ButterKnife.bind(this, view);
}
}
}