如何在ViewPager中使用PreferenceFragment?

时间:2014-08-01 21:07:53

标签: java android xml android-fragments android-viewpager

我无法弄清楚如何让PreferenceFragment正常工作。我有一个ViewPager连接到FragmentPagerAdapter,有两个片段,用户可以在它们之间滑动。我想要获得"设置"菜单工作,使用PreferenceFragment,但我不确定我做错了什么。当我点击“设置”时,视图将变为空白屏幕。

我的SettingsFragment类:

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

我的PagerAdapter类:

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);
    }

}

我的主要课程,当用户点击时会创建PreferenceFragment&#34;设置&#34;

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);
    }
}

我的偏好.XML

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

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <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>

以及活动的XML:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
/>

1 个答案:

答案 0 :(得分:0)

在你的getitem案例1中,需要在默认值

之前列出