Reputation: 4396
I'm using SlidingMenu with a ListView as a menu and I want to change the content of my MainActivity dynamically based on the click of a ListView element. I know that I have to use Fragments to achieve this.
I have a PlayerFragment with nothing in it yet:
public class PlayerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_player_fragment, container, false);
}
}
And my standard MainActivity with a SlidingMenu in it:
public class MainActivity extends SlidingFragmentActivity {
private ListView mainListView;
private ArrayAdapter<String> listAdapter;
private SlidingMenu menu;
protected ListFragment mFrag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(8);
menu.setFadeDegree(0.8f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindWidth(600);
menu.setMenu(R.layout.menu_frame);
mainListView = (ListView) findViewById(R.id.listView1);
String[] planets = new String[] { "News", "Android", "Control",
"About" };
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll(Arrays.asList(planets));
listAdapter = new ArrayAdapter<String>(this,
R.layout.custom_list_element, R.id.title, planetList);
mainListView.setAdapter(listAdapter);
mainListView.setSelector(R.layout.listselector);
mainListView
.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = String.valueOf(mainListView
.getItemAtPosition(position));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onBackPressed() {
if (menu.isMenuShowing()) {
menu.toggle();
} else {
super.onBackPressed();
}
}
}
How do I call the standard fragment when the app starts? And how do I change the fragment based on the click of the ListView?
I thought of something like this:
if (fragment == null) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.RELATIVEVIEW_ID_HERE, new BasicFragment());
ft.commit();
}
and to change the Fragment this:
Fragment fg = NewsFragment.newInstance();
getFragmentManager().beginTransaction().add(R.id.RELATIVEVIEW_ID_HERE, fg).commit();
Is this correct?
Thanks in advance!
Upvotes: 2
Views: 3027
Reputation: 28470
You can use ft.replace(...)
for all your Fragment
changes, including displaying the first one, like so:
FragmentTransaction ft = fm.beginTransaction();
MyFragment myFragment = myFragment.newInstance();
ft.replace(android.R.id.content, myFragment).commit();
Upvotes: 2