我正遇到一个问题-删除RecyclerView项目后尝试刷新搜索结果。

现在,当我过滤掉项目并删除其中一项时,RecyclerView再次显示所有其他项目,而未应用搜索过滤器。
但是,如何实现搜索过滤器,以便即使RecyclerView列表已更改也能保持打开状态?

这是我在主要活动中加载数据的方式:

private void loadData(final int ordernumber) {

    orderLineViewModel.getAllUnCheckedSingleOrderLines(ordernumber).observe(this, new Observer<List<OrderLine>>() {
        @Override
        public void onChanged(List<OrderLine> orderLines) {

            orderLineListAdapter.setOrderLines(orderLines);
        }
    });
}


这是我主要活动中的SearchView代码:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            orderLineListAdapter.getFilter().filter(s);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            orderLineListAdapter.getFilter().filter(s);
            return false;
        }
    });


这是我在RecyclerViewAdapter中的过滤器:

public void setOrderLines(List<OrderLine> orderLines) {

    orderLineList = orderLines;
    orderLineListFull = new ArrayList<>(orderLines);

    notifyDataSetChanged();
}

@Override
public Filter getFilter() {
    return orderFilter;
}

private Filter orderFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        List<OrderLine> filteredList = new ArrayList<>();

        if (charSequence == null || charSequence.length() == 0){
            filteredList.addAll(orderLineListFull);
        }else {
            String filterPattern = charSequence.toString().toLowerCase().trim();

            for (OrderLine orderLine: orderLineListFull){
                if (orderLine.getProductCode().toLowerCase().contains(filterPattern)){
                    filteredList.add(orderLine);
                }
            }
        }

        FilterResults results = new FilterResults();
        results.values = filteredList;

        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        orderLineList.clear();
        orderLineList.addAll((List)filterResults.values);
        notifyDataSetChanged();
    }
};


在RecyclerView项目中按复选框:

@Override
    public void onClick(View view) {

        int position = getAdapterPosition();
        OrderLine line = orderLineList.get(position);

        switch (view.getId()) {

            case R.id.foam_in_checkbox:

                if (okCheckBox.isChecked()) {

                    line.setIsArrived(1);
                    orderLineViewModel.update(line);
                    orderLineList.remove(position);
                    notifyItemRemoved(position);

                } else {
                    line.setIsArrived(0);
                    orderLineViewModel.update(line);
                }
                break;
        }
    }


编辑:添加了整个RecyclerViewAdapter:

public class OrderLineListAdapter extends

RecyclerView.Adapter<OrderLineListAdapter.OrderLineViewHolder> implements Filterable {

private Context context;
private final LayoutInflater orderLineInflater;
private List<OrderLine> orderLineList;
private List<OrderLine> orderLineListFull;
private OrderLineViewModel orderLineViewModel;

public OrderLineListAdapter(Context context) {
    this.context = context;
    orderLineInflater = LayoutInflater.from(context);
    orderLineViewModel = ViewModelProviders.of((FoamInActivity) context).get(OrderLineViewModel.class);
}

@NonNull
@Override
public OrderLineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = orderLineInflater.inflate(R.layout.foam_in_single_line, parent, false);

    return new OrderLineViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull OrderLineViewHolder orderLineViewHolder, int position) {

    if (orderLineList != null) {

        OrderLine current = orderLineList.get(position);

        orderLineViewHolder.productCodeTextView.setText(current.getProductCode());
        orderLineViewHolder.orderedQuantityTextView.setText(String.valueOf(current.getOrderedQuantity()));

        orderLineViewHolder.okCheckBox.setOnCheckedChangeListener(null);
        orderLineViewHolder.okCheckBox.setChecked(false);

    } else {
        orderLineViewHolder.productCodeTextView.setText(R.string.no_open_positions);
    }
}

public void setOrderLines(List<OrderLine> orderLines) {

    orderLineList = orderLines;
    orderLineListFull = new ArrayList<>(orderLines);

    notifyDataSetChanged();
}

@Override
public int getItemCount() {

    if (orderLineList != null) {
        return orderLineList.size();
    } else {
        return 0;
    }
}

