How can I use a PreferenceFragment with a ViewPager?



I cannot figure out how to get PreferenceFragment to work correctly. I have a ViewPager hooked up to a FragmentPagerAdapter, with two Fragments that the user can swipe between. I am trying to get the "Settings" menu working, using a PreferenceFragment, but I am unsure what I'm doing wrong. When I tap Settings the view is changed to a blank white screen.


My SettingsFragment class:



public class SettingsFragment extends PreferenceFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}


}


My PagerAdapter class:



public class PagerAdapter extends FragmentPagerAdapter {

SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

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

@Override
public Object instantiateItem(ViewGroup container, int position){
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}

@Override
public Fragment getItem(int i) {
switch (i) {
// default case is also case 0 to avoid redundant code
default: return new CalculatorFragment();
case 1: return new TapeFragment();
}
}

@Override
public int getCount() {
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 1: return "Review";
default: return "Calculator";
}
}

public Fragment getRegisteredFragment(int position){
return registeredFragments.get(position);
}


}


My main class which should create the PreferenceFragment when user taps "Settings"



public class MangoCalc extends FragmentActivity implements CalculatorFragment.CalcTapeInterface{

PagerAdapter pagerAdapter;
ViewPager myViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mango_calc);

pagerAdapter = new PagerAdapter(getFragmentManager());
myViewPager = (ViewPager) findViewById(R.id.main);
myViewPager.setAdapter(pagerAdapter);

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
myViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
};

for (int i=0; i< pagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(pagerAdapter.getPageTitle(i))
.setTabListener(tabListener));
}

myViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener());
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mango_calc, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
FragmentManager fm = getFragmentManager();
SettingsFragment settingsFragment = new SettingsFragment();
fm.beginTransaction().replace(R.id.main, settingsFragment).commit();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onTapePass(ArrayList<String> data) {
if (data!=null) Log.d("Tape", "Last element got: "+data.get(data.size()-1));
TapeFragment tapeFragment = (TapeFragment) pagerAdapter.getRegisteredFragment(1);
tapeFragment.updateTape(data);
}


}


My preferences.XML



<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://ift.tt/nIICcg">
<PreferenceCategory android:title="@string/lookandfeel">
<ListPreference
android:key="theme_preference"
android:title="@string/theme"
android:summary="@string/summary_theme_preference"
android:entries="@array/entries_theme_preference"
android:entryValues="@array/entryvalues_theme_preference"
android:defaultValue="mango"/>
<SwitchPreference
android:key="keypress_vibration_preference"
android:title="@string/keypress_vibration"
android:summaryOn="@string/summaryon_keypress_vibration_preference"
android:summaryOff="@string/summaryoff_keypress_vibration_preference"
android:switchTextOn="@string/text_keypress_vibration_on"
android:switchTextOff="@string/text_keypress_vibration_off"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>


and the XML for the activity:



<android.support.v4.view.ViewPager xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
/>


Please help!


No comments:

Post a Comment