Load layout with Preferencefragment in a Drawerlayout

Asked

Viewed 123 times

0

I’m developing an app that uses DrawerLayout. The opening of each menu item is performed 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 must extend from android.app.Fragment, that is, it is not possible to replace fragmentTransaction.replace(R.id.container_body, fragment) for PreferenceFragment does not extend from the same base class.

How to load a class that extends Preferencefragment in a Drawerlayout?

1 answer

0

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

Using the new PreferenceFragmentCompat

Using the PreferenceFragmentCompat of preference-v7 is quite simple.

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

You need to import the library preference-v7, placing the declaration of dependence on your build.gradle:

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

An example of use:

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 need to define an attribute in your theme:

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

Source: https://stackoverflow.com/questions/32070670/preferencefragmentcompat-requires-preferencetheme-to-be-set

Only that implies some things:

The 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 with the styles and design of Material Design, which the AppCompat tail.

This is because when using version 23, you are required to change your compileSdkVersion and your targetSdkVersion to 23. And there begin some problems.

To not extend too much, when giving support to version 23, you need to take great care with the new permissions model that generates a different way of structuring certain flows of your app.

Using a port of PreferenceFragment

As an alternative, there is the library android-support-v4-preferencefragment 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, what remains is to use this port.

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

The way of use is the same. Instead of your Fragment inherit from PreferenceFragmentCompat (in the first case), he will inherit from android.support.v4.preference.PreferenceFragment. And the rest stay the same.

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

  • Walkin, Preferencefragmentcompat supports actionbar?

  • Regarding the libs com.android.support:appcompat-v7:23.0.1, com.android.support:support-v4:23.0.1 and com.android.support:recyclerview-v7:23.0.1, I already use them.

  • Unfortunately not directly, and I don’t think it makes sense either*. The ActionBar belongs to AppCompatActivity, is most used in terms of Toolbar. Soon you need to delegate the responsibility of manipulating to your Activity, but nothing prevents you from accessing directly using the method getActivity class Fragment.

  • If you already use these libs, perfect :D

  • I think I’m gonna need to ask another question. But anyway, how do you present the title of Toolbar and the "home" button to return to the previous screen? You use Preferencefragmentcompat or use another approach?

  • Sorry, I’m rushing. I already have Toolbar, because this fragment is loaded by Drawerlayout. I will test your solution.

  • As a matter of fact, I haven’t used this one yet preference-v7, but it seems pretty simple (sometimes there are some hidden tricks hehe). But yes, it is another question. And I think to make the change from "Menu Drawer" to "Arrow" I recommend using the interface OnBackStackChangedListener that will make your code much more uncoupled.

  • @Walkin, error is occurring Must specify preferenceTheme in theme. How the theme should be spoken?

  • In my answer is how to solve this problem :)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.