1

当我启动我的应用程序时,第一个活动是 TabsActivity,这是一个 FragmentActivity,在这个活动中有一个 ViewPager,我可以通过向左或向右滑动来更改片段。

但是当我旋转我的设备时,当前的片段显示得很好,如果我改变片段,列表不会被填充,如果我再次改变,列表会被正确填充。

这是我的 TabsActivity

public class TabsActivity extends SherlockFragmentActivity implements SearchView.OnQueryTextListener   {    
    private static ArrayList<String> CONTENT = new ArrayList<String>();

    private boolean refreshActivity = false;
    private boolean isConnected = true;

    private FragmentStatePagerAdapter adapter;
    private ViewPager pager;    


    @Override
    public void onResume(){
        super.onResume();

        refreshActivity();
    }

    @Override
    public void onStart(){
        super.onStart();

        refreshActivity();
    }

    private void refreshActivity(){
        if (refreshActivity == true && isConnected){
            Intent intent = new Intent(this, TabsActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
            startActivity(intent);

            refreshActivity = false;
        }
    }

    protected void onSaveInstanceState(Bundle bundle) {
        bundle.putSerializable("CONTENT", CONTENT);
        super.onSaveInstanceState(bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        File file = new File(getApplicationInfo().dataDir + "/databases/" + EventBDD.NOM_BDD);

        if (!AppStatus.getInstance(this).isOnline(this) && !file.exists()) { 

            isConnected = false;

            setContentView(R.layout.isoffline);
        }else{  

            setContentView(R.layout.tabs_layout);

            final SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

            if(savedInstanceState != null) {
                CONTENT = (ArrayList<String>) savedInstanceState.getSerializable("CONTENT");
            }else{              
                /*
                 * Generate tabs menu with preferences
                 */ 
                if (SP.getBoolean("displayConcert",true))
                    CONTENT.add("Concert");
                if (SP.getBoolean("displayConference",true))
                    CONTENT.add("Conférence");
                if (SP.getBoolean("displayTheatre",true))
                    CONTENT.add("Théatre");
                if (SP.getBoolean("displayDanse",true))
                    CONTENT.add("Danse");
                if (SP.getBoolean("displayJeunesse",true))
                    CONTENT.add("Jeunesse");
                if (SP.getBoolean("displayLoisir",true))
                    CONTENT.add("Loisir");
            }

            SP.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener(){
                @Override
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                    refreshActivity = true;                 
                }
            });

            adapter = new TabsAdapter(getSupportFragmentManager());

            pager = (ViewPager)findViewById(R.id.pager);
            TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);

            pager.setAdapter(adapter);
            indicator.setViewPager(pager);          
        }
    }


    class TabsAdapter extends FragmentStatePagerAdapter  {

        public TabsAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Item.newInstance(CONTENT.get(position % CONTENT.size()));
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT.get(position % CONTENT.size());
        }

        @Override
        public int getCount() {
          return CONTENT.size();
        }
    }
}

谢谢 ;)

4

0 回答 0