RubotoでActoinBar上にFragmentを使ったTabを作るサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:orientation="vertical" > | |
<TextView android:id="@+id/textView1" | |
android:text="Fragment A" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:orientation="vertical" > | |
<TextView android:id="@+id/textView2" | |
android:text="Fragment B" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" android:layout_gravity="center"> | |
<LinearLayout | |
android:id="@+id/fragment_container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ruboto/widget' | |
require 'ruboto/util/toast' | |
java_import 'android.app.ActionBar' | |
java_import 'android.app.Fragment' | |
class TabActivity | |
def onCreate(bundle) | |
super | |
self.setContentView(Ruboto::R::layout::main) | |
ab = getActionBar() | |
ab.setNavigationMode(ActionBar::NAVIGATION_MODE_TABS) | |
ab.addTab(ab.newTab().setText("tab1").setTabListener(TabListener.new(self, Fragment1.new()))) | |
ab.addTab(ab.newTab().setText("tab2").setTabListener(TabListener.new(self, Fragment2.new()))) | |
end | |
end | |
class TabListener | |
# toastを使うため、activityを渡している | |
def initialize(activity, fragment) | |
@activity = activity | |
@fragment = fragment | |
end | |
def onTabReselected(tab, fragment_transaction) | |
@activity.toast "tab reselected" | |
end | |
def onTabSelected(tab, fragment_transaction) | |
fragment_transaction.replace(Ruboto::R::id::fragment_container, @fragment) | |
end | |
def onTabUnselected(tab, fragment_transaction) | |
fragment_transaction.remove(@fragment) | |
end | |
end | |
class Fragment1 < Fragment | |
def onCreateView(inflater, container, bundle) | |
return inflater.inflate(Ruboto::R::layout::fragment1, container, false) | |
end | |
end | |
class Fragment2 < Fragment | |
def onCreateView(inflater, container, bundle) | |
return inflater.inflate(Ruboto::R::layout::fragment2, container, false) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment