Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

android - How to recreate all fragments in ViewPager:

I have this problem, I have a FragmentActivity with a ViewPager and a ViewPagerAdapter.

In this ViewPager I have 3 Fragments that each of them contains a TableLayout that gets populated programmaticly using a 2dArrayList.

on a click of a button in the FragmentActivity layout, I filter the data in the 2dArrayList and basically need to recreate all 3 Fragments with the new dataset (run the onCreateView method in all of them).

So I create an onClickListener for this button:

 private class SlicerSelectedOnClickListener implements OnClickListener {

    private ODSharedSlicer slicer;

    public SlicerSelectedOnClickListener(ODSharedSlicer slicer) {
        super();
        this.slicer = slicer;
    }

    public void onClick(View v) {
        String slicerValue = ((Button) v).getText().toString();
        Log.d(TAG,"Slicer value: "+ slicerValue +" chosen");
        application.currentReport.getODSharedSlicers().get(0).getSharedSlicerSelectedMemebers().add(slicerValue);

        ViewGroup parent = (ViewGroup) ((Button) v).getParent();

        for (int i = 0; i < parent.getChildCount(); i++) 
        {
            if ((parent.getChildAt(i) != v) && (parent.getChildAt(i) instanceof ToggleButton))
            {
                ((ToggleButton) parent.getChildAt(i)).setChecked(false);
            }
        }

        mPagerAdapter.notifyDataSetChanged();
    }
}

As you can see I have a mPagerAdapter.notifyDataSetChanged(); As suggested here:

How to recreate fragment with viewpager intentionally?

at the end of this method, So I expect the adapter to recreate all the Fragments, but the onCreateView from any of the fragments never runs.

Can any one suggest what am I doing wrong. any assistance would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

for those who still face the same problem which I faced the same problem when I have ViewPager with seven fragment. the default for these fragments to load the English content from the service but the problem here that I want to change the language from settings activity and after finish settings activity I want ViewPager in main activity to refresh the fragment to match the language selection from the user and load the Arabic content if user choose Arabic here what I did to work from the first trr :D

1. you must use FragmentStatePagerAdapter .*

  1. mainActivity I overrided the onResume and did the following

     if (!(mPagerAdapter == null)) {
    
             mPagerAdapter.notifyDataSetChanged();
    
    
         }
    
  2. I ovveride the getItemPosition() in mPagerAdapter and make it return POSITION_NONE.

     @Override
         public int getItemPosition(Object object) {
    
             return POSITION_NONE;
         }
    

works like charm


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...