用java写一个recyclerView类似相册支持多选删除的例子
时间: 2024-03-18 20:40:02 浏览: 98
一个RecyclerView 的实例
好的,以下是一个用 Java 编写的 RecyclerView 类似相册支持多选删除的例子:
1. 创建 RecyclerView 布局文件 `activity_main.xml`,包含一个 RecyclerView 和一个 FloatingActionButton:
```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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_delete"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16dp"
android:visibility="gone" />
</RelativeLayout>
```
2. 创建 RecyclerView 的 Item 布局文件 `item_photo.xml`,包含一个 ImageView 和一个 CheckBox:
```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="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop" />
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_margin="8dp" />
</RelativeLayout>
```
3. 创建 RecyclerView 的 ViewHolder 类 `PhotoViewHolder.java`:
```java
public class PhotoViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public CheckBox checkBox;
public PhotoViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
checkBox = itemView.findViewById(R.id.check_box);
}
}
```
4. 创建 RecyclerView 的 Adapter 类 `PhotoAdapter.java`:
```java
public class PhotoAdapter extends RecyclerView.Adapter<PhotoViewHolder> {
private List<Photo> photoList;
private List<Photo> selectedPhotoList;
private Context context;
public PhotoAdapter(List<Photo> photoList, Context context) {
this.photoList = photoList;
this.context = context;
selectedPhotoList = new ArrayList<>();
}
@Override
public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, parent, false);
return new PhotoViewHolder(view);
}
@Override
public void onBindViewHolder(PhotoViewHolder holder, int position) {
Photo photo = photoList.get(position);
Glide.with(context).load(photo.getUrl()).into(holder.imageView);
holder.checkBox.setChecked(selectedPhotoList.contains(photo));
holder.itemView.setOnClickListener(v -> {
if (selectedPhotoList.contains(photo)) {
selectedPhotoList.remove(photo);
} else {
selectedPhotoList.add(photo);
}
notifyDataSetChanged();
});
}
@Override
public int getItemCount() {
return photoList.size();
}
public void deleteSelectedPhotos() {
for (Photo photo : selectedPhotoList) {
photoList.remove(photo);
}
notifyDataSetChanged();
}
public void showDeleteButton(boolean show) {
((MainActivity) context).runOnUiThread(() -> {
FloatingActionButton fabDelete = ((MainActivity) context).findViewById(R.id.fab_delete);
if (show) {
fabDelete.setVisibility(View.VISIBLE);
fabDelete.setOnClickListener(v -> {
deleteSelectedPhotos();
selectedPhotoList.clear();
fabDelete.setVisibility(View.GONE);
});
} else {
fabDelete.setVisibility(View.GONE);
}
});
}
}
```
5. 在 Activity 中初始化 RecyclerView 和 Adapter:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private PhotoAdapter photoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
List<Photo> photoList = new ArrayList<>();
photoList.add(new Photo("https://picsum.photos/id/0/200/300"));
photoList.add(new Photo("https://picsum.photos/id/1/200/300"));
photoList.add(new Photo("https://picsum.photos/id/2/200/300"));
photoList.add(new Photo("https://picsum.photos/id/3/200/300"));
photoList.add(new Photo("https://picsum.photos/id/4/200/300"));
photoList.add(new Photo("https://picsum.photos/id/5/200/300"));
photoList.add(new Photo("https://picsum.photos/id/6/200/300"));
photoList.add(new Photo("https://picsum.photos/id/7/200/300"));
photoList.add(new Photo("https://picsum.photos/id/8/200/300"));
photoList.add(new Photo("https://picsum.photos/id/9/200/300"));
photoList.add(new Photo("https://picsum.photos/id/10/200/300"));
photoList.add(new Photo("https://picsum.photos/id/11/200/300"));
photoAdapter = new PhotoAdapter(photoList, this);
recyclerView.setAdapter(photoAdapter);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null) {
int position = recyclerView.getChildAdapterPosition(child);
photoAdapter.showDeleteButton(true);
super.onLongPress(e);
}
}
});
@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && gestureDetector.onTouchEvent(e)) {
int position = rv.getChildAdapterPosition(child);
return true;
}
return false;
}
@Override
public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
}
}
```
其中,`Photo` 类是一个简单的 Java 类,表示一张照片,包含一个 `url` 属性。在 `PhotoAdapter` 中,通过 Glide 加载图片,点击 Item 时选中或取消选中,长按 Item 时显示删除按钮,点击删除按钮时删除选中的照片。在 Activity 中,通过 `GestureDetector` 监听 RecyclerView 的长按事件,当长按 Item 时显示删除按钮。
阅读全文