Last active
February 7, 2016 22:12
-
-
Save stanch/0f7998d3b27dd81f20e3 to your computer and use it in GitHub Desktop.
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
... | |
libraryDependencies ++= Seq( | |
aar("org.macroid" %% "macroid" % "2.0.0-M3"), | |
apklib("com.viewpagerindicator" % "library" % "2.4.1"), | |
... | |
) | |
// viewpagerindicator includes a spurious copy of support library | |
dependencyClasspath in Compile ~= { _ filterNot (_.data.getName startsWith "android-support-v4") } | |
... |
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
// some other android imports here | |
... | |
import android.support.v4.app.{ Fragment, FragmentManager, FragmentPagerAdapter } | |
import android.support.v4.view.ViewPager | |
import com.viewpagerindicator.TitlePageIndicator | |
import macroid._ | |
import macroid.FullDsl._ | |
import macroid.contrib._ | |
import macroid.contrib.Layouts._ | |
/** An adapter that takes pairs (page title, fragment factory) */ | |
case class PagingAdapter(fm: FragmentManager, frags: Seq[(CharSequence, Ui[Fragment])]) extends FragmentPagerAdapter(fm) { | |
def getCount = frags.length | |
def getItem(position: Int) = frags(position)._2.get | |
override def getPageTitle(position: Int): CharSequence = frags(position)._1 | |
} | |
/** A trait that provides getTabs method to create a paging layout */ | |
trait FragmentPaging { self: IdGeneration ⇒ | |
def getTabs(frags: (CharSequence, Ui[Fragment])*)(implicit ctx: ActivityContext, manager: FragmentManagerContext[Fragment, FragmentManager]) = { | |
// create an adapter | |
val adapter = PagingAdapter(manager.get, frags) | |
// create a pager | |
val pager = w[ViewPager] <~ | |
id(Id.pager) <~ // it is important to assign an id! | |
PagerTweaks.adapter(adapter) | |
pager.flatMap { p ⇒ | |
// create an indicator and attach to the pager | |
val indicator = w[TitlePageIndicator] <~ | |
LpTweaks.matchWidth <~ | |
BgTweaks.color(Color.BLACK) <~ | |
Tweak[TitlePageIndicator] { x ⇒ | |
x.setViewPager(p) | |
// other initialization | |
} | |
// create a layout with both incicator and pager | |
l[VerticalLinearLayout](indicator, Ui(p)) | |
} | |
} | |
} |
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
// some other android imports here | |
... | |
import android.support.v4.app.FragmentActivity | |
import macroid._ | |
import macroid.FullDsl._ | |
class MyActivity extends FragmentActivity with Contexts[FragmentActivity] with IdGeneration with FragmentPaging { | |
... | |
override def onCreate(savedInstanceState: Bundle) = { | |
super.onCreate(savedInstanceState) | |
setContentView(getUi { | |
l[VerticalLinearLayout]( | |
..., | |
getTabs( | |
// list pairs (page title, factory) | |
"Fragment 1" → f[Fragment1].factory, | |
"Fragment 2" → f[Fragment2].factory, | |
... | |
), | |
... | |
) | |
}) | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, this is great work. Consider using an Upper Bound on the
getTabs
method like this:def getTabs[F <: Fragment](frags: (CharSequence, Ui[F])*)(implicit ctx: ActivityContext, manager: FragmentManagerContext[F, FragmentManager]) = {
This will allow user to use their own Fragments instead of a blank vanilla fragment