Skip to content

Instantly share code, notes, and snippets.

@pankaj89
Last active December 21, 2018 05:57
Show Gist options
  • Save pankaj89/ad2ae045eec5825f8ff100feac0a5721 to your computer and use it in GitHub Desktop.
Save pankaj89/ad2ae045eec5825f8ff100feac0a5721 to your computer and use it in GitHub Desktop.
Center in Coordinator visible area
package com.master.accessibility.ui.mainactivity2
import android.content.Context
import android.support.design.widget.AppBarLayout
import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
class CoordinatorCenterViewBehavior : CoordinatorLayout.Behavior<ImageView> {
constructor() : super()
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
val LOG = "LOG"
override fun layoutDependsOn(parent: CoordinatorLayout, child: ImageView, dependency: View): Boolean {
if (dependency is AppBarLayout) {
return true
}
return super.layoutDependsOn(parent, child, dependency)
}
override fun onDependentViewChanged(parent: CoordinatorLayout, child: ImageView, dependency: View): Boolean {
if (dependency is AppBarLayout) {
val visibleArea = (parent.height - dependency.bottom)
val centerPosition = (visibleArea / 2) - (child.height / 2) + dependency.bottom
child.x = (parent.width / 2 - child.width / 2).toFloat()
child.y = centerPosition.toFloat()
return true
}
return super.onDependentViewChanged(parent, child, dependency)
}
}
Used in case of viewpager
referenceView.addView(<your view inside viewpager fragments>)
package com.fanclips.common.util
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
class ReferenceView : View {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
val referenceViews = ArrayList<View>()
fun addView(view: View) {
var isFound = false
if(referenceViews.isNotEmpty()) {
for (i in 0..referenceViews.size) {
if (referenceViews.get(i).id == view.id) {
isFound = true
break
}
}
}
if (!isFound) {
referenceViews.add(view)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment