Skip to content

Instantly share code, notes, and snippets.

@Alec-DeSouza
Alec-DeSouza / RotatableDialogFragment.kt
Created July 8, 2018 22:36
DialogFragment that retains data on rotation
open class RotatableDialogFragment : DialogFragment() {
init {
retainInstance = true
}
// Fixes rotation bug with dialog fragments
override fun onDestroyView() {
if (retainInstance) {
dialog?.setDismissMessage(null)
}
@Alec-DeSouza
Alec-DeSouza / StoppableRecyclerView.kt
Created July 5, 2018 17:51
RecyclerView compatible with scrolling and ViewPager swiping
class StoppableRecyclerView(context: Context, attrs: AttributeSet?, defStyle: Int)
: RecyclerView(context, attrs, defStyle) {
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context) : this(context, null, 0)
override fun onInterceptTouchEvent(e: MotionEvent?): Boolean {
if(e?.action == MotionEvent.ACTION_DOWN) {
stopNestedScroll()
@Alec-DeSouza
Alec-DeSouza / ViewPagerTabLayoutInterpolation.kt
Last active May 31, 2018 01:21
ViewPager and TabLayout Dynamic Tab Opacity Interpolation [Kotlin]
view_pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
// Interpolate all tab opacity values
for (index in 0 until tab_layout.tabCount) {
tab_layout.getTabAt(index)?.updateOpacity(position, positionOffset)
}
}
fun TabLayout.Tab.updateOpacity(position: Int, offset: Float) {
val diffTab = 1 - abs(position - this.position + offset)