Last active
December 21, 2018 05:57
-
-
Save pankaj89/ad2ae045eec5825f8ff100feac0a5721 to your computer and use it in GitHub Desktop.
Center in Coordinator visible area
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 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) | |
} | |
} |
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
Used in case of viewpager | |
referenceView.addView(<your view inside viewpager fragments>) |
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 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