Skip to content Skip to sidebar Skip to footer

Calling Adapter From Fragments In ViewPager

I have Fragment which have a searchview and viewpager . My viewpager has 3 fragments and each fragment calling adapter . Now to implement searchview , i need to call adapter of vis

Solution 1:

With help of uploaded code I can tell that

make method in your fragments named search and call adapter filter method

public void search(String text)
{
adapter.filter(text)
}

you can check instance of fragment and call search method of fragment after this line

 Fragment fragment = adapter.getFragment(viewPager.getCurrentItem());

Solution 2:

I think the best design for this is to use the Activity as the central "hub" for events that need to go the fragments.

So this is how I would do this:

  • First start with a listener interface. This can be declared as an inner interface of the Activity:

    static interface QueryListener {
    
        void onQueryChange(String query);
    }
    
  • Activity gets properties for holding listeners and methods for adding and removing listeners (declared synchronized to prevent those pesky ConcurrentModificationExceptions:

    private List<QueryListener> mQueryListeners = new ArrayList<>();
    
    public synchronized void addQueryListener(@NonNull QueryListener listener) {
    
       // check if the listener was already added
       // you could also use a Set instead of a List for this
       if (mQueryListeners.contains(listener)) return;   
    
       mQueryListeners.add(listener);
    }
    
    public synchronized void removeQueryListener(@NonNull QueryListener listener) {
        mQueryListeners.remove(listener);
    }
    
  • Activity also gets a method that can be called by the fragment with the SearchView:

    public synchronized void changeQuery(String query) {
    
            for (QueryListener listener : mQueryListeners) {
                listener.onQueryChange(query);
            }
    }
    
  • Now your fragments with the adapters need to implement QueryListener and register/unregister themselves:

    Add this line somewhere in onCreate():

            ((YourActivity) getActivity()).addQueryListener(this);
    

    Unregister in onDestroy():

        @Override
        public void onDestroy() {
            ((YourActivity) getActivity()).removeQueryListener(this);
            super.onDestroy();
        }
    

    add the QueryListener method implementation:

    @Override
    public void onQueryChange(String query) {
    
        // if (isVisible()) {      if desired
        mAdapter.filter(query);
    }
    

    Note: You don't have to have the fragment implement QueryListener; you can use an anonymous class. But you'll have to hold the reference to it so you can call removeQueryListener() with it later.

  • Now just hook up the SearchView:

    @Override
    public boolean onQueryTextChange(String query) {
    
        ((YourActivity) getActivity()).changeQuery(query);
    }
    

Another pattern I've used is to create a dedicated class for registering/notifying listeners. I create a single instance of this class and have a method on the Activity to access it:

        QueryHandler handler = ((YourActivity) getActivity()).getQueryHandler();
        // QueryHandler has all the listener properties and methods shown for the Activity

        ...

        handler.changeQuery(query);

Alternatively, you can also use one of the many open-source event bus packages like Otto to pass events amongst your fragments.


Post a Comment for "Calling Adapter From Fragments In ViewPager"