Created
September 13, 2012 10:13
-
-
Save tonyseek/3713371 to your computer and use it in GitHub Desktop.
Writing android demo application with Scala
This file contains hidden or 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
package com.tonyseek.droid | |
import android.app.ActionBar | |
import android.app.FragmentTransaction | |
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.FragmentPagerAdapter | |
import android.support.v4.view.ViewPager | |
import android.view.Gravity | |
import android.view.LayoutInflater | |
import android.view.Menu | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
class MainActivity extends FragmentActivity with ActionBar.TabListener { | |
/** | |
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the | |
* sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will | |
* keep every loaded fragment in memory. If this becomes too memory intensive, it may be best | |
* to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. | |
*/ | |
var mSectionsPagerAdapter: SectionsPagerAdapter = null | |
/** | |
* The {@link ViewPager} that will host the section contents. | |
*/ | |
var mViewPager: ViewPager = null | |
override def onCreate(savedInstanceState: Bundle) = { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Create the adapter that will return a fragment for each of the three primary sections | |
// of the app. | |
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()) | |
// Set up the action bar. | |
val actionBar = getActionBar() | |
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) | |
// Set up the ViewPager with the sections adapter. | |
mViewPager = findViewById(R.id.pager).asInstanceOf[ViewPager] | |
mViewPager.setAdapter(mSectionsPagerAdapter) | |
// When swiping between different sections, select the corresponding tab. | |
// We can also use ActionBar.Tab#select() to do this if we have a reference to the | |
// Tab. | |
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { | |
override def onPageSelected(position: Int) = { | |
actionBar.setSelectedNavigationItem(position) | |
} | |
}) | |
// For each of the sections in the app, add a tab to the action bar. | |
for (i <- 0 to mSectionsPagerAdapter.getCount()) { | |
// Create a tab with text corresponding to the page title defined by the adapter. | |
// Also specify this Activity object, which implements the TabListener interface, as the | |
// listener for when this tab is selected. | |
actionBar.addTab( | |
actionBar.newTab() | |
.setText(mSectionsPagerAdapter.getPageTitle(i)) | |
.setTabListener(this)) | |
} | |
} | |
override def onCreateOptionsMenu(menu: Menu): Boolean = { | |
getMenuInflater().inflate(R.menu.activity_main, menu) | |
return true | |
} | |
def onTabUnselected(tab: ActionBar.Tab, fragmentTransaction: FragmentTransaction) = { | |
} | |
def onTabSelected(tab: ActionBar.Tab, fragmentTransaction: FragmentTransaction) = { | |
// When the given tab is selected, switch to the corresponding page in the ViewPager. | |
mViewPager.setCurrentItem(tab.getPosition()) | |
} | |
def onTabReselected(tab: ActionBar.Tab, fragmentTransaction: FragmentTransaction) = { | |
} | |
/** | |
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary | |
* sections of the app. | |
*/ | |
class SectionsPagerAdapter(val fm: FragmentManager) extends FragmentPagerAdapter(fm) { | |
override def getItem(i: Int): SectionsPagerAdapter.DummySectionFragment = { | |
val fragment = new SectionsPagerAdapter.DummySectionFragment() | |
val args = new Bundle() | |
args.putInt(SectionsPagerAdapter.DummySectionFragment.ARG_SECTION_NUMBER, i + 1) | |
fragment.setArguments(args) | |
return fragment | |
} | |
override def getCount() = 3 | |
override def getPageTitle(position: Int): CharSequence = { | |
position match { | |
case 0 => return getString(R.string.title_section1).toUpperCase() | |
case 1 => return getString(R.string.title_section2).toUpperCase() | |
case 2 => return getString(R.string.title_section3).toUpperCase() | |
case _ => return null | |
} | |
} | |
} | |
object SectionsPagerAdapter { | |
/** | |
* A dummy fragment representing a section of the app, but that simply displays dummy text. | |
*/ | |
class DummySectionFragment extends Fragment { | |
def DummySectionFragment() = { | |
} | |
override def onCreateView(inflater: LayoutInflater, container: ViewGroup, | |
savedInstanceState: Bundle): View = { | |
val textView = new TextView(getActivity()) | |
textView.setGravity(Gravity.CENTER) | |
val args = getArguments() | |
textView.setText(Integer.toString(args.getInt(SectionsPagerAdapter.DummySectionFragment.ARG_SECTION_NUMBER))) | |
return textView | |
} | |
} | |
object DummySectionFragment { | |
val ARG_SECTION_NUMBER = "section_number" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is pretty cool. One issue I haven't figured out, when I run this it appears to create 4 tabs, not just the 3 getCount says there are. I can't swipe to the 4th tab, but I can click on it. It doesn't actually change the displayed Fragment, but you can see the tab is selected.
Diving into this now, but I am curious as to whether or not anyone else has seen this when running this code.
edit: oh, looks like a fencepost error:
for (i <- 0 to mSectionsPagerAdapter.getCount()) { ... }
should be
for (i <- 0 to mSectionsPagerAdapter.getCount() - 1) { ... }
Also the DummySectionFragment constructor is not visible outside of this class, so it's probably better to move it outside of the main class (causes crashes on reloading/rotating). I'm no Scala expert though so there might be a better solution there.