Load layout with PreferenceFragment in a DrawerLayout

0

I'm developing an app that uses DrawerLayout . The opening of each menu item is done according to the code below:

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

    ...

    private void displayView(int position) {
        Fragment fragment = null;
        String title = getString(R.string.app_name);
        switch (position) {
            case 0:
                //Abre o primeiro item
                break;
            case 1:
                //Abre o segundo item
                break;
            case 2:
                //Abre o terceiro item
                break;
            case 3:
                fragment = new ConfiguracoesFragment();
                title = getString(R.string.title_configuracoes);
                break;
            case 4:
                title = getString(R.string.title_desconectar);
                startActivity(new Intent(this, InicialActivity.class));
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.commit();
            getSupportActionBar().setTitle(title);
        }
    }
}

Each menu item is a class that extends from android.support.v4.app.Fragment . The ConfiguracoesFragment file also extends from the same class. To use PreferenceFragment , I should extend it from android.app.Fragment , that is, it is not possible to make substitution in fragmentTransaction.replace(R.id.container_body, fragment) because PreferenceFragment does not extend from the same base class.

How to load a class that extends PreferenceFragment into a DrawerLayout?

    
asked by anonymous 19.09.2015 / 22:22

1 answer

0

There are two ways to use Fragment of support library to display Preferences .

Using the new PreferenceFragmentCompat

Using PreferenceFragmentCompat of preference-v7 is quite simple.

But this brings a small problem, which depending on the scenario is impractical.

You need to import the preference-v7 library by placing the dependency statement on your build.gradle :

compile 'com.android.support:preference-v7:23.0.1'

An example usage:

public class PrefsFragment extends PreferenceFragmentCompat {

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

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
}

A small detail that is not documented, but you must define an attribute in your theme:

<style name="SettingsTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

Source: link

But this implies some things:

preference-v7 depends on three other libraries:

com.android.support:appcompat-v7:23.0.1
com.android.support:support-v4:23.0.1
com.android.support:recyclerview-v7:23.0.1

If you already use these libraries, no problem. Otherwise you need to evaluate these changes.

One advantage is that all components will be in the style and design of Material Design, which AppCompat brings.

That's because when using version 23, you're forced to change your compileSdkVersion and its targetSdkVersion to 23. And then some problems begin.

Not to extend much, while supporting version 23, you need to be very careful with the new template of permissions that generates a different way to structure certain streams of your app.

Using a port of PreferenceFragment

As an alternative, there's the android-support-v4-preferencefragment library that adds a compatibility layer.

It is not an official version of AOSP, but if the first solution is not possible, there is no way, it's all about using this port.

  

One recommendation I give is to try to avoid making the most use of it, because it relies heavily on aspects of the Preferences internal API, using a lot Reflection to accomplish much of the work. This is bad because if something changes in an upcoming release, you will end up discovering it at runtime.

The form of use is the same. Instead of its Fragment inheriting from PreferenceFragmentCompat (in the first case), it will inherit from android.support.v4.preference.PreferenceFragment . And the rest remains the same.

First of all, the components of this library will not be in the style of Material Design that AppCompat brings to all versions after 7.

    
20.09.2015 / 02:35