请告诉我,我遇到了这样的问题。 我与CollapsingToolbar配合使用,如果您轻扫,一切都会正常。但是,如果我快速滑动,红色图片就不会落入位置
class MainActivity : AppCompatActivity() {
fun Int.toPx(): Float=(this * android.content.res.Resources.getSystem().displayMetrics.density).toFloat()
var avatarAnimationStartPointY = 0F
var avatarCollapseAnimationChangeWeight = 0F
var verticalToolbarAvatarMargin =0F
var isCalculated = false
var cashCollapseState: Pair<Int, Int>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(toolBar.height - 150.toPx())*2
appBarLayout.addOnOffsetChangedListener(
AppBarLayout.OnOffsetChangedListener{appBarLayout, i->
if (isCalculated.not()){
avatarAnimationStartPointY = abs((appBarLayout.height -(78.toPx() + 120.toPx()))/ appBarLayout.totalScrollRange)
avatarCollapseAnimationChangeWeight = 1/(1-avatarAnimationStartPointY)
verticalToolbarAvatarMargin=(toolBar.height - 150.toPx())*2
isCalculated=true
}
updateViews(abs(i/appBarLayout.totalScrollRange.toFloat()))
})
}
private fun updateViews(offset: Float){
when (offset){
in 0.15F..1F ->{
textViewName.apply {
if(visibility != View.VISIBLE) visibility = View.VISIBLE
alpha=(1- offset) * 0.35F
}
}
in 0F..0.15F ->{
textViewName.alpha=(1f)
imageViewAvatar.alpha=1f
}
}
when (offset){
in 0.15F..1F ->{
button_logout.apply {
if(visibility != View.VISIBLE) visibility = View.VISIBLE
alpha=(1- offset) * 0.35F
}
}
in 0F..0.15F ->{
button_logout.alpha=(1f)
}
}
when (offset){
in 0.15F..1F ->{
imageViewRename.apply {
if(visibility != View.VISIBLE) visibility = View.VISIBLE
alpha=(1- offset) * 0.35F
}
}
in 0F..0.15F ->{
imageViewRename.alpha=(1f)
}
}
when{
offset<SWITCH_BOUND-> Pair(TO_EXPANDED, cashCollapseState?.second?:WAIT_FOR_SWITCH)
else -> Pair(TO_COLLAPSED, cashCollapseState?.second ?: WAIT_FOR_SWITCH)
}.apply{
when{
cashCollapseState !=null && cashCollapseState != this ->{
when(first ){
TO_EXPANDED ->{
textViewNameSingle.visibility = View.INVISIBLE
}
TO_COLLAPSED -> frameLayoutLogin.apply {
textViewNameSingle.apply {
visibility=View.VISIBLE
alpha = 0F
animate().setDuration(500).alpha(1.0f)
}
}
}
cashCollapseState = Pair(first, SWITCHED)
}
else->{
cashCollapseState = Pair(first, WAIT_FOR_SWITCH)
}
}
imageViewAvatar.apply {
when {
offset > avatarAnimationStartPointY ->{
val avatarCollapseAnimateOffset = (offset - avatarAnimationStartPointY) * avatarCollapseAnimationChangeWeight
val avatarSize = 78.toPx()-(78.toPx() - 54.toPx()) * avatarCollapseAnimateOffset
this.layoutParams.also {
it.height= avatarSize.roundToInt()
it.width = avatarSize.roundToInt()
}
tvWorkaround.setTextSize(TypedValue.COMPLEX_UNIT_PX,offset)
this.translationX=((appBarLayout.width - 40.toPx() - avatarSize )/ 2)* avatarCollapseAnimateOffset
this.translationY=((toolBar.height -verticalToolbarAvatarMargin - avatarSize)/2)* avatarCollapseAnimateOffset
}
else -> this.layoutParams.also {
if (it.height != 78.toPx().toInt()){
it.height = 78.toPx().toInt()
it.width = 78.toPx().toInt()
this.layoutParams = it
}
translationX=0F
}
}
}
}
}
companion object {
const val SWITCH_BOUND = 0.8f
const val TO_EXPANDED = 0
const val TO_COLLAPSED = 1
const val WAIT_FOR_SWITCH = 0
const val SWITCHED = 1
}}
普通视图 enter image description here
快速滑动后
< androidx .coordinatorlayout.widget.CoordinatorLayout
xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : app = " http://schemas.android.com/apk/res-auto "
xmlns : tools = " http://schemas.android.com/tools "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : background = " # F5121212 "
android : fitsSystemWindows = " true " >
< com .google.android.material.appbar.AppBarLayout
android : id = " @ + id / appBarLayout "
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : background = " # 121212 " >
< com .google.android.material.appbar.CollapsingToolbarLayout
android : layout_width = " match_parent "
android : layout_height = " 193dp "
app : contentScrim = " ? attr / colorPrimary "
app : layout_scrollFlags = " scroll | exitUntilCollapsed | snap " >
< FrameLayout
android : id = " @ + id / frameLayoutLogin "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : fitsSystemWindows = " true "
/>
< androidx .appcompat.widget.Toolbar
android : id = " @ + id / toolBar "
android : layout_width = " match_parent "
android : layout_height = " ? attr / actionBarSize "
app : layout_collapseMode = " pin " >
< ImageButton
android : id = " @ + id / imageButtonBackTabLayout "
Android : layout_width = " 24dp "
Android : layout_height = " 24dp "
android : background = " @ drawable / ic_back_tab_layout "
android : layout_marginLeft = " 12dp "
android : layout_marginTop = " 17dp "
app : layout_collapseMode = " параллакс "
android: scaleType = " fitXY " />
</ androidx .appcompat.widget.Toolbar>
< ImageView
android : id = " @ + id / imageViewAvatar "
Android : layout_width = " 78dp "
android : layout_height = " 78dp "
Android : layout_gravity = " центр_горизонт "
android : background = " @ color / colorAccent "
android : layout_marginTop = " 42dp " >
</ ImageView >
< ImageButton
android : id = " @ + id / imageViewRename "
Android : layout_width = " 28dp "
android : layout_height = " 28dp "
android : background = " @ drawable / ic_rename "
Android : layout_gravity = " центр_горизонт "
android : layout_marginLeft = " 30dp "
android : layout_marginTop = " 88dp " />
< androidx .appcompat.widget.AppCompatTextView
android : id = " @ + id / tvWorkaround "
android : layout_width = " wrap_content "
android : layout_height = " ? attr / actionBarSize "
Android : layout_gravity = " центр_горизонт "
Android : гравитация = " центр "
app : lineHeight = " 21sp "
android : textSize = " 17sp "
android : textColor = " # F2F2F2 "
Android : видимость = " невидимый "
app : layout_collapseMode = " pin "
инструменты : видимость = " невидимый "
android : ellipsize = " конец "
android : scrollHorizontally = " true " />
< androidx .appcompat.widget.AppCompatTextView
android : id = " @ + id / textViewNameSingle "
android : layout_width = " wrap_content "
android : layout_height = " 22dp "
Android : layout_gravity = " центр_горизонт "
Android : гравитация = " центр "
app : lineHeight = " 21sp "
android : textSize = " 17sp "
android : textColor = " #FFFFFF "
Android : видимость = " невидимый "
app : layout_collapseMode = " pin "
инструменты : видимость = " видимый "
android : text = " name "
android : layout_marginTop = " 29dp "
android : scrollHorizontally = " true " />
< androidx .appcompat.widget.AppCompatTextView
android : id = " @ + id / textViewName "
Android : layout_width = " 211dp "
android : layout_height = " 34dp "
Android : layout_gravity = " центр_горизонт "
Android : гравитация = " центр "
app : lineHeight = " 28sp "
android : textSize = " 24sp "
android : textColor = " # F2F2F2 "
android : layout_margin = " 120dp "
инструменты : видимость = " видимый "
android : text = " name "
android : ellipsize = " marquee " />
< Кнопка
android : id = " @ + id / button_logout "
Android : layout_width = " 66dp "
android : layout_height = " 19dp "
android : layout_below = " @ + id / textViewName "
android : layout_centerHor horizontal = " true "
android : layout_marginTop = " 162dp "
android : background = " # D14C8E "
Android : гравитация = " снизу "
android : textSize = " 18sp "
android : textColor = " # B88A8A8F "
Android : layout_gravity = " центр_горизонт "
инструменты : видимость = " видимый " />
</ com .google.android.material.appbar.CollapsingToolbarLayout>
< com .google.android.material.tabs.TabLayout
android : id = " @ + id / tabLayoutSettings "
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
app : tabGravity = " fill "
Приложение : tabMode = " прокручивать "
app : tabTextColor = " # 979797 "
app : tabSelectedTextColor = " # F8634A "
android : textSize = " 17sp "
android : lineHeight = " 22sp "
android : paddingLeft = " 16dp "
app : tabIndicatorColor = " # F8634A "
app : tabIndicatorFullWidth = " false "
android : background = " # F8634A " />
</ com .google.android.material.appbar.AppBarLayout>
< RelativeLayout
android : layout_width = " match_parent "
android : layout_height = " 744dp "
android : scrollbars = " none "
app : layout_behavior = " @ string / appbar_scrolling_view_behavior " >
< Кнопка
android : id = " @ + id / b_go_demo_2 "
style = " @ style / Widget.AppCompat.Button.Small "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : layout_centerHor horizontal = " true "
android : text = " button " />
</ RelativeLayout >
可能会跳过事件,因为每单位时间存在某种限制。我不知道。请帮助