在SearchView [Inventory App]中搜索项目时,一经滑动就无法删除

时间:2018-09-17 14:56:06

标签: android android-recyclerview

实际上,我正在尝试制作用于库存的应用。

因此,在扫描了一些条形码后,我将它们添加到ArrayList中,然后在recyclerView中,我的recyclerView具有过滤方法和searchView,因此用户应该能够搜索投掷条形码,并且必须能够在滑动时删除搜索到的项目。

当我打开recyclerView中的dialogAlert并且删除项目时,一切正常,但是当我搜索并删除项目然后关闭searchView时,项目仍保留在recyclerView中。

我将在此处提供我的适配器代码,以及在“活动”中构建和删除项目的代码。

以下是来自活动的代码:

@SuppressLint("SetTextI18n")
@TargetApi(Build.VERSION_CODES.KITKAT)
public void alertDeleteSingleItem(final int position){
    final AlertDialog.Builder mBuilder = new AlertDialog.Builder(InventarioActivity.this);

    @SuppressLint("InflateParams") View mView = getLayoutInflater().inflate(R.layout.alert_confirm_delete, null);

    final Button yes = mView.findViewById(R.id.btnSI);
    final Button no = mView.findViewById(R.id.btnNO);
    final TextView text = mView.findViewById(R.id.textView);

    text.setText("ELIMINARE " + itemAdapter.getList().get(position).getCodiceArticolo() + " ?");

    mBuilder.setView(mView);
    final AlertDialog dialog = mBuilder.create();
    Objects.requireNonNull(dialog.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();


    yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            itemAdapter.removeItem(position);
            dialog.dismiss();
        }
    });

    no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });

}

public void buildVariableRecycler(View view) {
    recyclerView = view.findViewById(R.id.rectclerItems);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
    itemAdapter = new ItemAdapter(itemModel);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setAdapter(itemAdapter);


    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();


            if (direction == ItemTouchHelper.RIGHT) {
                alertDeleteSingleItem(position);
            }

            if (direction == ItemTouchHelper.LEFT) {
                alertDeleteSingleItem(position);
            }

        }
    }).attachToRecyclerView(recyclerView);

}

这是我的适配器:

    public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ExampleViewHolder>  implements Filterable {


    private ArrayList<ItemModel> variantiConstructors;
    private List<ItemModel> mFilteredList;


    @NonNull
    @Override
    public ItemAdapter.ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler,parent,false);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        v.setLayoutParams(lp);
        return new ItemAdapter.ExampleViewHolder(v);
    }

    ItemAdapter(ArrayList<ItemModel> exampleList){
        variantiConstructors = exampleList;
        mFilteredList = variantiConstructors;
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemAdapter.ExampleViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        ItemModel item = variantiConstructors.get(position);

        holder.desc.setText(item.getCodiceArticolo());
        holder.qta.setText(item.getQta());

        holder.qta.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                variantiConstructors.get(position).setQta(holder.qta.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });


        if(position % 2 == 0 ){
            holder.itemView.setBackgroundColor(Color.parseColor("#5C5C5C"));

        }else if(position % 2 == 1){
            holder.itemView.setBackgroundColor(Color.parseColor("#9E9E9E"));
        }
    }


    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults oReturn = new FilterResults();
                final ArrayList<ItemModel> results = new ArrayList<>();
                if (mFilteredList == null)
                    mFilteredList = new ArrayList<>(variantiConstructors);
                if (constraint != null && constraint.length() > 0) {
                    if (mFilteredList != null && mFilteredList.size() > 0) {
                        for (final ItemModel cd : mFilteredList) {
                            if (cd.getCodiceArticolo().toLowerCase()
                                    .contains(constraint.toString().toLowerCase()))
                                results.add(cd);
                        }
                    }
                    oReturn.values = results;
                    oReturn.count = results.size(); //newly Aded by ZA
                } else {
                    oReturn.values = mFilteredList;
                    oReturn.count = mFilteredList.size(); //newly added by ZA
                }
                return oReturn;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(final CharSequence constraint,
                                          FilterResults results) {
                variantiConstructors = new ArrayList<>((ArrayList<ItemModel>) results.values);
                notifyDataSetChanged();
            }
        };
    }

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

    public class ExampleViewHolder extends RecyclerView.ViewHolder {

        public TextView desc;
        public EditText qta;

        ExampleViewHolder(View itemView) {
            super(itemView);

            desc = itemView.findViewById(R.id.Desc);
            qta = itemView.findViewById(R.id.Qta);
        }
    }


    public ArrayList<ItemModel> getList(){
        return variantiConstructors;
    }


    public void removeItem(int position){

        variantiConstructors.remove(position);
        notifyDataSetChanged();
    }

}

做完一些测试后,当我搜索某个项目时,它已经滑动到了位置0,这就是问题所在,但是如何才能在该项目的正确位置上滑动呢?

3 个答案:

答案 0 :(得分:0)

检查getFilter()方法中的代码。您可以很容易地确定发生这种情况,因为过滤器将项目列表保留在临时列表中。并且在过滤过程中,主列表被切出。

从过滤列表中删除项目时,将从临时列表中删除而不是从主列表中删除。知道这一点之后,您也许还可以通过过滤后的列表以某种方式实现删除,但不会给我留下深刻的印象,因为getFilter()SearchView并不适合这种特定情况。

原因可能是删除更多是与后端相关的操作,而不是当前用户界面,更重要的是您是库存应用。

因此,最好的方式是通过更多控制来实现您真正想要的功能,而不是使用getFilter()SearchView。只需实现自己的自定义搜索视图,并使用相同的列表进行过滤,然后将删除与您的后端连接即可。我已经为类似的库存应用程序以这种方式完成此操作,并且发现它更加方便。

答案 1 :(得分:0)

您将要删除结果,因为它仍处于用于过滤器的临时数组列表中。 更新您的适配器方法,

public void removeItem(int position){

        variantiConstructors.remove(position);
        mFilteredList.remove(position);
        notifyDataSetChanged();
    }

答案 2 :(得分:0)

首先将这些行放在要删除项目的位置:

yourAdapter.remove(yourArrayList.get([INDEX]));
yourAdapter.notifyDataSetChanged();

然后,您可以再次拨打此行:

setListAdapter(yourAdapter);