Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加NodeAdapter中footer跟随内容收起展开的支持 #3592

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28
buildToolsVersion "30.0.3"
compileSdkVersion 26

defaultConfig {
applicationId "com.chad.baserecyclerviewadapterhelper"
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 26
versionCode 9
versionName "3.0"
}
Expand Down Expand Up @@ -46,9 +45,10 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation project(path: ':library')
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'

implementation 'com.kyleduo.switchbutton:library:2.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation 'com.android.support:recyclerview-v7:26.0.0'
implementation 'com.android.support:appcompat-v7:26.0.0'
implementation 'com.android.support:cardview-v7:26.0.0'
implementation 'com.android.support:design:26.0.0'
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Wed Feb 26 13:11:31 CST 2020
android.enableJetifier=true
#android.enableJetifier=true
org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx2048M"
android.buildCacheDir=./build/buildCache/
android.useAndroidX=true
#android.useAndroidX=true
android.enableD8=true
android.enableBuildCache=true
6 changes: 2 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 14
Expand Down Expand Up @@ -45,7 +44,6 @@ task sourcesJar(type: Jar) {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.annotation:annotation:1.2.0'

compileOnly 'androidx.recyclerview:recyclerview:1.2.1'
compileOnly 'com.android.support:recyclerview-v7:28.0.0'
compileOnly 'com.android.support:appcompat-v7:28.0.0'
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.chad.library.adapter.base

import android.annotation.SuppressLint
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.binder.BaseItemBinder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

Expand Down Expand Up @@ -121,7 +121,7 @@ open class BaseBinderAdapter(list: MutableList<Any>? = null) : BaseQuickAdapter<
//如果没有设置点击监听,则回调给 itemProvider
//Callback to itemProvider if no click listener is set
viewHolder.itemView.setOnClickListener {
var position = viewHolder.bindingAdapterPosition
var position = viewHolder.layoutPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnClickListener
}
Expand All @@ -137,7 +137,7 @@ open class BaseBinderAdapter(list: MutableList<Any>? = null) : BaseQuickAdapter<
//如果没有设置长按监听,则回调给itemProvider
// If you do not set a long press listener, callback to the itemProvider
viewHolder.itemView.setOnLongClickListener {
var position = viewHolder.bindingAdapterPosition
var position = viewHolder.adapterPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnLongClickListener false
}
Expand All @@ -160,7 +160,7 @@ open class BaseBinderAdapter(list: MutableList<Any>? = null) : BaseQuickAdapter<
it.isClickable = true
}
it.setOnClickListener { v ->
var position: Int = viewHolder.bindingAdapterPosition
var position: Int = viewHolder.adapterPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnClickListener
}
Expand All @@ -179,7 +179,7 @@ open class BaseBinderAdapter(list: MutableList<Any>? = null) : BaseQuickAdapter<
it.isLongClickable = true
}
it.setOnLongClickListener { v ->
var position: Int = viewHolder.bindingAdapterPosition
var position: Int = viewHolder.adapterPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnLongClickListener false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.chad.library.adapter.base

import android.support.annotation.LayoutRes
import android.util.SparseIntArray
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder

Expand Down
132 changes: 87 additions & 45 deletions library/src/main/java/com/chad/library/adapter/base/BaseNodeAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.chad.library.adapter.base

import androidx.annotation.IntRange
import androidx.recyclerview.widget.DiffUtil
import android.support.v7.util.DiffUtil
import com.chad.library.adapter.base.entity.node.BaseExpandNode
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.entity.node.ExpandableNodeFooterImpl
import com.chad.library.adapter.base.entity.node.NodeFooterImp
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.provider.BaseNodeProvider
import android.support.annotation.IntRange;

abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
: BaseProviderMultiAdapter<BaseNode>(null) {
abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null) :
BaseProviderMultiAdapter<BaseNode>(null) {

private val fullSpanNodeTypeSet = HashSet<Int>()

private val expandableFooterTypeSet = HashSet<Int>()

init {
if (!nodeList.isNullOrEmpty()) {
val flatData = flatData(nodeList)
Expand Down Expand Up @@ -42,9 +45,14 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* 添加脚部 node provider
* 铺满一行或者一列
* @param provider BaseItemProvider
* @param expandable 是否跟随内容展开收起
*/
fun addFooterNodeProvider(provider: BaseNodeProvider) {
addFullSpanNodeProvider(provider)
fun addFooterNodeProvider(provider: BaseNodeProvider, expandable: Boolean = false) {
if (expandable)
expandableFooterTypeSet.add(provider.itemViewType)
else
fullSpanNodeTypeSet.add(provider.itemViewType)
addItemProvider(provider)
}

/**
Expand All @@ -60,7 +68,9 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
}

override fun isFixedViewType(type: Int): Boolean {
return super.isFixedViewType(type) || fullSpanNodeTypeSet.contains(type)
return if (expandableFooterTypeSet.contains(type))
false
else super.isFixedViewType(type) || fullSpanNodeTypeSet.contains(type)
}

override fun setNewInstance(list: MutableList<BaseNode>?) {
Expand Down Expand Up @@ -370,7 +380,10 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* @param isExpanded Boolean? 如果不需要改变状态,设置为null。true 为展开,false 为收起
* @return MutableList<BaseNode>
*/
private fun flatData(list: Collection<BaseNode>, isExpanded: Boolean? = null): MutableList<BaseNode> {
private fun flatData(
list: Collection<BaseNode>,
isExpanded: Boolean? = null,
): MutableList<BaseNode> {
val newList = ArrayList<BaseNode>()

for (element in list) {
Expand Down Expand Up @@ -398,6 +411,9 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)

if (element is NodeFooterImp) {
element.footerNode?.let {
if (element is ExpandableNodeFooterImpl && element.isExpandable) {
return@let
}
newList.add(it)
}
}
Expand All @@ -415,11 +431,13 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* @param animate Boolean
* @param notify Boolean
*/
private fun collapse(@IntRange(from = 0) position: Int,
isChangeChildCollapse: Boolean = false,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
private fun collapse(
@IntRange(from = 0) position: Int,
isChangeChildCollapse: Boolean = false,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null,
): Int {
val node = this.data[position]

if (node is BaseExpandNode && node.isExpanded) {
Expand All @@ -431,6 +449,11 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
return 0
}
val items = flatData(node.childNode!!, if (isChangeChildCollapse) false else null)
if (node is ExpandableNodeFooterImpl && node.isExpandable) {
node.footerNode?.let {
items.add(it)
}
}
val size = items.size
this.data.removeAll(items)
if (notify) {
Expand All @@ -455,11 +478,13 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* @param animate Boolean
* @param notify Boolean
*/
private fun expand(@IntRange(from = 0) position: Int,
isChangeChildExpand: Boolean = false,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
private fun expand(
@IntRange(from = 0) position: Int,
isChangeChildExpand: Boolean = false,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null,
): Int {
val node = this.data[position]

if (node is BaseExpandNode && !node.isExpanded) {
Expand All @@ -471,6 +496,11 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
return 0
}
val items = flatData(node.childNode!!, if (isChangeChildExpand) true else null)
if (node is ExpandableNodeFooterImpl && node.isExpandable) {
node.footerNode?.let {
items.add(it)
}
}
val size = items.size
this.data.addAll(position + 1, items)
if (notify) {
Expand All @@ -493,10 +523,12 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* @param notify Boolean
*/
@JvmOverloads
fun collapse(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
fun collapse(
@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null,
): Int {
return collapse(position, false, animate, notify, parentPayload)
}

Expand All @@ -507,10 +539,12 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* @param notify Boolean
*/
@JvmOverloads
fun expand(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
fun expand(
@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null,
): Int {
return expand(position, false, animate, notify, parentPayload)
}

Expand All @@ -521,10 +555,12 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* @param notify Boolean
*/
@JvmOverloads
fun expandOrCollapse(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
fun expandOrCollapse(
@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null,
): Int {
val node = this.data[position]
if (node is BaseExpandNode) {
return if (node.isExpanded) {
Expand All @@ -537,18 +573,22 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
}

@JvmOverloads
fun expandAndChild(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
fun expandAndChild(
@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null,
): Int {
return expand(position, true, animate, notify, parentPayload)
}

@JvmOverloads
fun collapseAndChild(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
fun collapseAndChild(
@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null,
): Int {
return collapse(position, true, animate, notify, parentPayload)
}

Expand All @@ -561,13 +601,15 @@ abstract class BaseNodeAdapter(nodeList: MutableList<BaseNode>? = null)
* @param notify Boolean
*/
@JvmOverloads
fun expandAndCollapseOther(@IntRange(from = 0) position: Int,
isExpandedChild: Boolean = false,
isCollapseChild: Boolean = true,
animate: Boolean = true,
notify: Boolean = true,
expandPayload: Any? = null,
collapsePayload: Any? = null) {
fun expandAndCollapseOther(
@IntRange(from = 0) position: Int,
isExpandedChild: Boolean = false,
isCollapseChild: Boolean = true,
animate: Boolean = true,
notify: Boolean = true,
expandPayload: Any? = null,
collapsePayload: Any? = null,
) {

val expandCount = expand(position, isExpandedChild, animate, notify, expandPayload)
if (expandCount == 0) {
Expand Down
Loading