layout文件
<?xml version="1.0" encoding="utf-8"?>
<com.libin.finalexam.activity.view.SlidingButtonView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="1dp"
android:background="@android:color/white"
android:paddingLeft="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<!--删除按钮-->
<TextView
android:id="@+id/tv_delete"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/layout_content"
android:background="#995544"
android:gravity="center"
android:text="删 除"
android:textColor="#DDFFFFFF" />
<RelativeLayout
android:id="@+id/layout_content"
android:layout_width="310dp"
android:layout_height="match_parent"
android:gravity="center_vertical">
<!--图标-->
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:textColor="#000000" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_name"
android:text="789"
android:textColor="#000000" />
<TextView
android:id="@+id/tv_detail_addr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_name"
android:text="重庆xxxxxxx"
android:textColor="#000000"
android:textSize="10sp" />
</RelativeLayout>
</RelativeLayout>
</com.libin.finalexam.activity.view.SlidingButtonView>
SlidingButtonView类
package com.libin.finalexam.activity.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import com.libin.finalexam.activity.R;
/**
* 滑动按钮自定义控件 Created by Administrator on 2017/2/3.
*/
public class SlidingButtonView extends HorizontalScrollView {
private TextView lTextView_Delete; //删除按钮
private int lScrollWidth; //横向滑动的范围
private Boolean first = false; //标记第一次进入获取删除按钮控件
public SlidingButtonView(Context context) {
this(context, null);
}
public SlidingButtonView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlidingButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOverScrollMode(OVER_SCROLL_NEVER);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//第一次进入获取删除按钮控件
if(!first){
lTextView_Delete = findViewById(R.id.tv_delete);
first = true;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//默认隐藏删除按钮
if(changed){
this.scrollTo(0,0);
//获取水平滚动条可以滑动的范围,即右侧按钮的宽度
lScrollWidth = lTextView_Delete.getWidth();
}
}
/**
*滑动手指抬起时的手势判断
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
changeScrollx(); //根据滑动距离判断是否显示删除按钮
return true;
default:
break;
}
return super.onTouchEvent(event);
}
/**
* 根据滑动距离判断是否显示删除按钮
*/
public void changeScrollx(){
//触摸滑动的距离大于删除按钮宽度的一半
if(getScrollX() >= (lScrollWidth/2)){
//显示删除按钮
this.smoothScrollTo(lScrollWidth, 0);
}else{
//隐藏删除按钮
this.smoothScrollTo(0, 0);
}
}
}
在对应的地方删除了数据后要做的
notifyItemRemoved(position); //根据position位置删除recyclerview的单个item
notifyItemRangeChanged(0,data.size()); //刷新记录
data.remove(position); //移除显示数据对应的数据