AndroidAPS/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java

70 lines
2 KiB
Java
Raw Normal View History

2016-06-04 17:28:05 +02:00
package info.nightscout.androidaps.tabs;
2016-08-05 23:54:03 +02:00
import android.content.Context;
2017-01-21 23:48:05 +01:00
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
2016-06-12 14:18:21 +02:00
import android.support.annotation.Nullable;
2016-06-04 17:28:05 +02:00
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
2016-06-13 22:53:41 +02:00
import android.support.v4.app.FragmentStatePagerAdapter;
2017-04-21 18:56:48 +02:00
import android.view.ViewGroup;
2016-06-04 17:28:05 +02:00
import java.util.ArrayList;
2016-06-14 23:45:55 +02:00
import info.nightscout.androidaps.interfaces.PluginBase;
2016-06-04 17:28:05 +02:00
/**
* Created by mike on 30.05.2016.
*/
2016-06-13 22:53:41 +02:00
public class TabPageAdapter extends FragmentStatePagerAdapter {
2016-06-04 17:28:05 +02:00
2016-08-05 23:54:03 +02:00
ArrayList<PluginBase> visibleFragmentList = new ArrayList<>();
2016-06-13 22:53:41 +02:00
2016-08-05 23:54:03 +02:00
Context context;
2016-06-04 17:28:05 +02:00
2016-08-05 23:54:03 +02:00
public TabPageAdapter(FragmentManager fm, Context context) {
2016-06-04 17:28:05 +02:00
super(fm);
2016-08-05 23:54:03 +02:00
this.context = context;
2016-06-04 17:28:05 +02:00
}
@Override
2016-06-12 14:18:21 +02:00
@Nullable
2016-06-04 17:28:05 +02:00
public Fragment getItem(int position) {
2016-08-05 23:54:03 +02:00
//Fragment fragment = (Fragment) visibleFragmentList.get(position);
return Fragment.instantiate(context, visibleFragmentList.get(position).getFragmentClass());
2016-06-04 17:28:05 +02:00
}
2017-04-21 18:56:48 +02:00
@Override
public void finishUpdate(ViewGroup container) {
try{
super.finishUpdate(container);
} catch (NullPointerException nullPointerException){
System.out.println("Catch the NullPointerException in FragmentStatePagerAdapter.finishUpdate");
}
}
2016-06-04 17:28:05 +02:00
@Override
public CharSequence getPageTitle(int position) {
2017-01-21 23:48:05 +01:00
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if(preferences.getBoolean("short_tabtitles", false)){
return visibleFragmentList.get(position).getNameShort();
}
return visibleFragmentList.get(position).getName();
2016-06-04 17:28:05 +02:00
}
@Override
public int getCount() {
2016-06-13 22:53:41 +02:00
return visibleFragmentList.size();
2016-06-04 17:28:05 +02:00
}
2016-08-05 23:54:03 +02:00
public void registerNewFragment(PluginBase plugin) {
2016-07-11 14:04:57 +02:00
if (plugin.isVisibleInTabs(plugin.getType())) {
2016-06-13 22:53:41 +02:00
visibleFragmentList.add(plugin);
notifyDataSetChanged();
}
2016-06-13 22:53:41 +02:00
}
2017-04-21 18:56:48 +02:00
2016-06-04 17:28:05 +02:00
}