Android - 使用ArrayList作为Holder防止RecyclerView中的重复项

眼前多少难甘事,自古男儿当自强。这篇文章主要讲述Android - 使用ArrayList作为Holder防止RecyclerView中的重复项相关的知识,希望能为你提供帮助。
我使用ArrayList来保存要在RecyclerView中显示的对象。代码如下。

private ArrayList< BagLotNumberModel> bagLotNumbers = new ArrayList< > (); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_antam_incoming); bagLotNumberRestService = RestApiUtils.getBagLotNumberRestService(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); setUpRecyclerView(); } private void setUpRecyclerView() { RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyler_view_antam_incoming); bagLotNumberAdapter = new BagLotNumberAdapter(bagLotNumbers); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setAdapter(bagLotNumberAdapter); recyclerView.setHasFixedSize(true); }

CONSTRUCTOR
public class BagLotNumberModel {@SerializedName("id") public int id; @SerializedName("bagNumber") public int bagNumber; @SerializedName("lotNumber") public String lotNumber; public int getId() { return id; }public void setId(int id) { this.id = id; }public int getBagNumber() { return bagNumber; }public void setBagNumber(int bagNumber) { this.bagNumber = bagNumber; }public String getLotNumber() { return lotNumber; }public void setLotNumber(String lotNumber) { this.lotNumber = lotNumber; } }

适配器
public class BagLotNumberAdapter extends RecyclerView.Adapter< BagLotNumberAdapter.BagLotNumberViewHolder> {private ArrayList< BagLotNumberModel> dataList; public BagLotNumberAdapter(ArrayList< BagLotNumberModel> dataList) { this.dataList = dataList; }@NonNull @Override public BagLotNumberViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View view = layoutInflater.inflate(R.layout.adapter_antam_bag_layout_number, parent, false); return new BagLotNumberViewHolder(view); }@Override public void onBindViewHolder(@NonNull BagLotNumberViewHolder holder, int position) { holder.txtId.setText(String.valueOf(dataList.get(position).getId())); holder.txtBagNumber.setText(String.valueOf(dataList.get(position).getBagNumber())); holder.txtLotNumber.setText(dataList.get(position).getLotNumber()); }@Override public int getItemCount() { return dataList.size(); }class BagLotNumberViewHolder extends RecyclerView.ViewHolder {TextView txtId, txtBagNumber, txtLotNumber; BagLotNumberViewHolder(View itemView) { super(itemView); txtId = (TextView) itemView.findViewById(R.id.txt_id_bag); txtBagNumber = (TextView) itemView.findViewById(R.id.txt_bag_number); txtLotNumber = (TextView) itemView.findViewById(R.id.txt_lot_number); } } }

