Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created December 12, 2012 02:08
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 daichan4649/4264277 to your computer and use it in GitHub Desktop.
Save daichan4649/4264277 to your computer and use it in GitHub Desktop.
タブ表示(for Android)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/container_tabs"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:visibility="gone" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/container_tabs" />
</LinearLayout>
</LinearLayout>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/tab_1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/tab_2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/tab_3"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="atatatatata" />
</LinearLayout>
private void initTabLayout() {
// タブバー表示設定
View container = findViewById([resourceId]);
tabHost = (TabHost) container.findViewById(R.id.tabhost);
tabHost.setup();
for (TabType tabType : TabType.values()) {
tabHost.addTab(createTabSpec(tabHost, tabType));
}
// 初期表示タブ
tabHost.setCurrentTabByTag(TabType.TAB_1.getTag());
// タブ選択時処理(Fragment切替)
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tag) {
showTab(TabType.fromTag(tag));
}
});
}
private TabSpec createTabSpec(TabHost tabHost, TabType tabType) {
TabSpec tabSpec = tabHost.newTabSpec(tabType.getTag());
View tabView = View.inflate([context], R.layout.tabspec, null);
TextView text = (TextView) tabView.findViewById(R.id.text);
text.setText(tabType.getLabel());
tabSpec.setIndicator(tabView);
tabSpec.setContent(tabType.getTabId());
return tabSpec;
}
private void showTab(TabType tabType) {
Fragment fragment = null;
switch (tabType) {
case TAB_1:
fragment = Tab1Fragment.newInstance();
break;
case TAB_2:
fragment = Tab2Fragment.newInstance();
break;
default:
return;
}
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(tabType.getTabId(), fragment);
ft.commit();
}
private enum TabType {
TAB_1("abeshi", "あべし", R.id.tab_1),
TAB_2("hidebu", "ひでぶ", R.id.tab_2);
private String tag;
private String label;
private int tabId;
private TabType(String tag, String label, int tabId) {
this.tag = tag;
this.label = label;
this.tabId = tabId;
}
public String getTag() {
return tag;
}
public String getLabel() {
return label;
}
public int getTabId() {
return tabId;
}
public static TabType fromTag(String tag) {
for (TabType type : TabType.values()) {
if (type.getTag().equals(tag)) {
return type;
}
}
throw new RuntimeException("invalid params: " + tag);
}
}
public class Tab1Fragment extends Fragment {
public static Fragment newInstance() {
return new Tab1Fragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_1, null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment