package com.cy.stickheadrecycledemo.adapter
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cy.stickheadrecycledemo.MainActivity
import com.cy.stickheadrecycledemo.data.StickBean
import com.cy.stickheadrecycledemo.utils.dp
import kotlin.math.min
class OverAllDecoration : RecyclerView.ItemDecoration() {
private var mHeaderHeight:Float = 0f
private var mHeaderView:View? = null
private var mHeadPosition = 0
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val manager = parent.layoutManager
val childPosition = parent.getChildAdapterPosition(view)
if (isHead(childPosition,parent)){
outRect.top = 10.dp
}
}
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
if (mHeaderHeight==0f){
mHeaderHeight = getHeadHeight(parent)
}
val linearLayoutManager = parent.layoutManager as LinearLayoutManager
val firstPosition = linearLayoutManager.findFirstVisibleItemPosition()
val firstCompletePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition()
if (!isHead(firstCompletePosition,parent)){
// if (isHead(firstPosition,parent)){
// mHeaderView = parent.findViewHolderForAdapterPosition(firstPosition)?.itemView
// Log.e(">>>","mHeaderView:${mHeaderView}")
// }
val findViewHolderForLayoutPosition =
parent.findViewHolderForLayoutPosition(firstPosition) ?: return
if (parent.top < findViewHolderForLayoutPosition.itemView.top){
return
}
if (isHead(firstPosition,parent)){
addStickHead(parent, firstPosition)
}
mHeaderView?.measure(View.MeasureSpec.makeMeasureSpec(parent.measuredWidth,View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(parent.measuredWidth,View.MeasureSpec.AT_MOST))
mHeaderView?.layout(0,0,parent.width, mHeaderHeight.toInt())
mHeaderView?.draw(c)
return
}
if (!isHead(firstPosition,parent)&&isHead(firstCompletePosition,parent)){
val view =
parent.findViewHolderForAdapterPosition(firstPosition)?.itemView ?: return
val bottom = min(view.bottom.toFloat(),mHeaderHeight)
c.translate(0f,bottom - mHeaderHeight )
mHeaderView?.draw(c)
// mHeaderView?.translationY = bottom - mHeaderHeight
}
}
private fun addStickHead(
parent: RecyclerView,
firstPosition: Int
) {
val tvHeader = TextView(parent.context)
tvHeader.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
tvHeader.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f)
tvHeader.setPadding(20.dp, 10.dp, 20.dp, 10.dp)
tvHeader.setBackgroundColor(Color.parseColor("#71DFE7"))
BaseViewHolder(tvHeader)
val adapter = parent.adapter as BaseNodeAdapter
if (adapter.data.get(firstPosition) is StickBean) {
val stickBean = adapter.data.get(firstPosition) as StickBean
tvHeader.text = stickBean.head
Log.e(">>>","stickBean.head:${stickBean.head}")
tvHeader.setOnClickListener {
adapter.collapse(mHeadPosition,true)
}
}
mHeaderView = tvHeader
}
private fun getHeadHeight(parent: RecyclerView): Float {
val view =
parent.findViewHolderForAdapterPosition(0)?.itemView ?: return 0f
Log.e(">>","view.height:${view.height}")
return view.height.toFloat()
}
private fun isHead(position: Int, parent: RecyclerView) :Boolean{
if (position <0){
return false
}
val vh = parent.findViewHolderForAdapterPosition(position)
parent.findViewHolderForLayoutPosition(position)
val itemViewType = parent.adapter?.getItemViewType(position)
if (itemViewType == 1){
mHeadPosition = position
}
return itemViewType == 1
}
}
网友评论