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?
Walkin, Preferencefragmentcompat supports actionbar?
– Geison Santos
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.
– Geison Santos
Unfortunately not directly, and I don’t think it makes sense either*. The
ActionBar
belongs toAppCompatActivity
, is most used in terms ofToolbar
. Soon you need to delegate the responsibility of manipulating to yourActivity
, but nothing prevents you from accessing directly using the methodgetActivity
classFragment
.– Wakim
If you already use these libs, perfect :D
– Wakim
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?
– Geison Santos
Sorry, I’m rushing. I already have Toolbar, because this fragment is loaded by Drawerlayout. I will test your solution.
– Geison Santos
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 interfaceOnBackStackChangedListener
that will make your code much more uncoupled.– Wakim
@Walkin, error is occurring
Must specify preferenceTheme in theme
. How the theme should be spoken?– Geison Santos
In my answer is how to solve this problem :)
– Wakim