Skip to content

Commit

Permalink
Header Footer 设置为可点击
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCheen committed Oct 18, 2022
1 parent 007cd77 commit 620a0ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ class HeaderFooterTestcase : BaseTestcaseFragment() {
val headerView = LayoutInflater.from(activity).inflate(R.layout.header_layout, null, false)
headerFooterAdapter.setupHeaderView(headerView)

headerView.setOnClickListener {
Log.d(TAG, "onInit: HeaderView clicked")
}

val footerView = LayoutInflater.from(activity).inflate(R.layout.footer_layout, null, false)
headerFooterAdapter.setupFooterView(footerView)

footerView.setOnClickListener {
Log.d(TAG, "onInit: FooterView clicked")
}

//需要处理偏移量
adapter.doOnItemClick { recyclerView, childView, position ->
Log.d(TAG, "doOnItemClick called with: childView = $childView, position = $position")
Expand All @@ -49,6 +57,8 @@ class HeaderFooterTestcase : BaseTestcaseFragment() {
toast("点击了 position = $position,model=${adapter.getItemData(realPosition)}")
}

switchLayoutManager(1)

recyclerView.adapter = headerFooterAdapter

SwipeDragHelper(headerFooterAdapter)
Expand Down Expand Up @@ -81,14 +91,13 @@ class HeaderFooterTestcase : BaseTestcaseFragment() {
}
.attachToRecyclerView(recyclerView)

// 需要处理 SpanSize
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (headerFooterAdapter.isHeaderOrFooter(position)) gridLayoutManager.spanCount else 1
}
}
// 如果默认不是 GridLayoutManager,则需要处理 SpanSize
// gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
// override fun getSpanSize(position: Int): Int {
// return if (headerFooterAdapter.isHeaderOrFooter(position)) gridLayoutManager.spanCount else 1
// }
// }

switchLayoutManager(0)
}

override fun createRefreshData(size: Int): MutableList<Any> {
Expand Down
25 changes: 21 additions & 4 deletions flap/src/main/java/me/yifeiyuan/flap/ext/HeaderFooterAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ class HeaderFooterAdapter(var adapter: FlapAdapter) : RecyclerView.Adapter<Recyc
super.onAttachedToRecyclerView(recyclerView)
attachingRecyclerView = recyclerView
adapter.onAttachedToRecyclerView(recyclerView)

when (recyclerView.layoutManager) {
is GridLayoutManager -> {
(recyclerView.layoutManager as GridLayoutManager).apply {
val preSpanSizeLookup = spanSizeLookup
spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
if (this@HeaderFooterAdapter.isHeaderOrFooter(position)){
return spanCount
}else {
return preSpanSizeLookup?.getSpanSize(position) ?: 1
}
}
}
}
}
}
}

override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
Expand Down Expand Up @@ -235,11 +252,11 @@ class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view), ComponentCon
}

override fun isClickable(): Boolean {
return false
return true
}

override fun isLongClickable(): Boolean {
return false
return true
}
}

Expand All @@ -254,10 +271,10 @@ class FooterViewHolder(view: View) : RecyclerView.ViewHolder(view), ComponentCon
}

override fun isClickable(): Boolean {
return false
return true
}

override fun isLongClickable(): Boolean {
return false
return true
}
}

0 comments on commit 620a0ae

Please sign in to comment.