Created
July 26, 2016 14:25
-
-
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)
This file contains 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
<?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> |
This file contains 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
class MainActivity : AppCompatActivity() { | |
private fun updateViewPager() { | |
findViewById(R.id.main_container).let { | |
it as ViewPager | |
(it.adapter as MainPagerAdapter).last = it.currentItem | |
} | |
} | |
} |
This file contains 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 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