public class OrderLineViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView productCodeTextView, orderedQuantityTextView;
    public CheckBox okCheckBox;

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

        productCodeTextView = itemView.findViewById(R.id.foam_in_product_code);
        orderedQuantityTextView = itemView.findViewById(R.id.foam_in_quantity);
        okCheckBox = itemView.findViewById(R.id.foam_in_checkbox);

        okCheckBox.setOnClickListener(this);
        productCodeTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        int position = getAdapterPosition();

        OrderLine line = orderLineList.get(position);

        switch (view.getId()) {

            case R.id.foam_in_checkbox:

                if (okCheckBox.isChecked()) {

                    line.setIsArrived(1);
                    orderLineViewModel.update(line);
                    orderLineList.remove(position);
                    notifyItemRemoved(position);

                } else {
                    line.setIsArrived(0);
                    orderLineViewModel.update(line);
                }

                break;

            case R.id.foam_in_product_code:
                Toast.makeText(context, "Status: " + line.getIsArrived(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

@Override
public Filter getFilter() {
    return orderFilter;
}

private Filter orderFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        List<OrderLine> filteredList = new ArrayList<>();

        if (charSequence == null || charSequence.length() == 0){
            filteredList.addAll(orderLineListFull);
        }else {
            String filterPattern = charSequence.toString().toLowerCase().trim();

            for (OrderLine orderLine: orderLineListFull){
                if (orderLine.getProductCode().toLowerCase().contains(filterPattern)){
                    filteredList.add(orderLine);
                }
            }
        }

        FilterResults results = new FilterResults();
        results.values = filteredList;

        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        orderLineList.clear();
        orderLineList.addAll((List)filterResults.values);
        notifyDataSetChanged();
    }
};

最佳答案

整个RecyclerViewAdapter:

public class OrderLineListAdapter extends RecyclerView.Adapter<OrderLineListAdapter.OrderLineViewHolder> implements Filterable {

private Context context;
private final LayoutInflater orderLineInflater;
private List<OrderLine> orderLineList;
private List<OrderLine> orderLineListFull;
private OrderLineViewModel orderLineViewModel;

public OrderLineListAdapter(Context context) {
    this.context = context;
    orderLineInflater = LayoutInflater.from(context);
    orderLineViewModel = ViewModelProviders.of((FoamInActivity) context).get(OrderLineViewModel.class);
}

@NonNull
@Override
public OrderLineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = orderLineInflater.inflate(R.layout.foam_in_single_line, parent, false);

    return new OrderLineViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull OrderLineViewHolder orderLineViewHolder, int position) {

    if (orderLineList != null) {

        OrderLine current = orderLineList.get(position);

        orderLineViewHolder.productCodeTextView.setText(current.getProductCode());
        orderLineViewHolder.orderedQuantityTextView.setText(String.valueOf(current.getOrderedQuantity()));

        orderLineViewHolder.okCheckBox.setOnCheckedChangeListener(null);
        orderLineViewHolder.okCheckBox.setChecked(false);

    } else {
        orderLineViewHolder.productCodeTextView.setText(R.string.no_open_positions);
    }
}

public void setOrderLines(List<OrderLine> orderLines) {

    orderLineList = orderLines;
    orderLineListFull = new ArrayList<>(orderLines);

    notifyDataSetChanged();
}

@Override
public int getItemCount() {

    if (orderLineList != null) {
        return orderLineList.size();
    } else {
        return 0;
    }
}

public class OrderLineViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView productCodeTextView, orderedQuantityTextView;
    public CheckBox okCheckBox;

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

        productCodeTextView = itemView.findViewById(R.id.foam_in_product_code);
        orderedQuantityTextView = itemView.findViewById(R.id.foam_in_quantity);
        okCheckBox = itemView.findViewById(R.id.foam_in_checkbox);

        okCheckBox.setOnClickListener(this);
        productCodeTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        int position = getAdapterPosition();

        OrderLine line = orderLineList.get(position);

        switch (view.getId()) {

            case R.id.foam_in_checkbox:

                if (okCheckBox.isChecked()) {

                    line.setIsArrived(1);
                    orderLineViewModel.update(line);
                    orderLineList.remove(position);
                    notifyItemRemoved(position);

                } else {
                    line.setIsArrived(0);
                    orderLineViewModel.update(line);
                }

                break;

            case R.id.foam_in_product_code:
                Toast.makeText(context, "Status: " + line.getIsArrived(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

@Override
public Filter getFilter() {
    return orderFilter;
}

private Filter orderFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        List<OrderLine> filteredList = new ArrayList<>();

        if (charSequence == null || charSequence.length() == 0){
            filteredList.addAll(orderLineListFull);
        }else {
            String filterPattern = charSequence.toString().toLowerCase().trim();

            for (OrderLine orderLine: orderLineListFull){
                if (orderLine.getProductCode().toLowerCase().contains(filterPattern)){
                    filteredList.add(orderLine);
                }
            }
        }

        FilterResults results = new FilterResults();
        results.values = filteredList;

        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        orderLineList.clear();
        orderLineList.addAll((List)filterResults.values);
        notifyDataSetChanged();
    }
};


}

关于java - 更改RecyclerView项时刷新SearchView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59760484/

10-08 23:44