何在ViewPager中使用PreferenceFragment

何在ViewPager中使用PreferenceFragment

本文介绍了如何在ViewPager中使用PreferenceFragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何使PreferenceFragment正常工作.我有一个ViewPager连接到FragmentPagerAdapter,其中有两个可以在用户之间滑动的Fragment.我正在尝试使用PreferenceFragment使设置"菜单正常工作,但是我不确定自己在做什么错.当我点击设置"时,视图将变为空白屏幕.

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.

我的SettingsFragment类:

My SettingsFragment class:

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

我的PagerAdapter类:

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

}

我的主类,当用户点击设置"时应创建PreferenceFragment

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

我的首选项.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"
/>

推荐答案

在您的getitem情况下,需要在默认值之前列出1

In your getitem case 1 needs to be listed before the default

这篇关于如何在ViewPager中使用PreferenceFragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:00