Reputation: 7618
I have followed a tutorial to create step by step a sliding menu and it works, but looking in the code I have noticed that when users touch a menu item the code will run this piece of code:
MainActivity:
....
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:{
fragment = new PersonaggiPrincipali();
break;
}
case 1:{
fragment = new PersonaggiSecondari();
break;
}
case 2:{
fragment = new Video();
break;
}
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
....
Basically it call a new Class
when the item is clicked.
The class is formed in this way:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PersonaggiPrincipali extends Fragment {
public PersonaggiPrincipali(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.personaggi_principali, container, false);
return rootView;
}
}
So it just return the layout and the displayView
function above will replace the current with the returned one, right?
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
....
}
My question is, in the view returned how can I manipulate it if the class is just a return of the layout?
For example, let's assume that in the returned view I have a Button
and I need to set a ClickListener or just create a function that will chenge the Activity (activity that should be insert in the main view with the SlidingMenu), How can I do that?
Should I write all the code in the MainActivity
to manage that?
Upvotes: 0
Views: 2657
Reputation: 4339
Ciao Christian, when you instantiate a Fragment
you don't get a view as a result but a Fragment
.
The onCreateView
method is just part of the lifecycle of a fragment and that's where you have to return the main view of your fragment. Also, if your fragment has a Button
you can set the listener in the onCreateView
method of the Fragment.
http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
Example:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.personaggi_principali, container, false);
Button button = (Button) rootView.findViewById(R.id.REPLACE_WITH_YOUR_BUTTON_ID);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// do you stuff here...
}
});
return rootView;
}
Please notice the Button in the example is just a local variable, if you need to access it later, it's better to declare it as a class variable.
Regarding communication (Fragment
to Fragment
, host Activity
to Fragment
), I think everything you need to know is well explained in the official documentation: http://developer.android.com/training/basics/fragments/communicating.html
Upvotes: 1
Reputation: 2614
You can either do it in the same (onCreateView()
) method. Additionally, you could include private references to Views inside your fragment layout in your PersonaggiPrincipali
class. Also implement get() methods for these Views and you can change them from your MainActivity
.
For example:
public class PersonaggiPrincipali extends Fragment {
private Button fragButton;
public PersonaggiPrincipali(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.personaggi_principali, container, false);
fragButton = (Button) rootView.findViewById(R.id.fragButtonId); //replace R.id.fragButtonId with the appropriate Id from your xml layout
return rootView;
}
public Button getFragButton() {
return this.fragButton;
}
}
Upvotes: 1