创建一个Recyclerview列表item布局,自定义容器:
SlidingButtonView.java
package com.huatec.myapplication.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import com.huatec.myapplication.R;
public class SlidingButtonView extends HorizontalScrollView {
private static final String TAG = "SlidingButtonView";
private TextView lTextView_Delete;//删除按钮
private int lScrollWith;//横向滚动范围
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);
//获取水平滚动条可以滚动的范围,也就是右侧删除按钮的宽度
lScrollWith = lTextView_Delete.getWidth();
}
}
//手势判断
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "onTouchEvent: ");
changeScrollx();
return true;
}
return super.onTouchEvent(ev);
}
//根据滑动距离判断是否显示删除按钮
private void changeScrollx() {
//触摸滑动的距离大于删除按钮的一半时
if (getScrollX() >= (lScrollWith / 2)) {
//显示删除按钮
this.smoothScrollTo(lScrollWith, 0);
} else {
//隐藏删除按钮
this.smoothScrollTo(0, 0);
}
}
}
item:
<?xml version="1.0" encoding="utf-8"?>
<com.huatec.myapplication.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">
<RelativeLayout
android:layout_width="match_parent"
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="@drawable/btn_click_red_bg"
android:gravity="center"
android:text="删除"
android:textColor="@android:color/white"/>
<!--行的布局文件-->
<RelativeLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<!--图标-->
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/icon_1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/img"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="123"
android:textColor="@android:color/black"
android:textSize="15sp"/>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:singleLine="true"
android:text="123"
android:textColor="@android:color/black"
android:textSize="10sp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</com.huatec.myapplication.view.SlidingButtonView>
Adapter:
package com.huatec.myapplication.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.huatec.myapplication.R;
import com.huatec.myapplication.utils.Utils;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
//图标数组
private int[] icons = {
R.drawable.icon_1, R.drawable.icon_2, R.drawable.icon_3,
R.drawable.icon_4, R.drawable.icon_5, R.drawable.icon_6, R.drawable.icon_7,
R.drawable.icon_8, R.drawable.icon_9, R.drawable.icon_10, R.drawable.icon_11
};
//名字数组
private int[] names = {
R.string.name1, R.string.name2, R.string.name3, R.string.name4, R.string.name5,
R.string.name6, R.string.name7, R.string.name8, R.string.name9, R.string.name10,
R.string.name11
};
//信息数组
private int[] infos = {
R.string.info1, R.string.info2, R.string.info3, R.string.info4, R.string.info5,
R.string.info6, R.string.info7, R.string.info8, R.string.info9, R.string.info10,
R.string.info11
};
private Context lContent;//定义上下文
//集合
private List<Integer> listIcon = new ArrayList<>();
private List<Integer> listName = new ArrayList<>();
private List<Integer> listInfo = new ArrayList<>();
public Adapter(Context lContent) {
this.lContent = lContent;
//设置菜单行数与行内图标、名称、信息
for (int i = 0; i < 11; i++) {
listIcon.add(icons[I]);
listName.add(names[I]);
listInfo.add(infos[I]);
}
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//获取列表中每行的布局文件
View view = LayoutInflater.from(lContent).inflate(R.layout.layout_item, parent, false);
return new MyViewHolder(view);
}
//设置列表中行所显示的内容
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
//设置图标
holder.img.setBackgroundResource(listIcon.get(position));
//设置名称
holder.name.setText(listName.get(position));
//设置信息
holder.info.setText(listInfo.get(position));
//设置内容宽度为屏幕的宽度
holder.layout_content.getLayoutParams().width = Utils.getScreenWidth(lContent);
//删除按钮的方法
holder.btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int n = holder.getLayoutPosition();//获取要删除行的位置
removeData(n);//删除列表中指定的行
}
});
}
//返回行的总数
@Override
public int getItemCount() {
return listIcon.size();
}
//删除列表行中信息的方法
public void removeData(int position){
listIcon.remove(position);//删除图标
listName.remove(position);//删除行中名字
listInfo.remove(position);//删除信息
notifyItemRemoved(position);//删除行
}
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView btn_delete;
public TextView name, info;//名字与信息
public ImageView img;//图标
public ViewGroup layout_content;//图标与信息布局
//获取控件
public MyViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
info = itemView.findViewById(R.id.info);
img = itemView.findViewById(R.id.img);
layout_content = itemView.findViewById(R.id.layout_content);
btn_delete = itemView.findViewById(R.id.tv_delete);
}
}
}
效果图: