Skip to content

Instantly share code, notes, and snippets.

@nosix
Created July 26, 2016 14:25
Show Gist options
  • Save nosix/7da5cd591b726daff4c593ae6b36765d to your computer and use it in GitHub Desktop.
Save nosix/7da5cd591b726daff4c593ae6b36765d to your computer and use it in GitHub Desktop.
ViewPager for Android (SDK 22) in Kotlin 1.0.2. It can be scrolled in one direction. (Left only, Right only or Both)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.anyspirit.mycustom.view.CustomViewPager
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
class MainActivity : AppCompatActivity() {
private fun updateViewPager() {
findViewById(R.id.main_container).let {
it as ViewPager
(it.adapter as MainPagerAdapter).last = it.currentItem
}
}
}
package xxx.view
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
class MainPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
private val fragments: MutableList<Fragment> = mutableListOf()
var last: Int = 0
set(last) {
field = last
notifyDataSetChanged()
}
fun addFragment(fragment: Fragment) = fragments.add(fragment)
fun indexOf(fragment: Fragment): Int = fragments.indexOf(fragment)
override fun getCount(): Int = if (last < fragments.size) last + 1 else 0
override fun getItem(position: Int): Fragment? = fragments[position]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment