【发布时间】:2019-12-26 11:49:53
【问题描述】:
我为 recyclerview 的选定项目设置动画,并且我希望在选择另一个项目时删除上一个项目的动画
我的代码如下;
MainActivity 代码;
val premiumRecyclerAdapter: RecyclerView = findViewById(R.id.recyclerPremium)
premiumRecyclerAdapter.layoutManager =
object : LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) {
override fun canScrollVertically(): Boolean {
return false
}
}
premiumRecyclerAdapter.itemAnimator = DefaultItemAnimator()
premiumRecyclerAdapter.adapter = ProfilePremiumRecyclerAdapter(getPremium(), premiumRecyclerAdapter)
val snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(premiumRecyclerAdapter)
premiumRecyclerAdapter.smoothScrollToPosition(1)
val layoutManager = premiumRecyclerAdapter.layoutManager
val snapView = snapHelper.findSnapView(layoutManager)
val snapOnScrollListener = SnapOnScrollListener(snapHelper, SnapOnScrollListener.Behavior.NOTIFY_ON_SCROLL_STATE_IDLE)
premiumRecyclerAdapter.addOnScrollListener(snapOnScrollListener)
适配器;
class ProfilePremiumRecyclerAdapter(
val profilePremiumList: MutableList<ProfilePremium>, val premiumRecyclerView: RecyclerView) : RecyclerView.Adapter<ProfilePremiumRecyclerAdapter.ModelViewHolder>() {
class ModelViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val title: TextView = view.findViewById(R.id.txt_premium_title)
val content: TextView = view.findViewById(R.id.txt_premium_content)
val lottieAnim: LottieAnimationView = view.findViewById(R.id.lottie_profile_normal)
var relative_root_profile_popup: ConstraintLayout = view.findViewById(R.id.relative_root_profile_popup)
fun bindItems(item: ProfilePremium) {
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ModelViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.row_profile_normal, parent, false)
return ModelViewHolder(view)
}
override fun getItemCount(): Int {
return profilePremiumList.size
}
override fun onBindViewHolder(holder: ModelViewHolder, position: Int) {
holder.bindItems(profilePremiumList.get(position))
}
}
SnapOnScrollListener;
class SnapOnScrollListener(
private val snapHelper: SnapHelper,
var behavior: Behavior = Behavior.NOTIFY_ON_SCROLL,
var onSnapPositionChangeListener: OnSnapPositionChangeListener? = null
) : RecyclerView.OnScrollListener() {
var position = 1
enum class Behavior {
NOTIFY_ON_SCROLL,
NOTIFY_ON_SCROLL_STATE_IDLE
}
private var snapPosition = RecyclerView.NO_POSITION
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (behavior == Behavior.NOTIFY_ON_SCROLL) {
maybeNotifySnapPositionChange(recyclerView)
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (behavior == Behavior.NOTIFY_ON_SCROLL_STATE_IDLE
&& newState == RecyclerView.SCROLL_STATE_IDLE) {
maybeNotifySnapPositionChange(recyclerView)
}
}
private fun maybeNotifySnapPositionChange(recyclerView: RecyclerView) {
val snapPosition = snapHelper.getSnapPosition(recyclerView)
val snapPositionChanged = this.snapPosition != snapPosition
if (snapPositionChanged) {
onSnapPositionChangeListener?.onSnapPositionChange(snapPosition)
this.snapPosition = snapPosition
MyLog.log("recyclerview position: $snapPosition")
position = snapPosition
recyclerView.postOnAnimation {
val itemView = recyclerView.findViewHolderForLayoutPosition(snapPosition)
Handler().postDelayed({
itemView!!.itemView.scaleAnimPremium()
}, 500)
}
}
}
fun SnapHelper.getSnapPosition(recyclerView: RecyclerView): Int {
val layoutManager = recyclerView.layoutManager ?: return RecyclerView.NO_POSITION
val snapView = findSnapView(layoutManager) ?: return RecyclerView.NO_POSITION
return layoutManager.getPosition(snapView)
}
}
OnSnapPositionChangeListener;
interface OnSnapPositionChangeListener {
fun onSnapPositionChange(position: Int)
}
util/Utils.kt 缩放动画高级功能;
fun View.scaleAnimPremium() {
animate().scaleX(1.1f).start()
animate().scaleY(1.1f).start()
}
我正在向第 1 项添加动画。我希望在移动到第 2 项时删除第 1 项。所以我想删除之前选中项的动画。 我的动画允许项目增长/缩放至所选项目的 1.1f .currently 这些代码将我想要的缩放动画应用于所选项目。但是其他项目的动画并没有恢复到原来的状态。
我试过了,但是没用 :( ;
recyclerView.post {
val itemView = recyclerView.findViewHolderForLayoutPosition(snapPosition)
for (i in 0..3 ) {
if (i != snapPosition) {
val itemViewOther = recyclerView.findViewHolderForLayoutPosition(i)
itemViewOther!!.itemView.scaleAnimPremiumClear()
}
}
Handler().postDelayed({
itemView!!.itemView.scaleAnimPremium()
}, 500)
}
【问题讨论】:
标签: android algorithm kotlin android-recyclerview