Skip to content

Commit

Permalink
| 更新源数据无法更新理解的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
drake authored and liangjingkanji committed Jun 23, 2020
1 parent 99bfbcd commit 1b759f6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ allprojects {
module 的 build.gradle

```groovy
implementation 'com.github.liangjingkanji:BRV:1.3.2'
implementation 'com.github.liangjingkanji:BRV:1.3.3'
```


Expand Down
85 changes: 61 additions & 24 deletions brv/src/main/java/com/drake/brv/BindingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {

val viewDataBinding = DataBindingUtil.inflate<ViewDataBinding>(LayoutInflater.from(parent.context), viewType, parent, false)
?: return BindingViewHolder(parent.getView(viewType))
val viewDataBinding = DataBindingUtil.inflate<ViewDataBinding>(
LayoutInflater.from(parent.context),
viewType,
parent,
false
)
?: return BindingViewHolder(parent.getView(viewType))

return BindingViewHolder(viewDataBinding)
}
Expand All @@ -118,7 +123,11 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
holder.bind(getModel(position))
}

override fun onBindViewHolder(holder: BindingViewHolder, position: Int, payloads: MutableList<Any>) {
override fun onBindViewHolder(
holder: BindingViewHolder,
position: Int,
payloads: MutableList<Any>
) {
if (payloads.isNotEmpty()) {
onPayload?.invoke(holder, payloads[0])
} else {
Expand All @@ -131,7 +140,7 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
val model = getModel<Any>(position)
val modelClass: Class<*> = model.javaClass
return (typePool[modelClass]?.invoke(model, position)
?: throw NoSuchPropertyException("Please add item model type : ${model.javaClass.simpleName}"))
?: throw NoSuchPropertyException("Please add item model type : ${model.javaClass.simpleName}"))
}

override fun getItemCount() = headerCount + modelCount + footerCount
Expand Down Expand Up @@ -335,7 +344,8 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
}


fun isHeader(@IntRange(from = 0) position: Int): Boolean = (headerCount > 0 && position < headerCount)
fun isHeader(@IntRange(from = 0) position: Int): Boolean =
(headerCount > 0 && position < headerCount)

// </editor-fold>

Expand Down Expand Up @@ -422,7 +432,8 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
}


fun isFooter(@IntRange(from = 0) position: Int): Boolean = (footerCount > 0 && position >= headerCount + modelCount && position < itemCount)
fun isFooter(@IntRange(from = 0) position: Int): Boolean =
(footerCount > 0 && position >= headerCount + modelCount && position < itemCount)

// </editor-fold>

Expand All @@ -438,9 +449,12 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
// 数据模型集合
var models: List<Any?>? = null
set(value) {
field = if (value == null) null else flat(value.toMutableList())
field = when (value) {
is ArrayList -> flat(value)
is List -> flat(value.toMutableList())
else -> null
}
notifyDataSetChanged()

checkedPosition.clear()
if (isFirst) {
lastPosition = -1
Expand All @@ -450,7 +464,11 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
}
}

