Add ActionBarTabs demo

Change-Id: I21087d63f78a4d35b249e7ffd92bf6823cf29b60
This commit is contained in:
Adam Powell
2010-11-03 16:57:14 -07:00
parent 04281538b3
commit 2b658f0d9f
5 changed files with 211 additions and 0 deletions

View File

@@ -706,6 +706,14 @@
</intent-filter>
</activity>
<activity android:name=".app.ActionBarTabs"
android:label="@string/action_bar_tabs">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<!-- ************************************* -->
<!-- PREFERENCE PACKAGE SAMPLES -->
<!-- ************************************* -->

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

View File

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical">
<Button android:id="@+id/btn_add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_add_tab"
android:onClick="onAddTab" />
<Button android:id="@+id/btn_remove_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_remove_tab"
android:onClick="onRemoveTab" />
<Button android:id="@+id/btn_toggle_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_toggle_tabs"
android:onClick="onToggleTabs" />
<Button android:id="@+id/btn_remove_all_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_remove_all_tabs"
android:onClick="onRemoveAllTabs" />
</LinearLayout>
</LinearLayout>

View File

@@ -567,6 +567,7 @@
<string name="action_bar_mechanics">App/Action Bar/Action Bar Mechanics</string>
<string name="action_bar_usage">App/Action Bar/Action Bar Usage</string>
<string name="action_bar_tabs">App/Action Bar/Action Bar Tabs</string>
<string name="action_bar_search">Search</string>
<string name="action_bar_add">Add</string>
@@ -587,6 +588,11 @@
<string name="display_options_custom_button">Custom View!</string>
<string name="display_options_menu_item">Menu Item</string>
<string name="btn_add_tab">Add new tab</string>
<string name="btn_remove_tab">Remove last tab</string>
<string name="btn_toggle_tabs">Toggle tab mode</string>
<string name="btn_remove_all_tabs">Remove all tabs</string>
<!-- ============================ -->
<!-- graphics examples strings -->
<!-- ============================ -->

View File

@@ -0,0 +1,129 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.app;
import com.example.android.apis.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
/**
* This demonstrates the use of action bar tabs and how they interact
* with other action bar features.
*/
public class ActionBarTabs extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_bar_tabs);
}
public void onAddTab(View v) {
final ActionBar bar = getActionBar();
final int tabCount = bar.getTabCount();
final String text = "Tab " + tabCount;
bar.addTab(bar.newTab()
.setText(text)
.setTabListener(new TabListener(new TabContentFragment(text))));
}
public void onRemoveTab(View v) {
final ActionBar bar = getActionBar();
bar.removeTabAt(bar.getTabCount() - 1);
}
public void onToggleTabs(View v) {
final ActionBar bar = getActionBar();
if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
} else {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
}
}
public void onRemoveAllTabs(View v) {
getActionBar().removeAllTabs();
}
/**
* A TabListener receives event callbacks from the action bar as tabs
* are deselected, selected, and reselected. A FragmentTransaction
* is provided to each of these callbacks; if any operations are added
* to it, it will be committed at the end of the full tab switch operation.
* This lets tab switches be atomic without the app needing to track
* the interactions between different tabs.
*/
private class TabListener implements ActionBar.TabListener {
private TabContentFragment mFragment;
public TabListener(TabContentFragment fragment) {
mFragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, mFragment.getText());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
private class TabContentFragment extends Fragment {
private String mText;
public TabContentFragment(String text) {
mText = text;
}
public String getText() {
return mText;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);
TextView text = (TextView) fragView.findViewById(R.id.text);
text.setText(mText);
return fragView;
}
}
}