1

I have a fragment that implements SearchView.OnQueryTextListenerinside a viewPager. the onQueryTextChange is correctly called each time the search text changes (I can log it it works fine). The problem is that when I try to call a public function of my RecyclerView adapter like :

@Override
    public boolean onQueryTextChange(String newText) {
        Log.i("", "search newText " + newText);

        if(mMembersRecyclerAdapter!=null){
            mMembersRecyclerAdapter.filterSearch(newText);
        }
        //((MembersRecyclerAdapter) mRecyclerView.getAdapter()).filterSearch(newText);

        return true;
    }

It's always null, even when my adapter is definitly not null as my RecyclerView is working correctly... Same thing for my mRecyclerView, always null..

I'm not sure what part of my code would be usful to be displayed in this topic. Feed free to ask me what you would like to see.

-- Edit, the way I add my Listener, it's obviously not optimal, maybe the issue comes from there :

In the activity that contains the view pager that contains the fragment :

  @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        switch (mFragmentSelected){
            case PAGE_MEMBERS:
                getMenuInflater().inflate(R.menu.main_pager_member_page, menu);
                MenuItem searchItem = menu.findItem(R.id.search);
                SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
                searchView.setOnQueryTextListener((MainPagerMembersFragment) mViewPagerAdapter.getItem(PAGE_MEMBERS));
                break;
            default:
                getMenuInflater().inflate(R.menu.main_pager, menu);
                break;
        }


        return true;
    }

Thanks,

Renaud Favier
  • 1,645
  • 1
  • 17
  • 33
  • 1
    How do you initialize the mMembersRecyclerAdapter reference? – user Nov 18 '15 at 11:35
  • on the onCreateView, is there a better place to do so ? – Renaud Favier Nov 18 '15 at 11:35
  • 1
    I'm assuming you're not doing anything to make the getItem() method of the adapter to return the proper fragments, right? Maybe this will help http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager – user Nov 18 '15 at 12:05
  • yeah you're right the getItem() method sends a new instance of my fragment even if it has already been instanciated – Renaud Favier Nov 18 '15 at 12:27

1 Answers1

0

It came indeed from the way I had coded my getItem(),

now getItem look like that :

@Override
        public Fragment getItem(int position) {
            if(!fragments.containsKey(position)){
                switch (position) {
                    case PAGE_FEED:
                        fragments.put(PAGE_FEED, MainPagerNewsFeed.newInstance());
                    case PAGE_MEMBERS:
                        fragments.put(PAGE_MEMBERS, MainPagerMembersFragment.newInstance());
                    case PAGE_NOTIFS:
                        fragments.put(PAGE_NOTIFS, MainPagerNotifsFragment.newInstance());
                    default:
                        break;
                }
            }

            return fragments.get(position);

        }

and i have this

private ArrayMap<Integer, Fragment> fragments = new ArrayMap<>(NUM_ITEMS);

as a private member of my ViewPager adapter

thanks Luksprog for pointing that out.

Renaud Favier
  • 1,645
  • 1
  • 17
  • 33