1:效果展示
效果很简单,就是 RecycleView的 滑动删除功能
2:效果分析
主要是三个步骤:
步骤一:是RecyclerView 的每一个条目上增加 删除 View控件,这个是静态xml页面编写
步骤二:RecyclerView设置监听,拦截用户的触摸事件,当触目事件move到一定条件时,就可以将删除按钮滑动处理,这里可以通过 View.serTranslationX(float translationX) 函数实现滑动
步骤三:给删除按钮添加click事件,删除条目 RecyclerView.getAdapter().notifyItemRemoved(position)
2.1 :布局Xml编写
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="72dp"
>
<TextView
android:id="@+id/menu_layout_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#f44"
android:padding="20dp"
android:text="delete"
android:textColor="#fff"
android:textSize="20dp"/>
<LinearLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#fff"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="这里是标题"
android:textSize="20dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="这里是内容"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#ddd"></FrameLayout>
</RelativeLayout>
</RelativeLayout>
2.2 : RecyclerView控件加载数据
public class SwipeMenuListActivity extends Activity {
RecyclerView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recyclerview_activity);
list = (RecyclerView) findViewById(R.id.recycle_scrollview_activity_list);
list.setLayoutManager(new LinearLayoutManager(this));
list.setAdapter(new ListAdapter());
list.addOnItemTouchListener(new SwipeMenuTouchListener());
}
class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>{
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View item = LayoutInflater.from(SwipeMenuListActivity.this).inflate(R.layout.swipe_menu_list_item, null);
return new ViewHolder(item);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.contentLayout.setTranslationX(0);
holder.menuLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.getAdapter().notifyItemRemoved(position);
}
});
}
@Override
public int getItemCount() {
return 20;
}
class ViewHolder extends SwipeMenuViewHolder{
public ViewHolder(View itemView) {
super(itemView);
menuLayout = itemView.findViewById(R.id.menu_layout);
contentLayout = itemView.findViewById(R.id.content_layout);
}
}
}
}
2.3 :拦截滑动监听,滑出删除按钮
public class SwipeMenuTouchListener implements RecyclerView.OnItemTouchListener{
private float downX;
private float downY;
private float tmpX;
private float orientation = 0;
private boolean moving = false;
private View currentChild;
private SwipeMenuViewHolder currentHolder;
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child == null){
return false;
}
switch (e.getAction()){
case MotionEvent.ACTION_DOWN:
currentChild = rv.findChildViewUnder(e.getX(), e.getY());
currentHolder = (SwipeMenuViewHolder) rv.getChildViewHolder(currentChild);
downX = e.getX();
downY = e.getY();
moving = false;
break;
case MotionEvent.ACTION_MOVE:
float moveX = e.getX() - downX;
float moveY = e.getY() - downY;
if(moveX == 0 || Math.abs(moveX) < Math.abs(moveY)){
orientation = 0;
break;
}
if(Math.abs(e.getX() - tmpX) > 10){
orientation = e.getX() - tmpX;
}
if(child != currentChild){
break;
}
if(moveX > 20) {
moving = true;
}
if(moveX < 0) {
if(-moveX <= currentHolder.menuLayout.getWidth() && -currentHolder.contentLayout.getTranslationX() < currentHolder.menuLayout.getWidth()) {
currentHolder.contentLayout.setTranslationX(moveX);
}
else{
currentHolder.contentLayout.setTranslationX(-currentHolder.menuLayout.getWidth());
}
}
else if(moveX > 0 && currentHolder.contentLayout.getTranslationX() < 0){
float m = -currentHolder.menuLayout.getWidth() + moveX;
if(m > 0){
currentHolder.contentLayout.setTranslationX(0);
}
else{
currentHolder.contentLayout.setTranslationX(m);
}
}
tmpX = e.getX();
break;
case MotionEvent.ACTION_UP:
if(orientation >= 0){
currentHolder.contentLayout.setTranslationX(0);
}
else{
currentHolder.contentLayout.setTranslationX(-currentHolder.menuLayout.getWidth());
}
return moving;
//break;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}