AppCompatActivity Android的PreferenceFragment

时间:2016-02-02 00:15:47

标签: java android android-viewpager preferenceactivity preferencefragment

我尝试使用标签创建Preferencefragment或PreferenceActivity。实际上我不能这样做,我的代码很简单: MainActivity

public class MainActivity extends AppCompatActivity {

    private ViewPagerAdapter mSectionsPagerAdapter;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        viewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FirstFragment(), "ONE");
        adapter.addFragment(new SecondFragment(), "TWO");
        adapter.addFragment(new ThirdFragment(), "THREE");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

和其中一个片段

public class FirstFragment extends Fragment{
    public FirstFragment () {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);

        return view;
    }

}

你可以看到它是空的。现在,我需要用preferencefragment替换片段。但我无法理解如何使用标签实现它们。我该怎么办?

1 个答案:

答案 0 :(得分:3)

不久前,这个would not have been possible,由于PreferenceFragments与支持库不兼容。使用最新版本的支持v7库,我们现在拥有PreferenceFragmentCompat类,它将在AppCompatActivity中运行,并且还可以与ViewPager和FragmentPagerAdapter一起使用。

由于您使用android.support.v4.app.FragmentPagerAdapter支持库片段,因此这是您当前配置的唯一选项。 请注意,可以将TabLayout与不支持的片段一起使用,但是您必须使用android.support.v13.app.FragmentPagerAdapter类,它允许使用不支持的片段。

因此,要保持当前配置并使其正常工作,请使PreferenceFragment扩展PreferenceFragmentCompat:

import android.os.Bundle; 
import android.support.v7.preference.PreferenceFragmentCompat; 
import android.view.View; 

public class MySettingsPrefFragment extends PreferenceFragmentCompat { 

    public MySettingsPrefFragment() { 
        // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        addPreferencesFromResource(R.xml.fragment_settings_pref); 
    } 

    @Override 
    public void onCreatePreferences(Bundle bundle, String s) { 

    } 
}

添加与PreferenceFragment对应的新标签:

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new FirstFragment(), "ONE");
    adapter.addFragment(new SecondFragment(), "TWO");
    adapter.addFragment(new ThirdFragment(), "THREE");
    adapter.addFragment(new MySettingsPrefFragment(), "SETTINGS"); //added
    viewPager.setAdapter(adapter);
}

而且应该是它!