通过使用Client Rest API的改进,应用程序使用GET搜索项目。代码如下。
改造
private void doGetRestBagLotNumber(int bagNumber, String lotNumber, final BagLotNumberRestService callback) { Call< BagLotNumberModel> call = bagLotNumberRestService.getAntamBagLotNumber(bagNumber, lotNumber); call.enqueue(new Callback< BagLotNumberModel> () { @Override public void onResponse(Call< BagLotNumberModel> call, Response< BagLotNumberModel> response) {if (response.code() == 404 || response.code() == 422) { callback.onSuccess(false, "Not Found"); } else { int id = response.body().getId(); int bagNumber = response.body().getBagNumber(); String lotNumber = response.body().getLotNumber(); // Dynamically adding element to RecyclerView preparedObjectDataBagLotNumber(id, bagNumber, lotNumber); callback.onSuccess(response.body() != null, "Found"); } }@Override public void onFailure(Call< BagLotNumberModel> call, Throwable t) { Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); } }); }private void preparedObjectDataBagLotNumber(int id, int bagNumber, String lotNumber) { BagLotNumberModel bagLotNumberModel = new BagLotNumberModel(); bagLotNumberModel.setId(id); bagLotNumberModel.setBagNumber(bagNumber); bagLotNumberModel.setLotNumber(lotNumber); bagLotNumbers.add(bagLotNumberModel); bagLotNumberAdapter.notifyDataSetChanged(); }

并要求上述功能。
doGetRestBagLotNumber( Integer.parseInt(bagNumber.getText().toString()), lotNumber.getText().toString(), new BagLotNumberRestService() { @Override public void onSuccess(boolean value, String message) {if (value) { ((TextView) promptsView.findViewById(R.id.textViewTickMsgAntamIncoming)) .setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_check_green_24dp, 0, 0, 0); ((TextView) promptsView.findViewById(R.id.textViewTickMsgAntamIncoming)) .setText(message); } else { ((TextView) promptsView.findViewById(R.id.textViewTickMsgAntamIncoming)) .setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_sentiment_dissatisfied_red_24dp, 0, 0, 0); ((TextView) promptsView.findViewById(R.id.textViewTickMsgAntamIncoming)) .setText(message); }promptsView.findViewById(R.id.editTextDialogAntamBagNumber).requestFocus(); promptsView.postDelayed(new Runnable() { @Override public void run() { ((TextView) promptsView.findViewById(R.id.textViewTickMsgAntamIncoming)).setText(""); ((TextView) promptsView.findViewById(R.id.textViewTickMsgAntamIncoming)) .setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } }, 5000); }@Override public Call< BagLotNumberModel> getAntamBagLotNumber(int bagNumber, String lotNumber) { return null; } });

我的问题是:在应用程序向列表中添加新元素之前,应用程序如何检查具有特定属性的对象是否已存在?那么,在recyclerView中没有重复的元素?
活动更新
private void setUpRecyclerView() { RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyler_view_antam_incoming); bagLotNumberAdapter = new BagLotNumberAdapter(this, bagLotNumbers); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setAdapter(bagLotNumberAdapter); recyclerView.setHasFixedSize(true); ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteBagLotNumberCallback(bagLotNumberAdapter)); itemTouchHelper.attachToRecyclerView(recyclerView); }

新适配器
public class BagLotNumberAdapter extends RecyclerView.Adapter< BagLotNumberAdapter.BagLotNumberViewHolder> {Context context; private ArrayList< BagLotNumberModel> dataList; public BagLotNumberAdapter(Context c, ArrayList< BagLotNumberModel> dataList) { this.context = c; this.dataList = dataList; }ArrayList< BagLotNumberModel> newList = new ArrayList< BagLotNumberModel> (); private ArrayList< BagLotNumberModel> removeDuplicates(ArrayList< BagLotNumberModel> list) { for (BagLotNumberModel element : list) { if (!newList.contains(element)) { newList.add(element); } } return newList; }@NonNull @Override public BagLotNumberViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View view = layoutInflater.inflate(R.layout.adapter_antam_bag_layout_number, parent, false); return new BagLotNumberViewHolder(view); }@Override public void onBindViewHolder(@NonNull BagLotNumberViewHolder holder, int position) {removeDuplicates(dataList); holder.txtId.setText(String.valueOf(dataList.get(position).getId())); holder.txtBagNumber.setText(String.valueOf(dataList.get(position).getBagNumber())); holder.txtLotNumber.setText(dataList.get(position).getLotNumber()); }@Override public int getItemCount() { return dataList.size(); }class BagLotNumberViewHolder extends RecyclerView.ViewHolder { TextView txtId, txtBagNumber, txtLotNumber; BagLotNumberViewHolder(View itemView) { super(itemView); txtId = (TextView) itemView.findViewById(R.id.txt_id_bag); txtBagNumber = (TextView) itemView.findViewById(R.id.txt_bag_number); txtLotNumber = (TextView) itemView.findViewById(R.id.txt_lot_number); } }public Context getContext() { return context; }private BagLotNumberModel mRecentlyDeletedItem; private int mRecentlyDeletedItemPosition; void deleteItem(int position) { mRecentlyDeletedItem = dataList.get(position); mRecentlyDeletedItemPosition = position; dataList.remove(position); notifyItemRemoved(position); showUndoSnackBar(); }private void showUndoSnackBar() { String defaultContextName = context.getClass().getSimpleName(); View view = null; switch (defaultContextName) { case "AntamIncomingActivity": view = (View) ((AntamIncomingActivity) context).findViewById(R.id.coordinator_layout_antam_incoming); break; default: break; }assert view != null; Snackbar snackbar = Snackbar.make(view, "Undo ?", Snackbar.LENGTH_LONG); snackbar.setAction("Undo ", v -> undoDelete()); snackbar.show(); }private void undoDelete() { dataList.add(mRecentlyDeletedItemPosition, mRecentlyDeletedItem); notifyItemInserted(mRecentlyDeletedItemPosition); }}

截图
< ?xml version="1.0" encoding="utf-8"?> < android.support.constraint.ConstraintLayout 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".activity.antam.AntamIncomingActivity" tools:showIn="@layout/activity_antam_incoming"> < android.support.v7.widget.RecyclerView android:id="@+id/recyler_view_antam_incoming" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="8dp" android:scrollbars="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> < /android.support.constraint.ConstraintLayout>

Android - 使用ArrayList作为Holder防止RecyclerView中的重复项

文章图片

更新2
Android - 使用ArrayList作为Holder防止RecyclerView中的重复项

文章图片

答案在将arrayList传递给适配器之前,您可以使用removeDuplicates函数。
bagLotNumberAdapter = new BagLotNumberAdapter(removeDuplicates(bagLotNumbers));

removeDuplicates函数
// Create a new ArrayList ArrayList< BagLotNumberModel> newList = new ArrayList< BagLotNumberModel> (); public ArrayList< BagLotNumberModel> removeDuplicates(ArrayList< BagLotNumberModel> list) { for (BagLotNumberModel element : list) {if (!newList.contains(element)) { newList.add(element); } } return newList; }

【Android - 使用ArrayList作为Holder防止RecyclerView中的重复项】编辑
newList = removeDuplicates(dataList); holder.txtId.setText(String.valueOf(newList.get(position).getId())); holder.txtBagNumber.setText(String.valueOf(newList.get(position).getBagNumber())); holder.txtLotNumber.setText(newList.get(position).getLotNumber());


    推荐阅读