1. activity + adpter
package com.example.myapplication.view;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.List;
public class ForthActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private Button btnDelete;
private MyAdapter adapter;
private List<String> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forth);
for (int i = 1; i <= 5; i++) {
data.add("Item " + i);
}
recyclerView = findViewById(R.id.recyclerView);
btnDelete = findViewById(R.id.btnDelete);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyAdapter(data);
recyclerView.setAdapter(adapter);
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.deleteSelectedItems();
}
});
}
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<String> data;
private List<Boolean> checkedItems;
public MyAdapter(List<String> data) {
this.data = data;
this.checkedItems = new ArrayList<>(data.size());
for (int i = 0; i < data.size(); i++) {
checkedItems.add(false);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(data.get(position));
holder.checkbox.setChecked(checkedItems.get(position));
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
checkedItems.set(position, isChecked);
});
}
@Override
public int getItemCount() {
return data.size();
}
public void deleteSelectedItems() {
for (int i = data.size() - 1; i >= 0; i--) {
if (checkedItems.get(i)) {
data.remove(i);
checkedItems.remove(i);
notifyItemRemoved(i);
notifyDataSetChanged();
}
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
CheckBox checkbox;
TextView textView;
MyViewHolder(View itemView) {
super(itemView);
checkbox = itemView.findViewById(R.id.checkbox);
textView = itemView.findViewById(R.id.textView);
}
}
}
}
2. item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp" />
</LinearLayout>
3. 占位
3.1 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Selected Items"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/btnDelete" />
<ImageView
android:id="@+id/placeholderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher_background"
android:visibility="gone"/>
</RelativeLayout>
3.2 activity
package com.example.myapplication.view;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.List;
public class ForthActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private Button btnDelete;
private MyAdapter adapter;
private List<String> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forth);
for (int i = 1; i <= 5; i++) {
data.add("Item " + i);
}
recyclerView = findViewById(R.id.recyclerView);
btnDelete = findViewById(R.id.btnDelete);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyAdapter(data);
recyclerView.setAdapter(adapter);
ImageView placeholderImageView = findViewById(R.id.placeholderImageView);
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.deleteSelectedItems();
if (adapter.getItemCount() == 0) {
recyclerView.setVisibility(View.GONE);
placeholderImageView.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
placeholderImageView.setVisibility(View.GONE);
}
}
});
}
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<String> data;
private List<Boolean> checkedItems;
public MyAdapter(List<String> data) {
this.data = data;
this.checkedItems = new ArrayList<>(data.size());
for (int i = 0; i < data.size(); i++) {
checkedItems.add(false);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(data.get(position));
holder.checkbox.setChecked(checkedItems.get(position));
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
checkedItems.set(position, isChecked);
});
}
@Override
public int getItemCount() {
return data.size();
}
public void deleteSelectedItems() {
for (int i = data.size() - 1; i >= 0; i--) {
if (checkedItems.get(i)) {
data.remove(i);
checkedItems.remove(i);
notifyItemRemoved(i);
notifyDataSetChanged();
}
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
CheckBox checkbox;
TextView textView;
MyViewHolder(View itemView) {
super(itemView);
checkbox = itemView.findViewById(R.id.checkbox);
textView = itemView.findViewById(R.id.textView);
}
}
}
}