1

I have two fragments.

1) A fragment with just a TextView(OneFragment)

2) A fragment with a recycler view who's contents are being populated in the onCreateView() of the Fragment (Two Fragment)

In the Activity class I use a viewPager to control these fragments!!

However I'm faced with the problem.

The onCreateView() method of the TwoFragment is called when the activity is first loaded! When I go to the other fragments and return back to the Two Fragment the onCreateView() method is not called.

Instead of showing me a recyclerView it renders a blank screen ! However when I change my orientation of the device! The onCreateView() is called and and the list is populated!

I want it to show the TwoFragment while returning back to it.

However the OneFragment has no problems of this ! Any time I return to it, The normal big textView is shown!

Here is the Code for my ViewPager Adapter

    class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
    super(manager);
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
}


@Override
public int getItemPosition(Object object){
   // return PagerAdapter.POSITION_NONE;
    Fragment f = (Fragment) object;
    for(Fragment frag: mFragmentList){
      return frag.getTagInt();
    }
    return FragmentStatePagerAdapter.POSITION_NONE;

}

@Override
public int getCount() {

    return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);

}

public void clearAll(){
    mFragmentList.clear();
    mFragmentTitleList.clear();
    notifyDataSetChanged();
}

@Override
public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
}

}

And I set The ViewPager adapter like this :

      private void setupViewPager(ViewPager viewPager,int choice) {
    if(choice == 1){
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.clearAll();
        adapter.addFragment(new OneFragment(), "One");
        adapter.addFragment(new TwoFragment(), "Two");
        viewPager.setAdapter(adapter);

    }
    else{
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.clearAll();
        adapter.addFragment(new OneFragment(), "One");
        adapter.addFragment(new OneFragment(), "Two");
        viewPager.setAdapter(adapter);


    }

}

and here's my previous getItemId()

     @Override
public int getItemPosition(Object object){
   // return PagerAdapter.POSITION_NONE;
    Fragment f = (Fragment) object;
    for(int i=0;i<getCount();i++){
        Fragment fragment = getItem(i);
        if(f.equals(fragment)){
            return i;
        }
    }
    return FragmentStatePagerAdapter.POSITION_NONE;

}

Any kind of help is Much Appreciated ! Cause I'm kinda stuck after going through the web and stackOverflow!!

1 Answers1

0

You should not return POSITION_NONE in getItemPosition () for every request. POSITION_NONE means you're telling the pager adapter that this particular fragment is removed from the list and please modify the pages/fragments accordingly. When you return this, adapter will delete the fragment from its own private list of fragments. This is returned when you want to remove the fragment.

Send the position of the fragment using the indexof method of list interface.

See this post for more info.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
  • I've changed my getItemPosition() code and still the same problem persists!! I've updated my code!! Still the problem persists! Anything that I did wrong? – Jason Bakthakumar Nov 25 '15 at 06:46
  • It's not good practice to use getItem() in your code. That should be called only by framework. why can't you simply use index of method from list interface. I'm not sure if your equals () works add you expected here. I think it's not. – cgr Nov 25 '15 at 07:51
  • I changed the getItem() method and got it to work!! I just figured out that the onCreateView() is indeed being invoked ! But the recyclerView is not being populated ! I also tried initializing the adapter in the onCreate() method of the fragment! Only if I change my screen orientaion it's being populated and shown! Any sloution for that!! – Jason Bakthakumar Nov 25 '15 at 09:59
  • You mean getItem() method or removed getItem() from getItemPosition() ? – cgr Nov 25 '15 at 10:01
  • I mean I removed the getItem() method from the getItemPosition() and used tags in fragments to solve it up!! – Jason Bakthakumar Nov 25 '15 at 10:03
  • It works fine for all the other fragments with just the textView and ImageView ! But the fragment with the RecyclerView is not working properly!! – Jason Bakthakumar Nov 25 '15 at 10:03
  • may I know where tags helped ? What is the issue now ? Still it is blank or what ? Explain in detail and you may paste relevant code to understand better. – cgr Nov 25 '15 at 10:07
  • I managed to solve the ViewPager problem!! But now I'm faced with an even bigger problem where other fragements with normal text and ImageView are working fine! But the fragment with the recycler view is not showing when i traverse back to it!! – Jason Bakthakumar Nov 25 '15 at 15:29
  • However the recycler view is shown when i change the orientation!! In the log I'm getting a warning which says requestLayout() method is improperly called by the NavigationMenu! !! – Jason Bakthakumar Nov 25 '15 at 15:33
  • "Fragment with recycler view is not shown when swiping back from other fragment" - this is the problem you ever had. Now - does it show the blank screen or no screen at all ? Paste your modified getItemPosition(). – cgr Nov 25 '15 at 15:34
  • As far as the onCreateView() of the recyclerViewFragment is concerned ! The app does go there!! I found it by using a Log() statement!! So the real problem lies in the fragment with the recyclerView !! – Jason Bakthakumar Nov 25 '15 at 15:35
  • Sure Will!! The getItemPosition()!! and it shows a blank screen! I can see a blank screen – Jason Bakthakumar Nov 25 '15 at 15:35
  • Do not modify the existing code. It'll confuse the guys who will read this post later..append to the existing question. – cgr Nov 25 '15 at 15:41
  • I'm kinda new here! Didn't consider that!! Did it!! Any suggestion bro!!! Cause I'm kinda stuck !! Any help is appreciated!! – Jason Bakthakumar Nov 25 '15 at 15:44
  • I think you getItemPosition is again wrong. Tag int is just the position of fragment? – cgr Nov 25 '15 at 15:52
  • Can user add or delete any page (fragment ) from pager dynamically? If not replace your code with this: return mFragmentList.indexof (object) and see. – cgr Nov 25 '15 at 15:56
  • The user can add and remove pages from the view Pager dynamically to modify the View!!And the indexOf() is not working! Just tried that! Still not working !! – Jason Bakthakumar Nov 25 '15 at 16:07
  • with your tag, you just make sure it returns the correct position. Put a log statement and see if it is retruning proper position when you swipe. Make sure that we can narrow the analysis. Try with all available solutions. – cgr Nov 25 '15 at 16:10
  • Could you manage to do add pet the above comment? I was curious if you're struggling still. – cgr Dec 01 '15 at 19:50
  • Sorry couldn't tell ya, network's down here!! I figured a workaround bro!! I'm all good!! Solved it! Thank you so much for your intrest and help!! @cgr ... – Jason Bakthakumar Dec 05 '15 at 17:06
  • That's awesome. Add your own answer with solution. Our if you think my answer helped vote it up. .it'll be helpful for somebody. – cgr Dec 05 '15 at 17:14
  • Yeah Sure will!! But need to have 15 reputation to vote up :( :(... Will do as soon as I get there!! Thanks again!! – Jason Bakthakumar Dec 05 '15 at 17:25