Skip to content

Instantly share code, notes, and snippets.

@MatthieuLJ
Created November 29, 2012 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthieuLJ/4166640 to your computer and use it in GitHub Desktop.
Save MatthieuLJ/4166640 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#0077cc">
<FrameLayout
android:id="@+id/menuPlace"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" />
<FrameLayout
android:id="@+id/place3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/menuPlace" />
<FrameLayout
android:id="@+id/place2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/place3" />
<FrameLayout
android:id="@+id/place1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/place2" />
</RelativeLayout>
package com.matthieu;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class FragAccordion extends FragmentActivity {
private final static String TAG = "FragAccordion";
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.frag_fragaccordion);
getSupportFragmentManager().beginTransaction()
.add(R.id.menuPlace, new Fragment0()).commit();
}
public static class Fragment0 extends ListFragment {
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
FragmentActivity fa = getActivity();
FragmentTransaction ft = fa.getSupportFragmentManager()
.beginTransaction();
LinearLayout main_linear = (LinearLayout) getActivity().findViewById(R.id.main_linear);
int position_to_remove = 3; //right-most pane
int containing_id = main_linear.getChildAt(position_to_remove).getId();
Fragment old_fragment = (Fragment) getActivity().findViewById(containing_id).getTag(R.id.contained_fragment);
if (old_fragment != null) {
ft.remove(old_fragment);
}
main_linear.removeViewAt(position_to_remove);
FrameLayout fl = new FrameLayout(getActivity());
fl.setId(containing_id);
fl.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1));
ft.setCustomAnimations(android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
if (position == 0) {
ft.add(containing_id, new Fragment1()).commit();
} else if (position == 1) {
ft.add(containing_id, new Fragment2()).commit();
} else {
ft.add(containing_id, new Fragment3()).commit();
}
main_linear.addView(fl, 1);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, new String[] {
"Fragment1", "Fragment2", "Fragment3" }));
}
}
public static class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button b = new Button(getActivity());
b.setBackgroundColor(Color.GREEN);
b.setText("Fragment 1");
return b;
}
}
public static class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button b = new Button(getActivity());
b.setBackgroundColor(Color.BLUE);
b.setText("Fragment 2");
return b;
}
}
public static class Fragment3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button b = new Button(getActivity());
b.setBackgroundColor(Color.BLACK);
b.setText("Fragment 3");
return b;
}
}
}
<resources>
<item type="id" name="contained_fragment"/>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment