basically this is my app(idea) for a tablet landscape orientation: Two Fragments, left fragment is a listfragment populated by a resource.xml file (Got that working).
Right fragment is supposed to dynamically change fragment and layout based on which list item the user clicks. Googling so far told me that I need to programmatically add and remove fragments to a viewgroup to do that. Is that right?
Basically the question is/are:
- How do I create the viewgroup and where (Main.java or menufragment.java)?
- How do I put the dynamic "user clicked ID 3 on the list so add fragment 3 to the viewgroup"
- What do I add to my main.xml file? got the fragment for the listfragment in there, what to add for the dynamic viewgroup?
EDIT:
So this is what my activity looks like: Main.java
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
This is my listfragment MenuFragment.java
public class MenuFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.listfragment, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.listmenu)));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Main activity = (Main) getActivity();
}
}
and finally my main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/list"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_weight="1"
class="com.mwerner.fragmentstest.MainMenu" />
<View
android:id="@+id/contentview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/list" />
</RelativeLayout>
The string array I have in my xml file that populates the list is called "listmenu"
Please tell me where I need to put in the code you wrote down?