Jump to content

  1. 0
  2. 0
  3. 0/5

Rate this Topic

- - - - -

Nested Fragments Example for Android 4.2 API v17

  • Please log in to reply

OP JBirdVegas

JBirdVegas

java code monkey

Posted 14 November 2012 - 09:35 PM #1

Mod Type:: Application

Difficulty:: Moderate

Mod Status:: Stable

Apply In:: Other (See Description)

Requires Root:: No

Source::
****** ANDROID 4.2 API v17 REQUIRED ******

With the release of Android 4.2 API v17 Google introduced the ability to nest fragments within other fragments. This is important because we can now host any Fragment from another Fragment instead of from an Activity. I wanted to investigate this new API and get a feel for the implementation on a small scale before adding this functionality to a larger project.

Now some sample code from https://github.com/J...FragmentExample

MainActivity here all we need to do is inflate our layout
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// just load the fragments from xml
		setContentView(R.layout.main);
	}

res/layout/main.xml //xml defined fragments that share the screen equally
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
			  android:orientation="horizontal"
			  android:id="@+id/root_layout"
			  android:layout_width="fill_parent"
			  android:layout_height="fill_parent" >
	<fragment android:name="com.example.NestedFragmentsExample.LeftFrag"
			  android:id="@+id/left_fragment"
			  android:layout_weight="1"
			  android:layout_width="0dp"
			  android:layout_height="match_parent"/>
	<fragment android:name="com.example.NestedFragmentsExample.RightFrag"
			  android:id="@+id/right_fragment"
			  android:layout_weight="1"
			  android:layout_width="0dp"
			  android:layout_height="match_parent"/>
</LinearLayout>

LeftFrag.java //this is our base fragment without a nested fragment
	TextView changeableText;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// set the view
		View root = inflater.inflate(R.layout.left_frag, container, false);
		Button doNestingButton = (Button) root.findViewById(R.id.left_frag_button);
		changeableText = (TextView) root.findViewById(R.id.left_frag_text);
		// make our buttons nest the second Fragment
		doNestingButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Fragment videoFragment = new NestedFrag();
				// we get the 'childFragmentManager' for our transaction
				FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
				// make the back button return to the main screen
				// and supply the tag 'left' to the backstack
				transaction.addToBackStack("left");
				// add our new nested fragment
				transaction.add(getId(), videoFragment, "left");
				// commit the transaction
				transaction.commit();
			}
		});
		return root;
	}
	public void setChangeableText(String newText) {
		if (changeableText != null)
			changeableText.setText(newText);
	}

res/layout/left_frag.xml //simple layout for base fragment
<?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:gravity="center">
	<TextView xmlns:android="http://schemas.android.com/apk/res/android"
			  android:id="@+id/left_frag_text"
			  android:text="Left Fragment { Hello World }"
			  android:layout_width="wrap_content"
			  android:layout_height="wrap_content"
			  android:gravity="center"
			  android:padding="16dp"
			  android:textSize="18sp"/>
	<Button
			android:id="@+id/left_frag_button"
			android:text="Launch Nested Fragment"
			android:gravity="center"
			android:layout_alignParentBottom="true"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:paddingBottom="10dp"/>
</RelativeLayout>

NestedFrag.java //our nested Fragment *** can not be defined in xml as fragment, must be created dynamically ***
	public NestedFrag() {
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.nested_frag, container, false);
		ImageButton doNestingButton = (ImageButton) root.findViewById(R.id.image_button);
		doNestingButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					((LeftFrag) getParentFragment()).setChangeableText("***I saw `cout’ being shifted 'Hello World' times to the left and stopped right there. (Steve Gonedes)***");
				} catch (ClassCastException e) {
					try {
						((RightFrag) getParentFragment()).setChangeableText("***That’s what’s cool about working with computers.  They don’t argue, they remember everything, and they don’t drink all your beer. (Paul Leary)***");
					} catch (ClassCastException e1) {
						// should never get here
					}
				}
				// drop the view
				destroyFragment();
			}
		});
		return root;
	}
	private void destroyFragment() {
		getChildFragmentManager().beginTransaction().hide(this).commit();
	}

VCS::anonymous git checkout: git://github.com/JBirdVegas/NestedFragmentExample.git
Compiled Apk: Attached File  NestedFragmentsExample.apk   78.8KB   1070 downloads

Compressed archives of source code
tarball: https://github.com/J...e/master.tar.gz
zip: https://github.com/J...hive/master.zip

I would be happy to help however I can if you have questions.

Edited by JBirdVegas, 14 November 2012 - 09:36 PM.

  • 1 Likes

Show Your appreciation by clicking Thanks!

I co-maintain the droid2 branch of CyanogenMod, n00bware founder and lead developer and a developer for AOKP
my icon is custom artwork from ThaPHLASH