Skip to content

Commit

Permalink
日常修改:2024-04-18.
Browse files Browse the repository at this point in the history
  • Loading branch information
walgr committed Apr 18, 2024
1 parent 283f63b commit c149380
Show file tree
Hide file tree
Showing 35 changed files with 258 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@ import androidx.annotation.DrawableRes
import androidx.appcompat.graphics.drawable.StateListDrawableCompat
import com.wpf.app.base.NO_SET
import com.wpf.app.base.NO_SET_F
import com.wpf.app.quick.ability.scope.ViewScope
import com.wpf.app.base.ability.scope.ViewScope
import com.wpf.app.quickutil.helper.toColor
import com.wpf.app.quickutil.other.forceTo

interface BackgroundScope: ViewScope

fun <T : View> T.background(
drawable: Drawable? = null,
builder: (ViewScope.() -> Unit)? = null,
builder: (BackgroundScope.() -> Unit)? = null,
): T {
drawable?.let {
background = it
}
builder?.invoke(object : ViewScope {
builder?.invoke(object : BackgroundScope {
override val view: View = this@background
})
return this
}

fun ViewScope.backgroundRes(
fun BackgroundScope.backgroundRes(
@DrawableRes drawableRes: Int,
): View {
view.setBackgroundResource(drawableRes)
return view
}

fun ViewScope.background(
fun BackgroundScope.background(
shape: Int = GradientDrawable.RECTANGLE,
@ColorInt color: Int = android.R.color.white.toColor(),
colorStateList: ColorStateList? = null,
Expand Down Expand Up @@ -87,7 +89,7 @@ fun ViewScope.background(
return view
}

fun ViewScope.rect(
fun BackgroundScope.rect(
@ColorInt color: Int = android.R.color.white.toColor(),
colorStateList: ColorStateList? = null,
orientation: Orientation = Orientation.LEFT_RIGHT,
Expand Down Expand Up @@ -120,7 +122,7 @@ fun ViewScope.rect(
)
}

fun ViewScope.oval(
fun BackgroundScope.oval(
@ColorInt color: Int = android.R.color.white.toColor(),
colorStateList: ColorStateList? = null,
orientation: Orientation = Orientation.LEFT_RIGHT,
Expand Down Expand Up @@ -153,7 +155,7 @@ fun ViewScope.oval(
)
}

fun ViewScope.state(
fun BackgroundScope.state(
defaultDrawable: Drawable,
selectedDrawable: Drawable? = null,
checkedDrawable: Drawable? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import androidx.annotation.ColorInt
import com.wpf.app.quickutil.helper.dpF
import com.wpf.app.quickutil.helper.dp
import com.wpf.app.quickutil.widget.smartLayoutParams
import io.github.florent37.shapeofview.shapes.ArcView
import io.github.florent37.shapeofview.shapes.ArcView.ArcPosition
Expand Down Expand Up @@ -126,9 +126,9 @@ fun ViewGroup.triangle(

fun ViewGroup.bubble(
layoutParams: LayoutParams = smartLayoutParams(),
arrowWidth: Float = 10.dpF(),
arrowHeight: Float = 10.dpF(),
borderRadius: Float = 10.dpF(),
arrowWidth: Float = 10f.dp,
arrowHeight: Float = 10f.dp,
borderRadius: Float = 10f.dp,
positionPer: Float = 0.5f,
position: Int = BubbleView.POSITION_BOTTOM,
builder: (BubbleView.() -> Unit)? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.view.View
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import com.wpf.app.quick.ability.scope.TextViewScope
import com.wpf.app.base.ability.scope.TextViewScope

fun <T : TextView> T.textColor(
@ColorRes colorRes: Int? = null,
Expand Down
4 changes: 3 additions & 1 deletion QuickBase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ dependencies {
} else {
api project(":QuickUtil")
}
}
}

apply from: rootProject.file('gradle-mvn-push-aar.gradle')
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wpf.app.quick.ability.scope
package com.wpf.app.base.ability.scope

import android.view.ViewGroup

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wpf.app.quick.ability.scope
package com.wpf.app.base.ability.scope

import android.view.View

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Checkable
import android.widget.TextView
import androidx.core.view.children
import com.wpf.app.quickbind.annotations.BindD2VHelper
import com.wpf.app.quickutil.run.RunOnViewWithSelf
import com.wpf.app.quickutil.helper.allChild
import com.wpf.app.quickutil.other.forceTo
import com.wpf.app.quickutil.run.RunOnViewWithSelf

object DataAutoSet2ViewUtils {

Expand All @@ -20,7 +20,7 @@ object DataAutoSet2ViewUtils {

private fun setData2ViewGroup(view: View, data: Any) {
if (view is ViewGroup) {
view.allChild().forEach {
view.children.forEach {
if (it is ViewGroup) {
setData2ViewGroup(it, data)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.wpf.app.quickrecyclerview.data.QuickAbilityData
import com.wpf.app.quickrecyclerview.data.QuickBindData
import com.wpf.app.quickrecyclerview.data.QuickFooterData
import com.wpf.app.quickrecyclerview.data.QuickHeaderData
import com.wpf.app.quickrecyclerview.data.QuickItemData
import com.wpf.app.quickrecyclerview.data.QuickViewData
import com.wpf.app.quickrecyclerview.holder.QuickViewBindingHolder
Expand All @@ -18,21 +20,27 @@ import com.wpf.app.quickutil.other.asTo
*
*/
open class QuickAdapter : RecyclerView.Adapter<QuickViewHolder<QuickItemData>>(), DataAdapter {
private var mRecyclerView: RecyclerView? = null
private var mQuickAdapterListener: QuickAdapterListener<QuickItemData>? = null

var mDataList: MutableList<QuickItemData>? = null

private var mQuickAdapterListener: QuickAdapterListener<QuickItemData>? = null
private var mRecyclerView: RecyclerView? = null
val headerViews = mutableListOf<QuickHeaderData>()
val footerViews = mutableListOf<QuickFooterData>()

internal val extraParameter: LinkedHashMap<String, Any> = linkedMapOf()

fun getDataByViewType(viewType: Int): QuickItemData? {
return (mDataList?.find {
it.viewType == viewType
} ?: headerViews.find { it.viewType == viewType }
?: footerViews.find { it.viewType == viewType })
}

override fun onCreateViewHolder(
viewGroup: ViewGroup,
viewType: Int,
): QuickViewHolder<QuickItemData> {
mDataList?.find {
it.viewType == viewType
}?.let { findData ->
getDataByViewType(viewType)?.let { findData ->
var holder: QuickViewHolder<QuickItemData>? = null
if (findData is QuickBindData) {
findData.setAdapter(this)
Expand Down Expand Up @@ -71,21 +79,35 @@ open class QuickAdapter : RecyclerView.Adapter<QuickViewHolder<QuickItemData>>()

override fun onBindViewHolder(viewHolder: QuickViewHolder<QuickItemData>, position: Int) {
viewHolder.itemPosition = position
viewHolder.onBindViewHolder(this, mDataList!![position], position)
val realData: QuickItemData? =
if (headerViews.isNotEmpty() && position < headerViews.size) {
headerViews[position]
} else if (position < headerViews.size + (mDataList?.size ?: 0)) {
mDataList?.get(position - headerViews.size)
} else {
footerViews[position - (headerViews.size + (mDataList?.size ?: 0))]
}
viewHolder.onBindViewHolder(this, realData, position)
}

override fun getItemViewType(position: Int): Int {
mDataList?.get(position)?.let {
if (it is QuickAbilityData) {
it.viewType = it.initViewType(position)
if (headerViews.isNotEmpty() && position < headerViews.size) {
return headerViews[position].viewType
} else if (position < headerViews.size + (mDataList?.size ?: 0)) {
mDataList?.get(position - headerViews.size)?.let {
if (it is QuickAbilityData) {
it.viewType = it.initViewType(position)
}
return it.viewType
}
return it.viewType
} else if (position < headerViews.size + (mDataList?.size ?: 0) + footerViews.size) {
return footerViews[position - (headerViews.size + (mDataList?.size ?: 0))].viewType
}
return mDataList?.get(position)?.viewType ?: super.getItemViewType(position)
return super.getItemViewType(position)
}

override fun getItemCount(): Int {
return mDataList?.size ?: 0
return headerViews.size + (mDataList?.size ?: 0) + footerViews.size
}

fun getQuickAdapterListener(): QuickAdapterListener<QuickItemData>? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package com.wpf.app.quickrecyclerview

import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wpf.app.quickrecyclerview.data.QuickFooterData
import com.wpf.app.quickrecyclerview.data.QuickHeaderData
import com.wpf.app.quickrecyclerview.helper.toFooterViewData
import com.wpf.app.quickrecyclerview.helper.toHeaderViewData
import com.wpf.app.quickrecyclerview.helper.toViewData
import com.wpf.app.quickrecyclerview.listeners.DataAdapter
import com.wpf.app.quickrecyclerview.utils.SpaceItemDecoration
import com.wpf.app.quickrecyclerview.utils.SpaceType
import com.wpf.app.quickrecyclerview.widget.QuickFooterShadow
import com.wpf.app.quickrecyclerview.widget.QuickHeaderShadow
import com.wpf.app.quickutil.helper.allChild
import com.wpf.app.quickutil.helper.attribute.AutoGetAttributeHelper
import com.wpf.app.quickutil.helper.children
import com.wpf.app.quickutil.other.asTo

/**
* Created by 王朋飞 on 2022/7/13.
Expand All @@ -37,6 +43,31 @@ open class QuickRecyclerView @JvmOverloads constructor(
if (layoutManager == null) {
layoutManager = LinearLayoutManager(context)
}

if (layoutManager is GridLayoutManager) {
layoutManager?.asTo<GridLayoutManager>()?.apply {
spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
val itemData = getData(position)
var isMatch = true
var isHeaderOrFooter = false
if (itemData is QuickHeaderData) {
isHeaderOrFooter = true
isMatch = itemData.isMatch
}
if (itemData is QuickFooterData) {
isHeaderOrFooter = true
isMatch = itemData.isMatch
}
return if (isHeaderOrFooter && isMatch) {
spanCount
} else {
1
}
}
}
}
}
mQuickAdapter.setRecyclerView(this)
adapter = mQuickAdapter
}
Expand Down Expand Up @@ -67,19 +98,20 @@ open class QuickRecyclerView @JvmOverloads constructor(

override fun onFinishInflate() {
super.onFinishInflate()
val allChild = allChild()
val allChild = children()
allChild.forEach {
when (it) {
is QuickHeaderShadow -> {
it.shadowView?.let { realView ->
getQuickAdapter().addData(realView.toViewData(isSuspension = it.isSuspension))
}
getQuickAdapter().headerViews.add(
it.toHeaderViewData(
isSuspension = it.isSuspension,
isMatch = it.isMatch
)
)
}

is QuickFooterShadow -> {
it.shadowView?.let { realView ->
getQuickAdapter().addData(realView.toViewData())
}
getQuickAdapter().footerViews.add(it.toFooterViewData(isMatch = it.isMatch))
}

else -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.wpf.app.quickrecyclerview.ability.helper

import android.view.View
import com.wpf.app.quickrecyclerview.QuickRecyclerView
import com.wpf.app.quickrecyclerview.widget.QuickFooterShadow
import com.wpf.app.quickrecyclerview.widget.QuickHeaderShadow

//interface QuickRecyclerViewHeaderScope: ViewScope

fun QuickRecyclerView.header(
isSuspension: Boolean = false,
isMatch: Boolean = true,
builder: QuickHeaderShadow.() -> View
): QuickHeaderShadow {
val headerView = QuickHeaderShadow(context, isSuspension = isSuspension, isMatch = isMatch)
headerView.addView(builder.invoke(headerView))
return headerView
}

fun QuickRecyclerView.footer(
isMatch: Boolean = true,
builder: QuickFooterShadow.() -> View
): QuickFooterShadow {
val footerView = QuickFooterShadow(context, isMatch = isMatch)
footerView.addView(builder.invoke(footerView))
return footerView
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wpf.app.quickrecyclerview.data

open class QuickFooterData(
val isMatch: Boolean = true, //是否占满
) : QuickViewData()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.wpf.app.quickrecyclerview.data

open class QuickHeaderData(
isSuspension: Boolean = false, //View是否悬浮置顶
val isMatch: Boolean = true, //是否占满
) : QuickViewData(isSuspension = isSuspension)
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package com.wpf.app.quickrecyclerview.helper

import android.view.View
import com.wpf.app.quickrecyclerview.data.QuickFooterData
import com.wpf.app.quickrecyclerview.data.QuickHeaderData
import com.wpf.app.quickrecyclerview.data.QuickViewData

fun View.toHeaderViewData(isSuspension: Boolean = false, isMatch: Boolean = true): QuickHeaderData {
return QuickHeaderData(isSuspension = isSuspension, isMatch = isMatch).apply {
layoutView = this@toHeaderViewData
viewType = this.hashCode()
}
}

fun View.toFooterViewData(isMatch: Boolean = true): QuickFooterData {
return QuickFooterData(isMatch = isMatch).apply {
layoutView = this@toFooterViewData
viewType = this.hashCode()
}
}

fun View.toViewData(isSuspension: Boolean = false): QuickViewData {
return QuickViewData(layoutView = this, isSuspension = isSuspension).apply {
viewType = this.hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ interface DataAdapter {
return getQuickAdapter().mDataList
}

fun getData(pos: Int): QuickItemData? {
return getQuickAdapter().mDataList?.getOrNull(pos)
}

fun setData(newData: MutableList<QuickItemData>) {
getQuickAdapter().mDataList = newData
}
Expand Down
Loading

0 comments on commit c149380

Please sign in to comment.