如何通过按钮删除回收器视图适配器中的最后一项?



我有一个回收器视图,当我单击不在回收器视图中的按钮时,我想删除它上面的最后一项。我想删除该项目并通知适配器该项目已删除,以便我可以输入另一个项目。顺便说一下,我输入的项目是通过编辑文本和按钮。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.brecyclerview.MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edit_ten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Score"
android:inputType="numberSigned|number" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_add"
android:text="Add"
tools:ignore="OnClick" />
<Button
android:id="@+id/btn_undo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_undo"
android:text="Undo"
tools:ignore="OnClick" />
<Button
android:id="@+id/btn_new"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_new"
android:text="New Game" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:itemCount="8" />
</LinearLayout>

list_item.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">
<RelativeLayout
android:id="@+id/singleRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/userImg"
android:src="@mipmap/ic_launcher"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="@+id/pNametxt"
android:text="User Name"
android:textSize="20sp"
android:layout_marginTop="6dp"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/userImg"
android:layout_toEndOf="@+id/userImg"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" />
</RelativeLayout>
<View
android:layout_below="@+id/singleRow"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f2f2f2" />
</RelativeLayout>

主要活动.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
int total = 0;
Button button;
Button button1;
Button button2;
EditText editText;
TextView personName;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager layoutManager;
List<PersonUtils> personUtilsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn_add);
button1 = (Button)findViewById(R.id.btn_undo);
button2 = (Button)findViewById(R.id.btn_new);
editText = (EditText)findViewById(R.id.edit_ten);
personName = (TextView)findViewById(R.id.pNametxt);
recyclerView = (RecyclerView) findViewById(R.id.recycleViewContainer);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
personUtilsList = new ArrayList<>();
mAdapter = new CustomRecyclerAdapter(this, personUtilsList);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == button2) {
MainActivity.this.finish();
startActivity(new Intent(MainActivity.this, MainActivity.class));
}}});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i<personUtilsList.size(); i++)
{
total = personUtilsList.get(i).getPersonName();
}
total += Integer.parseInt(editText.getText().toString());
personUtilsList.add(new PersonUtils(total));
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
editText.setText("");
}});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i<personUtilsList.size(); i++)
{
personUtilsList.remove(i).getPersonName();
personUtilsList.remove(new PersonUtils(total));
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}}
});
}}

定制回收器适配器.java

import android.content.Context;
import android.preference.PreferenceScreen;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> {
private Context context;
private List<PersonUtils> personUtils;
public CustomRecyclerAdapter(Context context, List personUtils) {
this.context = context;
this.personUtils = personUtils;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setTag(personUtils.get(position));
PersonUtils pu = personUtils.get(position);
holder.pName.setText(String.valueOf(pu.getPersonName()));
}
@Override
public int getItemCount() {
return (personUtils.size()>8)?8:personUtils.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView pName;
final Button deleteButton;
public ViewHolder(final View itemView) {
super(itemView);
pName = (TextView) itemView.findViewById(R.id.pNametxt);
deleteButton = (Button) itemView.findViewById(R.id.btn_undo);
}
}}

PersonUtils.java

public class PersonUtils {
private Integer personName;
public static void remove(int index) {
}
public Integer getPersonName() {
return personName;
}
public void setPersonName(Integer personName) {
this.personName = personName;
}
public PersonUtils(Integer personName) {
this.personName = personName;
}
}

我希望删除回收器视图中的最后一项(整数(并通知适配器,以便我可以在需要时编辑该值。

您可以在适配器中添加一种方法以删除列表中的最后一项:

public ViewHolder removeLastItem() {
if (personUtils == null || personUtils.isEmpty()) return;
personUtils.removeAt(personUtils.getSize()-1);
notifyDataSetChanged();
}

单击按钮时,只需调用:

adapter.removeLastItem()

检查您是否在最后一个索引中,然后删除该项目并使用notifyItemRemoved通知任何已注册的观察者:

if(currentPosition == 0){
listOfItems.removeAt(currentPosition)
notifyItemRemoved(currentPosition)  
}

最新更新