1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| public class TabsSessionActivity extends SherlockFragmentActivity {
private GeoLocatingService mGeoService;
private MyPagerAdapter mPagerAdapter;
private FragmentTabHost mTabHost;
private ViewPager mViewPager;
final IGeoLocatingServiceListener listener = new IGeoLocatingServiceListener() {
@Override
public void dataChanged(final Object data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
refreshCurrentFragment();
}
});
}
};
/************************** SERVICE CONNECTION **********************/
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("GeoService", "TabsSessionActivity:Disconnected!");
mGeoService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i("GeoService", "TabsSessionActivity:Connected!");
mGeoService = ((GeoLocatingService.LocalBinder) service).getService();
mGeoService.addListener(listener);
refreshCurrentFragment();
}
};
public GeoLocatingService getGeoLocatingService() {
return mGeoService;
}
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the view from main_fragment.xml
setContentView(R.layout.activity_tabs_session);
// Création de la liste de Fragments que fera défiler le PagerAdapter
List fragments = new Vector();
// Ajout des Fragments dans la liste
fragments.add(Fragment.instantiate(this,Dashboard_Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,OsmMapFragment.class.getName()));
fragments.add(Fragment.instantiate(this,Fragment1.class.getName()));
// Création de l'adapter qui s'occupera de l'affichage de la liste de
// Fragments
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
mViewPager = (ViewPager) super.findViewById(R.id.pager);
// Affectation de l'adapter au ViewPager
mViewPager.setAdapter(this.mPagerAdapter);
//Bind the title indicator to the adapter
LinePageIndicator LineIndicator = (LinePageIndicator)findViewById(R.id.indicator);
LineIndicator.setViewPager(mViewPager);
//Style Line Indicator
final float density = getResources().getDisplayMetrics().density;
LineIndicator.setSelectedColor(0xFF45A5D7);
LineIndicator.setUnselectedColor(0xFF888888);
LineIndicator.setStrokeWidth(4 * density);
LineIndicator.setLineWidth(30 * density);
}
public void refreshCurrentFragment(){
//Retrieve fragments instances
Dashboard_Fragment fragment1 = (Dashboard_Fragment) this.mPagerAdapter.getItem(0);
OsmMapFragment fragment2 = (OsmMapFragment) this.mPagerAdapter.getItem(1);
Fragment1 fragment3 = (Fragment1)this.mPagerAdapter.getItem(2);
/*Fragment1 fragment3 = (Fragment1) getSupportFragmentManager()
.findFragmentByTag("tab3");*/
//Check if fragment is active and visible
if (fragment1!=null){
if (fragment1.isVisible()){
fragment1.onDatachangedCallback();
}
}
if (fragment2!=null){
if (fragment2.isVisible()){
fragment2.onDatachangedCallback();
}
}
if (fragment3!=null){
if (fragment3.isVisible()){
fragment3.onDatachangedCallback();
}
}
}
@Override
public void onStart() {
super.onStart();
//On attache la vue au service
bindService(
new Intent(this, GeoLocatingService.class),
connection, BIND_AUTO_CREATE);
}
@Override
public void onStop() {
//On detache la vue du service
if(mGeoService!=null){
unbindService(connection);
}
super.onStop();
}
@Override
public void onResume() {
if(mGeoService!=null){
refreshCurrentFragment();
}
super.onResume();
}
@Override
public void onDestroy() {
if(ServiceTools.isGeoLocatingServiceRunning(this)){
stopService(new Intent(this, GeoLocatingService.class));
}
finish();
super.onDestroy();
} |
Partager