自定义微调器不过滤recycleview

pcww981p  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(251)

使用自定义微调器根据产品直径筛选产品列表。选择直径后,列表将完全清除。我曾经通过searchview成功地过滤过一个recyclerview,但是我想在这里实现一些不同的东西。
活动

initList();
        diameterSpinner = findViewById(R.id.diameter_spinner);
        spinnerAdapter = new SpinnerAdapter(this, diametersList);
        diameterSpinner.setAdapter(spinnerAdapter);

diameterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Diameters diameterFilter = (Diameters) parent.getItemAtPosition(position);
                String clickedDiameter = diameterFilter.getProductDiameter();
                dAdapter.getFilter().filter(clickedDiameter);
                dAdapter.notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

微调器的自定义适配器。从youtube上的“流中编码”中得到了这个代码。

public class SpinnerAdapter extends ArrayAdapter<Diameters> {

    public SpinnerAdapter(Context context, ArrayList<Diameters> productDiameters) {
        super(context, 0, productDiameters);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return initView(position, convertView, parent);
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return initView(position, convertView, parent);
    }

    private View initView(int position, View convertView, ViewGroup parent){
        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.diameter_spinner_row, parent,false);
        }
        TextView diameterSetting = convertView.findViewById(R.id.spinner_text);

        Diameters current = getItem(position);

        diameterSetting.setText(current.getProductDiameter());
        return convertView;
    }
}

initlist()用于通过自定义适配器将直径列表传递给微调器。

private void initList(){
        diametersList = new ArrayList<>();
        diametersList.add(new Diameters("1 1/8"));
        diametersList.add(new Diameters("2 1/8"));
        diametersList.add(new Diameters("2 7/8"));
        diametersList.add(new Diameters("3 1/2"));
        diametersList.add(new Diameters("4 1/2"));
        diametersList.add(new Diameters("5 11/16"));
        diametersList.add(new Diameters("7 5/8"));
        diametersList.add(new Diameters("8 5/8"));
        diametersList.add(new Diameters("11 3/4"));
        diametersList.add(new Diameters("12 3/4"));

自定义recyclerview适配器

public class DynamicAdapter extends RecyclerView.Adapter<DynamicAdapter.DynamicViewHolder> implements Filterable {
    private ArrayList<Inventory> displayedList;
    //for filter
    private ArrayList<Inventory> displayedListFull;

    public static class DynamicViewHolder extends RecyclerView.ViewHolder {
        public TextView productName;
        public TextView productCode;

        public DynamicViewHolder(@NonNull View itemView) {
            super(itemView);
            productName = itemView.findViewById(R.id.dynamic_name_view);
            productCode = itemView.findViewById(R.id.dynamic_code_view);

        }

    }

    public DynamicAdapter(ArrayList<Inventory> inventoryList){
                displayedList = inventoryList;
                displayedListFull = new ArrayList<>(displayedList);
    }

    @NonNull
    @Override
    public DynamicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dynamic_text_row_view, parent, false);
        DynamicViewHolder dynamicViewHolder = new DynamicViewHolder(v);
        return dynamicViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull DynamicViewHolder holder, int position) {
        Inventory currentItem = displayedList.get(position);
        holder.productCode.setText(currentItem.getProductCode());
        holder.productName.setText(currentItem.getProductName());

    }

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

    //FILTER
    @Override
    public Filter getFilter() {
        return displayFilter;
    }

    private Filter displayFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Inventory> filteredList = new ArrayList<>();

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

                for (Inventory item : displayedListFull){
                    if (item.getProductName().toLowerCase().contains(filterPattern)){
                        filteredList.add(item);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            displayedList.clear();
            displayedList.addAll((List)results.values);
            notifyDataSetChanged();
        }
    };
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题