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

时间:2018-06-29 03:42:43

标签: android android-recyclerview searchview

Here is the code 我无法在具有两个不同观看者的异构回收视图中实施搜索。谁能给我建议。此外,我对recyclerview的看法是textViews和cardViews。 CardView上面写有数据,例如姓名,年龄和电话号码。我想搜索姓名和电话号码。日期textView没什么。

See publishResults image

谢谢。

这是我的代码:

            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();
        }
    };
}`

还有我的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;
        }
    });
}

2 个答案:

答案 0 :(得分:0)

搜索不依赖于视图,它依赖于数据。根据您的条件将Filer应用于列表,并使用viewType区分视图。

答案 1 :(得分:0)

基于您发布的代码。

在else语句中,我不确定要比较什么。您可以有条件检查DataAppointments是否为DataPatientDetails类型,然后进行比较。我假设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
  }
 }
}