title: How to filter a RecyclerView, within a fragment, which is within a ViewPager
body: <p>I want to use a SearchView to Filter my RecylerView,(the RecyclerView) which is within a fragment, which is within a ViewPager, the ViewPager is within a custom layout, and this custom layout is included in the main activity layout.</p>
  
  <p>I have implemented the filtering logic in the RecyclerView adapter.</p>
  
  <p>What I want to know is where to call the filter method, and how to get reference to the RecyclerView.</p>
  
  <p>This is the adapter class for the RecyclerView: </p>
  
  <pre><code>public class cardAdapter extends RecyclerView.Adapter&lt;cardAdapter.noteHolder&gt; implements Filterable {
  
  private List&lt;singleNoteDetail&gt; myNotesList;
  private List&lt;singleNoteDetail&gt; myNotesListFull;
  private OnItemClickListenerMyNotes listener;
  
  
  
  public static class noteHolder extends RecyclerView.ViewHolder {
      protected static TextView titleField;
      protected static TextView dateField;
  
      public noteHolder(View v) {
          super(v);
          titleField = v.findViewById(R.id.singleNoteTitle);
          dateField = v.findViewById(R.id.singleNoteDate);
      }
      public void bind(final singleNoteDetail item, final OnItemClickListenerMyNotes listener) {
          itemView.setOnClickListener(new View.OnClickListener() {
              @Override public void onClick(View v) {
                  listener.onItemClick(item);
              }
          });
      }
  }
  public cardAdapter(List&lt;singleNoteDetail&gt; myNotesList,OnItemClickListenerMyNotes listener) {
      this.myNotesList = myNotesList;
      this.myNotesListFull = new ArrayList&lt;&gt;(myNotesList);
      this.listener = listener;
  }
  
  @NonNull
  @Override
  public noteHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      View itemView = LayoutInflater.from(viewGroup.getContext())
              .inflate(R.layout.single_note_card, viewGroup, false);
      return new noteHolder(itemView);
  }
  
  @Override
  public void onBindViewHolder(@NonNull noteHolder viewHolder, int i) {
      singleNoteDetail singleNoteDetail = myNotesList.get(i);
      noteHolder.titleField.setText(singleNoteDetail.title);
      noteHolder.dateField.setText(singleNoteDetail.date);
      viewHolder.bind(myNotesList.get(i), listener);
  }
  
  
  @Override
  public int getItemCount() {
      return myNotesList.size();
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  @Override
  public int getItemViewType(int position) {
      return position;
  }
  
  @Override
  public Filter getFilter() {
      return filter;
  
  }
  
  private Filter filter = new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence constraint) {
          List&lt;singleNoteDetail&gt; filteredList = new ArrayList&lt;&gt;();
          if(constraint == null || constraint.length() == 0){
              filteredList.addAll(myNotesListFull);
          }else{
              String pattern = constraint.toString().toLowerCase().trim();
              for(singleNoteDetail item : myNotesListFull){
                  if(item.getTitle().toLowerCase().contains(pattern)){
                      filteredList.add(item);
                  }
              }
          }
          FilterResults results = new FilterResults();
          results.values = filteredList;
          return results;
      }
  
      @Override
      protected void publishResults(CharSequence constraint, FilterResults results) {
          myNotesList.clear();
          myNotesList.addAll((List)results.values);
          notifyDataSetChanged();
      }
  };
  
  
  }
  </code></pre>
  
  <p>This is the fragment class: </p>
  
  <pre><code>public class MyNotes extends Fragment {
  
  SharedPreferences prefs;
  
  RecyclerView recyclerView;
  
  SearchView searchView;
  
  public RecyclerView.LayoutManager layoutManager;
  public List&lt;singleNoteDetail&gt; cardList;
  public RecyclerView.Adapter adapter;
  ProgressBar progressBar;
  
  
  // TODO: Rename parameter arguments, choose names that match
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  private static final String ARG_PARAM1 = "param1";
  private static final String ARG_PARAM2 = "param2";
  
  // TODO: Rename and change types of parameters
  private String mParam1;
  private String mParam2;
  
  private OnFragmentInteractionListener mListener;
  
  public MyNotes() {
      // Required empty public constructor
  }
  
  public void syncMyNotes(){
  
      //
  
  }
  public static MyNotes newInstance(String param1, String param2) {
      MyNotes fragment = new MyNotes();
      Bundle args = new Bundle();
      args.putString(ARG_PARAM1, param1);
      args.putString(ARG_PARAM2, param2);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (getArguments() != null) {
          mParam1 = getArguments().getString(ARG_PARAM1);
          mParam2 = getArguments().getString(ARG_PARAM2);
  
  
      }
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      prefs = getActivity().getSharedPreferences("Username",Context.MODE_PRIVATE);
  
      View view = inflater.inflate(R.layout.fragment_my_notes, container, false);
  
      recyclerView = view.findViewById(R.id.myNotesList);
  
      progressBar = view.findViewById(R.id.progress);
  
  
  
      layoutManager = new LinearLayoutManager( getActivity());
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
  
  
      syncMyNotes();
  
  
      adapter = new cardAdapter(cardList, new OnItemClickListenerMyNotes() {
          @Override
          public void onItemClick(singleNoteDetail item) {
              Intent i = new Intent(getActivity(),NoteEditingActivity.class);
              i.putExtra("noteTitle",item.title);
              startActivity(i);
          }
      });
      recyclerView.setAdapter(adapter);
      return view;
  }
  
  public void onButtonPressed(Uri uri) {
      if (mListener != null) {
          mListener.onFragmentInteraction(uri);
      }
  }
  
  @Override
  public void onAttach(Context context) {
      super.onAttach(context);
      if (context instanceof OnFragmentInteractionListener) {
          mListener = (OnFragmentInteractionListener) context;
      } else {
          throw new RuntimeException(context.toString()
                  + " must implement OnFragmentInteractionListener");
      }
  }
  
  @Override
  public void onAttachFragment(Fragment fragment) {
      // TODO Auto-generated method stub
      super.onAttachFragment(fragment);
  
      Toast.makeText(getActivity().getApplicationContext(), String.valueOf(fragment.getId()), Toast.LENGTH_SHORT).show();
  
  }
  
  @Override
  public void onDetach() {
      super.onDetach();
      mListener = null;
  }
  public interface OnFragmentInteractionListener {
      // TODO: Update argument type and name
      void onFragmentInteraction(Uri uri);
  }
  }
  </code></pre>
  
  <p>And here is the layout which contains the SearchView and the ViewPager</p>
  
  <p>existing_notes_layout.xml :</p>
  
  <pre><code>&lt;LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/customborder"
  android:orientation="vertical"
  android:gravity="center_horizontal"&gt;
  
  &lt;android.support.design.widget.TabLayout
      android:id="@+id/tablayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorPrimary"
      android:minHeight="?attr/actionBarSize"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
  
  &lt;/android.support.design.widget.TabLayout&gt;
  
  &lt;SearchView
      android:id="@+id/searchNotes"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      &gt;
  
  &lt;/SearchView&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
  &lt;/android.support.v4.view.ViewPager&gt;
  </code></pre>
  
  <p></p>