Skip to content

Commit

Permalink
titles() function to add titles to pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Hashim committed Apr 25, 2018
1 parent 90128b2 commit 1715a7b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
/.idea
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ viewPager.apply {

fragmentPagerAdapter(pageCount = 3, fragmentManager = supportFragmentManager) {

createItems { position ->
pages { position ->
when (position) {
0 -> InputFragment()
1 -> OutputFragment()
2 -> AboutFragment()
else -> throw Exception("¯\\_(ツ)_/¯")
}
}

titles { position ->
when (position) {
0 -> "Entries"
1 -> "Results"
2 -> "About Us"
else -> "¯\\_(ツ)_/¯"
}
}

onPageSelected { position ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ import android.support.v4.view.ViewPager

class FragmentPagerAdapterDSL(var pageCount: Int = 0, fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager) {

private lateinit var onCreateItem: (position: Int) -> Fragment
private var onCreateItem: (position: Int) -> Fragment = throw RuntimeException("pages() is not called")
private var onCreateTitle: (position: Int) -> CharSequence = { "" }

fun createItems(action: (position: Int) -> Fragment) {
fun pages(action: (position: Int) -> Fragment) {
onCreateItem = action
}

/**default implementation returns empty string */
fun titles(action: (position: Int) -> CharSequence) {
onCreateTitle = action
}

override fun getItem(position: Int) = onCreateItem.invoke(position)
override fun getCount() = pageCount
override fun getPageTitle(position: Int): CharSequence = onCreateTitle.invoke(position)
}

//Extensions
Expand Down

0 comments on commit 1715a7b

Please sign in to comment.