Skip to content

Instantly share code, notes, and snippets.

@thinkAmi
Created August 5, 2013 21:00
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 thinkAmi/6159571 to your computer and use it in GitHub Desktop.
Save thinkAmi/6159571 to your computer and use it in GitHub Desktop.
RubotoでActoinBar上にFragmentを使ったTabを作るサンプル
<?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>
<?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>
<?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>
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