Commit cad14190 authored by Filip Wiesner's avatar Filip Wiesner

*minor* DynamicViewPager small progress

parent a2063287
......@@ -14,6 +14,7 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.doOnLayout
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
......@@ -170,12 +171,8 @@ class NewsViewHolder(view: View, context: Context) : TaskViewHolder(view, contex
val article = TextView(context).apply {
setupNewsText()
setHtmlText(news[position].title)
setOnClickListener {
newsTask.lastClickedStory = news[position]
clickListener(task)
}
}
//TODO Make array of Article Views and measure them?
container.addView(article)
return article
......@@ -187,7 +184,14 @@ class NewsViewHolder(view: View, context: Context) : TaskViewHolder(view, contex
override fun isViewFromObject(view: View, item: Any) = view == item
}
pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
override fun onPageSelected(position: Int) {
newsTask.lastClickedStory = news[position]
}
})
(itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams)
.isFullSpan = true
view.setOnClickListener { clickListener(task) }
......
......@@ -2,28 +2,80 @@ package com.cvut.blackbird.support.dynamicViewPager
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import androidx.viewpager.widget.ViewPager
import android.view.animation.Animation
import android.view.animation.Transformation
import android.widget.LinearLayout
import com.google.gson.util.VersionUtils
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.PagerAdapter
import com.github.florent37.kotlin.pleaseanimate.core.Expectations
import com.github.florent37.kotlin.pleaseanimate.please
class WrappingViewPager : ViewPager {
private var mAnimStarted = false
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
var heightMeasureSpec: Int
var heightMeasureSpec = heightMeasureSpec
var height = 0
val child = getChildAt(1)
child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
if (!mAnimStarted && null != adapter) {
var height: Int
val child = (adapter as PagerAdapter).instantiateItem(LinearLayout(context), currentItem) as View
child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
height = child.measuredHeight
if (height < minimumHeight) {
height = minimumHeight
}
val h = child.measuredHeight
// Not the best place to put this animation, but it works pretty good.
val newHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
if (layoutParams.height != 0 && heightMeasureSpec != newHeight) {
val targetHeight = height
val currentHeight = layoutParams.height
val heightChange = targetHeight - currentHeight
if (h > height) height = h
val a = object : Animation() {
override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
if (interpolatedTime >= 1) {
layoutParams.height = targetHeight
} else {
val stepHeight = (heightChange * interpolatedTime).toInt()
layoutParams.height = currentHeight + stepHeight
}
requestLayout()
}
heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
override fun willChangeBounds(): Boolean {
return true
}
}
a.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {
mAnimStarted = true
}
override fun onAnimationEnd(animation: Animation) {
mAnimStarted = false
}
override fun onAnimationRepeat(animation: Animation) = Unit
})
a.duration = 100
mAnimStarted = true
startAnimation(a)
} else {
heightMeasureSpec = newHeight
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment