Closed thib3113 closed 9 years ago
Hello @thib3113 .
I will not more code to be able to help you. I see you're using the same fragment for different views. So far so good. But I need to know how you're showing it in your activity. I need your transaction code so I can check how you're placing it.
my transaction code ? i don't know what it is, so i create a new github repo : https://github.com/thib3113/MenuTest you can see all my code
I see. You have everything but you don't seem to know how to work with fragments. Before continuing I suggest you read some tutorials about them.
As for the onNavigationDrawerItemSelected
method I posted a code in my blog that could help you getting started:
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch (position) {
case 0: //search//todo
break;
case 1: //stats
fragment = getFragmentManager().findFragmentByTag(StatsFragment.TAG);
if (fragment == null) {
fragment = new StatsFragment();
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment, StatsFragment.TAG).commit();
break;
case 2: //my account //todo
break;
case 3: //settings //todo
break;
}
}
Depending on the position you need to check if that fragment is already added by calling findFragmentByTag
. If it is null that means there is no fragment so we need to create a new one fragment = new StatsFragment();
. Next you create a transaction that add the fragment to your container view.
in your statFragment i don't know what is :
public static final String TAG = "stats";
i don't found this tag in your xml :/
( sorry i'm a very noob in android, i have 3 lesson, and a project ... and i'm bad in frontend :P )
No problem. We all need to start from somewhere.
That is just a tag. That will identify the fragment in the FragmentManager (which works like a bag where you put all your fragments). Whenever we need to create a new fragment we will check the bag if it exists and if so use it, otherwise, create a new one.
Your tag can be whatever you like. I suggest you use the same name as the fragment to be easily identified.
thank you, i have this error :
"must implement OnFragmentInteractionListener"
it was an error with your project or just with fragments ?
No. That's just an error with your activity. Your fragments are altered and will not work if the activity doesn't implement OnFragmentInteractionListener
.
However you seem to have an interface declaration on all your fragments:
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
You shouldn't do this because this will create different types of interfaces but all with the same name. Remove all declarations but one. Or if you want to do it better, remove all and place that declaration on your MainActivity. Then implement that interface on the activity:
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks,OnFragmentInteractionListener
This will make you implement a method on the activity that will be used to communicate between fragments and your activity. Once again I must say, there seems to be a lot of stuff you don't know. You're not getting better by just asking linear and easy stuff like that. I suggest that you learn some simple tutorials on how to work with fragments.
Happy codding ;)
yes i know, i like android dev so i come to learn this, but i need to make a project in one week for my lesson :/, thank you for this help, i try when i can
sorry for question but i'm a noob in Android developpment, i try to follow my lesson when i have this :
for change fragment when i click on basic drawer fragment, i try this with your code :
but i don't know how i can have a Viewgroup for the inflater .
Thank you for the time you use to read me.