private fun flat(list: MutableList<Any?>, expand: Boolean? = null, @IntRange(from = -1) depth: Int = 0): MutableList<Any?> {
private fun flat(
list: MutableList<Any?>,
expand: Boolean? = null,
@IntRange(from = -1) depth: Int = 0
): MutableList<Any?> {

if (list.isEmpty()) return list
val newList = ArrayList(list)
Expand All @@ -467,7 +485,8 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
}

val itemSublist = item.itemSublist
val sublist: MutableList<Any?>? = if (itemSublist is ArrayList) itemSublist else itemSublist?.toMutableList()
val sublist: MutableList<Any?>? =
if (itemSublist is ArrayList) itemSublist else itemSublist?.toMutableList()
if (!sublist.isNullOrEmpty() && (item.itemExpand || (depth != 0 && expand != null))) {
val nestedList = flat(sublist, expand, nextDepth)
list.addAll(nestedList)
Expand All @@ -477,7 +496,8 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
return list
}

fun isModel(@IntRange(from = 0) position: Int): Boolean = !(isHeader(position) || isFooter(position))
fun isModel(@IntRange(from = 0) position: Int): Boolean =
!(isHeader(position) || isFooter(position))

/**
* 根据索引返回数据模型, 如果不存在该模型则返回Null
Expand Down Expand Up @@ -509,18 +529,19 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()

if (models.isNullOrEmpty()) return

val data: MutableList<Any?> = when (models) {
is ArrayList -> models
else -> models.toMutableList()
}

if (this.models.isNullOrEmpty()) {
val data: MutableList<Any?> = when (models) {
is ArrayList -> models
else -> models.toMutableList()
}
this.models = flat(data)
notifyDataSetChanged()
} else {
val originModels = this.models as MutableList
originModels.addAll(models)
val realModels = this.models as MutableList
realModels.addAll(flat(data))
if (animation) {
notifyItemRangeInserted(headerCount + modelCount, originModels.size)
notifyItemRangeInserted(headerCount + modelCount, realModels.size)
} else {
notifyDataSetChanged()
}
Expand Down Expand Up @@ -675,7 +696,10 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
*/
fun setChecked(@IntRange(from = 0) position: Int, checked: Boolean) {

if ((checkedPosition.contains(position) && checked) || (!checked && !checkedPosition.contains(position))) return
if ((checkedPosition.contains(position) && checked) || (!checked && !checkedPosition.contains(
position
))
) return

val itemViewType = getItemViewType(position)

Expand Down Expand Up @@ -731,7 +755,11 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
* @param depth 递归展开子项的深度, 如等于-1则代表展开所有子项, 0表示仅展开当前
* @return 展开后增加的条目数量
*/
fun expand(@IntRange(from = 0) position: Int, scrollTop: Boolean = false, @IntRange(from = -1) depth: Int = 0): Int {
fun expand(
@IntRange(from = 0) position: Int,
scrollTop: Boolean = false,
@IntRange(from = -1) depth: Int = 0
): Int {
val holder = rv?.findViewHolderForLayoutPosition(position) as? BindingViewHolder ?: return 0
return holder.expand(scrollTop, depth)
}
Expand All @@ -753,7 +781,11 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
* @param depth 递归展开子项的深度, 如等于-1则代表展开所有子项, 0表示仅展开当前
* @return 展开或折叠后变动的条目数量
*/
fun expandOrCollapse(@IntRange(from = 0) position: Int, scrollTop: Boolean = false, @IntRange(from = -1) depth: Int = 0): Int {
fun expandOrCollapse(
@IntRange(from = 0) position: Int,
scrollTop: Boolean = false,
@IntRange(from = -1) depth: Int = 0
): Int {
val holder = rv?.findViewHolderForLayoutPosition(position) as? BindingViewHolder ?: return 0
return holder.expandOrCollapse(scrollTop, depth)
}
Expand Down Expand Up @@ -857,9 +889,13 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
notifyItemChanged(layoutPosition)
0
} else {
val sublist = if (itemSublist is ArrayList) itemSublist else itemSublist.toMutableList()
val sublist =
if (itemSublist is ArrayList) itemSublist else itemSublist.toMutableList()
val sublistFlat = flat(sublist, true, depth)
(this@BindingAdapter.models as MutableList).addAll(layoutPosition + 1, sublistFlat)
(this@BindingAdapter.models as MutableList).addAll(
layoutPosition + 1,
sublistFlat
)
if (expandAnimationEnabled) {
notifyItemChanged(layoutPosition)
notifyItemRangeInserted(layoutPosition + 1, sublistFlat.size)
Expand Down Expand Up @@ -894,7 +930,8 @@ class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolder>()
notifyItemChanged(layoutPosition, itemExpand)
0
} else {
val sublist = if (itemSublist is ArrayList) itemSublist else itemSublist.toMutableList()
val sublist =
if (itemSublist is ArrayList) itemSublist else itemSublist.toMutableList()
val sublistFlat = flat(sublist, false, depth)
(this@BindingAdapter.models as MutableList).removeAll(sublistFlat)
if (expandAnimationEnabled) {
Expand Down
2 changes: 1 addition & 1 deletion brv/src/main/java/com/drake/brv/PageRefreshLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ open class PageRefreshLayout : SmartRefreshLayout, OnRefreshLoadMoreListener {
val adapter = rv.adapter as? BindingAdapter
?: throw UnsupportedOperationException("RecyclerView require use BindingAdapter")

val isRefreshState = getState() == RefreshState.Refreshing
val isRefreshState = state == RefreshState.Refreshing

if (isRefreshState) {
adapter.models = data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import kotlinx.android.synthetic.main.fragment_preload.*
*/
class PreloadFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_preload, container, false)
}

Expand Down

0 comments on commit 1b759f6

Please sign in to comment.