键入时我的编辑文本非常缓慢。我找到了很多解决方案,但没有任何帮助。要按名称过滤我的recyclerview项目,我编写以下代码:

private void editSearchListener() {
    editSearch.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) {
        adapter.getFilter().filter(charSequence);
        return;

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});
}


过滤器类别:

public class CustomFilter extends Filter {
    private MyAdapter adapter;
    private List<Data> filterList;

    public CustomFilter(List<Data> filterList, MyAdapter adapter) {
        this.filterList = filterList;
        this.adapter = adapter;
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results=new FilterResults();
        if(constraint != null && constraint.length() > 0) {
            constraint=constraint.toString().toUpperCase();
            List<Data> filteredPlayers = new ArrayList<>();
            for (int i=0;i<filterList.size();i++) {
                if(filterList.get(i).getName().toUpperCase().startsWith((String)constraint)) {
                    filteredPlayers.add(filterList.get(i));
                }
            }
            results.count=filteredPlayers.size();
            results.values=filteredPlayers;
        } else {
            results.count=filterList.size();
            results.values=filterList;
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        adapter.mData= (ArrayList<Data>) results.values;
        adapter.notifyDataSetChanged();
    }
}


而我的适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.DataViewHolder> implements Filterable {

    public List<Data> mData;
    private OnItemClickListener itemClickListener;
    private CustomFilter filter;


    public MyAdapter(List<Data> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public DataViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.data_item, viewGroup, false);
        DataViewHolder dataViewHolder = new DataViewHolder(v);
        return dataViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull DataViewHolder dataViewHolder, int i) {
        dataViewHolder.tvId.setText(String.valueOf(mData.get(i).getId()));
        dataViewHolder.tvName.setText(mData.get(i).getName());
        dataViewHolder.tvCreated.setText(mData.get(i).getCreatedAt());
    }

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

    public class DataViewHolder extends RecyclerView.ViewHolder{
        private   TextView tvId;
        private   TextView tvName;
        private  TextView tvCreated;

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

            tvId = itemView.findViewById(R.id.tv_data_id);
            tvName = itemView.findViewById(R.id.tv_data_name);
            tvCreated = itemView.findViewById(R.id.tv_data_created);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(itemClickListener != null) {
                        itemClickListener.onClick(mData != null && mData.size() != 0 ? mData.get(getAdapterPosition()): null);
                    }
                }
            });

        }
    }

    public interface OnItemClickListener {
        void onClick(Data data);
    }

    public void setOnItemClickListener (OnItemClickListener onItemClickListener) {
        this.itemClickListener = onItemClickListener;
    }

    @Override
    public Filter getFilter() {
        if(filter==null) {
            filter=new CustomFilter(mData,this);
        }
        return filter;
    }
}


但是当我输入一些文本时,它的工作是如此缓慢。但是,如果我删除了addTextChangeListener事件,那么它将正常工作。如何优化代码,请帮帮我。

最佳答案

希望这对您有所帮助。

代替在adapter.getFilter().filter(charSequence);中调用onTextChangedafterTextChanged中将其替换

private void editSearchListener() {
   editSearch.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) {
            return;
     }

    @Override
    public void afterTextChanged(Editable editable) {
             adapter.getFilter().filter(charSequence);
     }
   });
}


由于onTextChanged在文本更改期间运行,因此它可能运行缓慢。

尝试afterTextChanged,它将在更改文本后立即运行。

关于android - 如此缓慢地过滤recyclerview的项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54743802/

10-12 00:38