从RecyclerView中删除项目后,TextView上的删除线保持在同一行

时间:2019-03-06 02:15:39

标签: android android-recyclerview recycler-adapter

我在RecyclerView上有一个项目列表,当您选择“ onLongclick”项目时,项目名称上方会出现删除线。 我的问题是,当我删除项目(并调用notifyDataSetChanged())时,删除线仍保留在与删除的上一个项目完全相同的行中,但是现在删除了一个新项目。例如:

我的列表在删除项目之前

  1. 列出项目A
  2. 列表项B |项目B代表删除线
  3. 列表项C
  4. 列出项目D

我的列表项目删除后

  1. 列出项目A
  2. 列表项C |意外的项目具有删除线
  3. 列出项目D

当我离开页面并重新打开它时,项目将正确显示,没有出现不需要的删除线,这在我的代码中已经确定,但是我不知道如何在第一次删除删除线时执行此操作。我是应用程序开发的新手,但我感觉我只是想念一个显而易见的解决方案而已,也许不愿意……在此先感谢您。

P.s。我的理解是,即使数据发生更改,删除线仍会保留在完全相同的recycleViewHolder上,但是我不确定在不关闭和打开活动的情况下如何撤消删除线。

class ListRecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {


    public TextView nameView;
    public TextView categoryView;

    private ItemClickListener itemClickListener;

    public ListRecyclerViewHolder(@NonNull View itemView) {
        super(itemView);


        nameView = itemView.findViewById(R.id.li_name_view);
        categoryView = itemView.findViewById(R.id.li_category_view);

        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    public void setItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;

    }
    @Override
    public void onClick(View v) {
        itemClickListener.Onclick(v,getAdapterPosition(),false);
    }

    @Override
    public boolean onLongClick(View v) {
        itemClickListener.Onclick(v,getAdapterPosition(),true);
        return true;
    }

    public void SetStrike(TextView text,boolean strike){
        if (strike) {
            text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            text.setTextColor(Color.GRAY);
        } else {
            text.setPaintFlags(text.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
            text.setTextColor(Color.BLACK);
        }
    }
}

public class ListItemAdaptor extends RecyclerView.Adapter<ListRecyclerViewHolder> {

    private List<Inventory> mAllItems = new ArrayList<>();

    public Context context;

    public int selectedItem;

    private List<Inventory> allCrossedItemsOnList = new ArrayList<Inventory>();

    private boolean checkIfEmpty = false;

    public void setItemsAdaptor(List<Inventory> allItems,Context context){
        if(allItems.isEmpty() || allItems == null)
        {
            //No item in the list
            Inventory item = new Inventory();
            item.setName("List Empty!");
            item.setCategory("");
            allItems.add(item);
            this.mAllItems = allItems;
            this.context = context;
            this.allCrossedItemsOnList = null;
            checkIfEmpty = true;
            //when called this updates all items displayed on the list.
            this.notifyDataSetChanged();
            return;
        }
        this.mAllItems = allItems;
        this.context = context;
        this.allCrossedItemsOnList = FindAllCrossedItems();
        checkIfEmpty = false;
        //when called this updates all items displayed on the list.
        notifyDataSetChanged();
        Log.d("TAG","Data Updated Adaptor");


    }

    @NonNull
    @Override
    public ListRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(R.layout.list_item_tab,viewGroup, false);
        return new ListRecyclerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull  final ListRecyclerViewHolder recyclerViewHolder, int i) {


        recyclerViewHolder.nameView.setText(mAllItems.get(i).getName());
        recyclerViewHolder.categoryView.setText(mAllItems.get(i).getCategory());

        //Set the item if they are crossed or not when page is opened.
        if(!checkIfEmpty && allCrossedItemsOnList != null){
            for (Inventory item: allCrossedItemsOnList){
                if(mAllItems.get(i).getId() == item.getId()){
                    recyclerViewHolder.SetStrike(recyclerViewHolder.nameView,true);
                }
            }
        }
        else if (!checkIfEmpty && recyclerViewHolder.nameView.getPaint().isStrikeThruText()) {
            recyclerViewHolder.SetStrike(recyclerViewHolder.nameView,false);
        }

        recyclerViewHolder.setItemClickListener(new ItemClickListener() {
            @Override
            public void Onclick(View view, int position, boolean isLongClicked) {
                if (!checkIfEmpty) {
                    if (isLongClicked) {
                        Toast.makeText(context, "Selected: " + mAllItems.get(position).getName(), Toast.LENGTH_SHORT).show();
                        //selectedItem = position;
                        if (!recyclerViewHolder.nameView.getPaint().isStrikeThruText()) {
                            recyclerViewHolder.SetStrike(recyclerViewHolder.nameView,true);
                            allCrossedItemsOnList.add(mAllItems.get(position));
                            mAllItems.get(position).setCrossedOnList(true);
                            MainActivity.myAppLocalDatabase.myDao().updateItem(mAllItems.get(position));
                        } else {
                            recyclerViewHolder.SetStrike(recyclerViewHolder.nameView,false);
                            allCrossedItemsOnList.remove(mAllItems.get(position));
                            mAllItems.get(position).setCrossedOnList(false);
                            MainActivity.myAppLocalDatabase.myDao().updateItem(mAllItems.get(position));
                        }
                    } else {
                        Toast.makeText(context, "Clicked: " + mAllItems.get(position).getName(), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mAllItems.size();
    }

    public int getSelectedItem(){return selectedItem;}
    public List<Inventory> getCrossedItems(){return allCrossedItemsOnList;}
    public void setCrossedItems(List<Inventory> crossed) {
        this.allCrossedItemsOnList = crossed;
    }
    //When page opened checks items (my data set) for which items are crossed already.
    private List<Inventory> FindAllCrossedItems(){
        List<Inventory> itemsCrossed = new ArrayList<Inventory>();
        for(Inventory item : mAllItems)
        {
            if (item.isCrossedOnList()) {
                itemsCrossed.add(item);
            }
        }

        return itemsCrossed;
    }
    //Updated database of all the crossed out items on the list that have been deleted.
    public void RemoveAllCrossedItems(){
        for (Inventory item: allCrossedItemsOnList) {
            Log.d("TAG","To be removed "+item.getName());
            item.setCrossedOnList(false);
            item.setAddToList(false);
            MainActivity.myAppLocalDatabase.myDao().updateItem(item);

        }
    }
}

0 个答案:

没有答案