android左滑出现删除按钮的场景比较常见,实现方式也有多种,这里采用比较简单的一种,即列表的item布局采用HorizontalScrollView,将正常展示的内容宽度设置为屏幕的宽度,删除按钮默认隐藏,通过滑动来显示或者隐藏删除按钮。item布局如下:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hsv"
android:scrollbars="none"
android:orientation="horizontal">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#998"
android:gravity="center"
tools:text="998"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#666"
android:gravity="center"
android:text="delete"
/>
</LinearLayout>
</HorizontalScrollView>
列表采用Recyclerview,代码比较简单,一看就明白了,关键处在于adapter中onBindViewHolder的处理,见下面的代码:
public class Main2Activity extends AppCompatActivity {
List<String> list = new ArrayList<>();
private RvAdapter rvAdapter;
private String[] items = new String[20];
private int width;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initData();
final RecyclerView rv = findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(linearLayoutManager);
rvAdapter = new RvAdapter(items);
rv.setAdapter(rvAdapter);
width = getWindowManager().getDefaultDisplay().getWidth();
}
private void initData() {
for (int i = 0; i < 20; i++) {
items[i] = Integer.toString(i);
}
}
class RvAdapter extends RecyclerView.Adapter<RvAdapter.VH> {
String[] items;
public RvAdapter(String[] items) {
this.items = items;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VH(LayoutInflater.from(Main2Activity.this).inflate(R.layout.scroll_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull final VH holder, final int position) {
//list用来记录itemView是否滑开,避免recyclerview复用出现bug
if (list.contains(position + "")) {
holder.itemView.scrollTo(holder.textDelete.getWidth(), 0);
} else {
holder.itemView.scrollTo(0, 0);
}
holder.text.setText(items[position]);
//将item要展示的内容宽度设置为屏幕宽度,删除按钮则会隐藏在屏幕右侧
ViewGroup.LayoutParams layoutParams = holder.text.getLayoutParams();
layoutParams.width = width;
holder.hsv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
//获取滑动的距离
int scrollX = holder.hsv.getScrollX();
//获取右侧删除按钮的宽度
int width = holder.textDelete.getWidth();
//如果滑动距离超过了删除按钮宽度的1/2,则将删除按钮滑出,否则隐藏
if (scrollX >= width / 2) {
holder.itemView.scrollTo(width, 0);
//滑动的item记录下来
list.add(position + "");
} else {
holder.itemView.scrollTo(0, 0);
if (list.contains(position + "")) {
//记录中移除
list.remove(position + "");
}
}
break;
case MotionEvent.ACTION_MOVE:
break;
}
//注意此处返回false
return false;
}
});
}
@Override
public int getItemCount() {
return items.length;
}
class VH extends RecyclerView.ViewHolder {
private final TextView text;
private final TextView textDelete;
private final HorizontalScrollView hsv;
public VH(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.tv1);
textDelete = itemView.findViewById(R.id.tv2);
hsv = itemView.findViewById(R.id.hsv);
}
}
}
}