本文主要是介绍[Android开发] RecyleView+Checkbox 实现勾选条目删除简单功能 + 删除完后用图片占位,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1. activity + adpter
- 2. item.xml
- 3. 占位
- 3.1 xml
- 3.2 activity
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<>();@Overrideprotected 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() {@Overridepublic 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);}}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);return new MyViewHolder(view);}@Overridepublic 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);});}@Overridepublic 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"><CheckBoxandroid:id="@+id/checkbox"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid: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"><Buttonandroid: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.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@id/btnDelete" /><ImageViewandroid: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<>();@Overrideprotected 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() {@Overridepublic 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);}}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);return new MyViewHolder(view);}@Overridepublic 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);});}@Overridepublic 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);}}}
}
这篇关于[Android开发] RecyleView+Checkbox 实现勾选条目删除简单功能 + 删除完后用图片占位的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!