RecyclerView项不过滤

时间:2019-02-20 08:28:24

标签: android android-recyclerview rx-java2

我需要按名称过滤recyclerview的项目。为此,我使用rxjava(反跳运算符),视图绑定。但是,当我在edittext中键入一些文本时,recyclerview不会被过滤。这是我的代码。主要活动:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String DATABASE_NAME = "dataDb";
    private ProgressBar progressBar;
    @BindView(R.id.rv_data_list)
     RecyclerView recyclerDataList;
    private MyAdapter adapter;
    private AppDatabase db;
    private SwipeRefreshLayout swipeRefreshToUpdateList;
    private TextView tvErrorMessage;
    private List<Data> listData;
    private CompositeDisposable cDisposable = new CompositeDisposable();
    private Unbinder unbinder;
    private Disposable disposable;
    @BindView (R.id.edit_search)
     EditText editSearch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);
        progressBar = findViewById(R.id.pb_wait);
        swipeRefreshToUpdateList = findViewById(R.id.srl_update_data_list);
        tvErrorMessage = findViewById(R.id.tv_error_text);
        Toolbar toolbar = findViewById(R.id.tb_toolbar_main);
        setSupportActionBar(toolbar);

        editSearch.setVisibility(View.VISIBLE);
    listData = new ArrayList<>();
    db =  Room.databaseBuilder(getApplicationContext(),
            AppDatabase.class, DATABASE_NAME).build();

    recyclerDataList.setLayoutManager(new LinearLayoutManager(this));
    adapter = new MyAdapter(listData);
    recyclerDataList.setAdapter(adapter);

    hideKeyboard();
    swipeRefreshListener();
    recyclerTouchListListener();
    disposable = db.dataDao().dataSize()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe((dbSize) -> {
            if(dbSize > 0) {
                fillListFromDb();

            }
            else
                getData();
        }, this :: onErrorShow
            , this :: onCompleteShow
            );

    cDisposable.add(RxTextView.textChangeEvents(editSearch)
            .skipInitialValue()
            .debounce(100, TimeUnit.MILLISECONDS)
            .filter(new Predicate<TextViewTextChangeEvent>() {
                @Override
                public boolean test(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
                    return TextUtils.isEmpty(textViewTextChangeEvent.text().toString()) || textViewTextChangeEvent.text().toString().length() > 2;
                }
            })
            .distinctUntilChanged()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(searchContacts()));


}

private DisposableObserver<TextViewTextChangeEvent> searchContacts() {
    return new DisposableObserver<TextViewTextChangeEvent>() {
        @Override
        public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
            Log.d(TAG, "Search query: " + textViewTextChangeEvent.text());
            adapter.getFilter().filter(textViewTextChangeEvent.text());
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, "onError: " + e.getMessage());
        }

        @Override
        public void onComplete() {

        }
    };
}

在我的适配器的getFilter方法中:

@Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    dataFiltered = mData;
                } else {
                    List<Data> filteredList = new ArrayList<>();
                    for (Data row : mData) {
                        if (row.getName().toLowerCase().contains(charString.toLowerCase()) ) {
                            filteredList.add(row);
                        }
                    }

                    dataFiltered = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = dataFiltered;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                dataFiltered = (ArrayList<Data>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

请帮助我。我是rxJava的新手,需要帮助。

0 个答案:

没有答案