简体   繁体   English

如何在具有两个不同视图持有者的异构recyclerview中实现文件管理器(搜索)?

[英]How to implement filer (search) in a heterogeneous recyclerview with two different viewholders?

Here is the code I am not able to implement search in heterogeneous recyclerview with two different viewholders. 这是我无法使用两个不同的Viewholder在异构recyclerview中实现搜索的代码 Can anyone suggest me something. 谁能给我建议。 Moreover my views of recyclerview are textViews and cardViews. 此外,我对recyclerview的看法是textViews和cardViews。 CardView is having data written on it for example name, age and phone number. CardView上面写有数据,例如姓名,年龄和电话号码。 I want to search with the name and phone number. 我想搜索姓名和电话号码。 Nothing with the date textView. 日期textView没什么。

See publishResults image 参见publishResults图片

Thanks in advance. 提前致谢。

Here is my code : 这是我的代码:

            if (charString.isEmpty()) {
                // Search is empty/Nothing Entered
                mAppointmentsList = mAppointmentsListFiltered;
            } else {
                List<DataAppointments> filterResults = new ArrayList<>();

                for (DataAppointments data : mAppointmentsList) {
                    if (data instanceof DataPatientDetails) {
                        DataPatientDetails details = (DataPatientDetails) data;

                        if (details.getName().toLowerCase().contains(charString.toLowerCase()) ||
                                details.getPhone().toLowerCase().contains(charString.toLowerCase()) ||
                                details.getId().toLowerCase().contains(charString.toLowerCase())) {
                            filterResults.add(data);

                            if (BuildConfig.DEBUG)
                                Log.d(TAG, "performFiltering: " + details.getName());
                        }
                    }
                }

                mAppointmentsListFiltered = filterResults;
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "performFilteringPatientListFiltered: " + mAppointmentsListFiltered);
            }

            FilterResults results = new FilterResults();
            results.values = mAppointmentsListFiltered;
            results.count = mAppointmentsListFiltered.size();
            if (BuildConfig.DEBUG)
                Log.d(TAG, "performFilteringResultsUpcoming: " + results.count);
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mAppointmentsListFiltered = (ArrayList<DataAppointments>) results.values;
            if (BuildConfig.DEBUG) Log.d(TAG, "publishResults: " + mAppointmentsListFiltered);
            notifyDataSetChanged();
        }
    };
}`

And my onQueryChangeListener : 还有我的onQueryChangeListener:

    // Query Search in Material Search view
private void querySearch() {
    if (getActivity() != null)
        mSearchView = getActivity().findViewById(R.id.search_view_upcoming);
    mSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if (BuildConfig.DEBUG) Log.d(TAG, "onQueryTextSubmit: " + query);
            mAdapter.getFilter().filter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (BuildConfig.DEBUG) Log.d(TAG, "onQueryTextChange: " + newText);
            mAdapter.getFilter().filter(newText);
            if (BuildConfig.DEBUG) Log.d(TAG, "onQueryTextChange: " + mAdapter.getItemCount());

            return false;
        }
    });
}

Search is not depends upon view, it is depend upon data. 搜索不依赖于视图,它依赖于数据。 Apply filer upon your list according to your criteria and differentiate your view with viewType. 根据您的条件将Filer应用于列表,并使用viewType区分视图。

Based on the code you posted. 根据您发布的代码。

in the else statement, I am not exactly sure what is being compared. 在else语句中,我不确定要比较什么。 You can have condition to check if DataAppointments is of type DataPatientDetails and then do your comparision. 您可以有条件检查DataAppointments是否为DataPatientDetails类型,然后进行比较。 I am assuming mAppointmentsList is an actual list of data. 我假设mAppointmentsList是实际的数据列表。

else {
 for (DataAppointments data : mAppointmentsList) {
  if (data instanceof DataPatientDetails) {
    DataPatientDetails details = (DataPatientDetails) data;
    // do your comparision here, if it passes, then add to the filtered results
  }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM