Skip to content

Instantly share code, notes, and snippets.

@noxi515
Created December 11, 2012 14:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save noxi515/4259065 to your computer and use it in GitHub Desktop.
Save noxi515/4259065 to your computer and use it in GitHub Desktop.
Tab + ChildFragment sample
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
style="?android:attr/tabWidgetStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" />
<FrameLayout
android:id="@+id/tab_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
package jp.noxi.sample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
private static final String TAG_TAB_1 = "TAB_1";
private static final String TAG_TAB_2 = "TAB_2";
private static final String ARGUMENT_NAME = "name";
FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tab_container);
final Bundle args1 = new Bundle();
args1.putString(ARGUMENT_NAME, TAG_TAB_1);
mTabHost.addTab(mTabHost.newTabSpec(TAG_TAB_1)
.setIndicator(TAG_TAB_1), TabRoot.class, args1);
final Bundle args2 = new Bundle();
args2.putString(ARGUMENT_NAME, TAG_TAB_2);
mTabHost.addTab(mTabHost.newTabSpec(TAG_TAB_2)
.setIndicator(TAG_TAB_2), TabRoot.class, args2);
}
/*
* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onBackPressed()
* バックキーをタブ内フラグメントに処理させる。
*/
@Override
public void onBackPressed() {
Fragment f = getSupportFragmentManager()
.findFragmentByTag(mTabHost.getCurrentTabTag());
if (f != null && f instanceof TabRoot) {
TabRoot tabChild = (TabRoot) f;
if (tabChild.onBackPressed()) {
return;
}
}
super.onBackPressed();
}
@Override
protected void onDestroy() {
super.onDestroy();
mTabHost = null;
}
/**
* Tabに入れる親Fragment
*
* @author noxi
*/
public static class TabRoot extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.tab_root, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// 初回のみ自動で子を入れる
if (savedInstanceState == null) {
getChildFragmentManager()
.beginTransaction()
.addToBackStack(null)
.add(R.id.fragment_container, createNewChild())
.commit();
}
}
/*
* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
* 子を追加する処理
*/
@Override
public void onClick(View v) {
getChildFragmentManager()
.beginTransaction()
.addToBackStack(null)
.replace(R.id.fragment_container, createNewChild())
.commit();
}
/**
* バックキーの処理
*
* @return このFragmentが処理を行う場合TRUE
*/
public boolean onBackPressed() {
FragmentManager fm = getChildFragmentManager();
if (fm.getBackStackEntryCount() == 1) {
return false;
} else {
fm.popBackStack();
return true;
}
}
/**
* 子Fragmentを作成する
*/
Fragment createNewChild() {
FragmentManager fm = getChildFragmentManager();
Bundle args = getArguments();
if (args == null) {
args = new Bundle();
args.putString(ARGUMENT_NAME, "Name unknown");
} else {
args = new Bundle(args);
}
args.putInt(TabChild.ARGUMENT_CHILD_COUNT, fm.getBackStackEntryCount() + 1);
Fragment f = new TabChild();
f.setArguments(args);
return f;
}
}
/**
* Tabの子Fragment
*
* @author noxi
*/
public static class TabChild extends Fragment {
private static final String ARGUMENT_CHILD_COUNT = "child_count";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.tab_child, container, false);
Bundle args = getArguments();
if (args != null
&& args.containsKey(ARGUMENT_NAME)
&& args.containsKey(ARGUMENT_CHILD_COUNT)) {
String text = args.getString(ARGUMENT_NAME)
+ "__" + args.getInt(ARGUMENT_CHILD_COUNT);
Button button = (Button) v.findViewById(R.id.button);
button.setText(text);
Fragment f = getParentFragment();
if (f instanceof OnClickListener) {
button.setOnClickListener((OnClickListener) f);
}
}
return v;
}
}
}
<?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" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment