提交 53644d00 编写于 作者: limuyang2's avatar limuyang2

fix bug

上级 453767de
......@@ -45,7 +45,9 @@ public class NodeTreeUseActivity extends BaseActivity {
nodes.add(seNode);
nodes.add(seNode2);
//第一个夫node,位置为子node的3号位置
adapter.nodeAddData(adapter.getData().get(0), 2, nodes);
// adapter.nodeAddData(adapter.getData().get(0), 2, nodes);
// adapter.nodeSetData(adapter.getData().get(0), 2, seNode2);
adapter.nodeReplaceChildData(adapter.getData().get(0), nodes);
Tips.show("新插入了两个node", Toast.LENGTH_LONG);
}
}, 2000);
......
......@@ -34,4 +34,6 @@ public class NodeTreeAdapter extends BaseNodeAdapter {
}
return -1;
}
public static final int EXPAND_COLLAPSE_PAYLOAD = 110;
}
package com.chad.baserecyclerviewadapterhelper.adapter.node.tree.provider;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import androidx.core.view.ViewCompat;
import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.adapter.node.tree.NodeTreeAdapter;
import com.chad.baserecyclerviewadapterhelper.entity.node.tree.FirstNode;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
......@@ -11,6 +16,8 @@ import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class FirstProvider extends BaseNodeProvider {
@Override
......@@ -27,16 +34,50 @@ public class FirstProvider extends BaseNodeProvider {
public void convert(@NotNull BaseViewHolder helper, @Nullable BaseNode data) {
FirstNode entity = (FirstNode) data;
helper.setText(R.id.title, entity.getTitle());
helper.setImageResource(R.id.iv, R.mipmap.arrow_r);
setArrowSpin(helper, data, false);
}
@Override
public void convert(@NotNull BaseViewHolder helper, @Nullable BaseNode data, @NotNull List<?> payloads) {
for (Object payload : payloads) {
if (payload instanceof Integer && (int) payload == NodeTreeAdapter.EXPAND_COLLAPSE_PAYLOAD) {
// 增量刷新,使用动画变化箭头
setArrowSpin(helper, data, true);
}
}
}
private void setArrowSpin(BaseViewHolder helper, BaseNode data, boolean isAnimate) {
FirstNode entity = (FirstNode) data;
ImageView imageView = helper.getView(R.id.iv);
if (entity.isExpanded()) {
helper.setImageResource(R.id.iv, R.mipmap.arrow_b);
if (isAnimate) {
ViewCompat.animate(imageView).setDuration(200)
.setInterpolator(new DecelerateInterpolator())
.rotation(0f)
.start();
} else {
imageView.setRotation(0f);
}
} else {
helper.setImageResource(R.id.iv, R.mipmap.arrow_r);
if (isAnimate) {
ViewCompat.animate(imageView).setDuration(200)
.setInterpolator(new DecelerateInterpolator())
.rotation(90f)
.start();
} else {
imageView.setRotation(90f);
}
}
}
@Override
public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
getAdapter().expandOrCollapse(position);
// 这里使用payload进行增量刷新(避免整个item刷新导致的闪烁,不自然)
getAdapter().expandOrCollapse(position, true, true, NodeTreeAdapter.EXPAND_COLLAPSE_PAYLOAD);
}
}
......@@ -5,7 +5,8 @@
android:layout_marginBottom="1dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
android:orientation="horizontal"
android:paddingRight="16dp">
<ImageView
android:id="@+id/iv_head"
......@@ -30,6 +31,5 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:paddingRight="16dp"
android:src="@mipmap/arrow_r" />
</LinearLayout>
\ No newline at end of file
......@@ -9,7 +9,6 @@ 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 com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlin.math.max
abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
: BaseProviderMultiAdapter<BaseNode>(data) {
......@@ -131,7 +130,14 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
val newFlatData = flatData(arrayListOf(data))
this.data.addAll(index, newFlatData)
notifyItemRangeChanged(index + getHeaderLayoutCount(), max(removeCount, newFlatData.size))
if (removeCount == newFlatData.size) {
notifyItemRangeChanged(index + getHeaderLayoutCount(), removeCount)
} else {
notifyItemRangeRemoved(index + getHeaderLayoutCount(), removeCount)
notifyItemRangeInserted(index + getHeaderLayoutCount(), newFlatData.size)
// notifyItemRangeChanged(index + getHeaderLayoutCount(), max(removeCount, newFlatData.size)
}
}
/**
......@@ -369,7 +375,6 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
val parentIndex = this.data.indexOf(parentNode)
val removeCount = removeChildAt(parentIndex)
notifyItemRangeRemoved(parentIndex + 1 + getHeaderLayoutCount(), removeCount)
it.clear()
it.addAll(newData)
......@@ -377,7 +382,13 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
val newFlatData = flatData(newData)
this.data.addAll(parentIndex + 1, newFlatData)
notifyItemRangeInserted(parentIndex + 1 + getHeaderLayoutCount(), newFlatData.size)
val positionStart = parentIndex + 1 + getHeaderLayoutCount()
if (removeCount == newFlatData.size) {
notifyItemRangeChanged(positionStart, removeCount)
} else {
notifyItemRangeRemoved(positionStart, removeCount)
notifyItemRangeInserted(positionStart, newFlatData.size)
}
// notifyItemRangeChanged(parentIndex + 1 + getHeaderLayoutCount(), max(removeCount, newFlatData.size))
}
}
......@@ -438,7 +449,8 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
private fun collapse(@IntRange(from = 0) position: Int,
isChangeChildCollapse: Boolean = false,
animate: Boolean = true,
notify: Boolean = true): Int {
notify: Boolean = true,
parentPayload: Any? = null): Int {
val node = this.data[position]
if (node is BaseExpandNode && node.isExpanded) {
......@@ -446,7 +458,7 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
node.isExpanded = false
if (node.childNode.isNullOrEmpty()) {
notifyItemChanged(adapterPosition)
notifyItemChanged(adapterPosition, parentPayload)
return 0
}
val items = flatData(node.childNode!!, if (isChangeChildCollapse) false else null)
......@@ -454,7 +466,7 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
this.data.removeAll(items)
if (notify) {
if (animate) {
notifyItemChanged(adapterPosition)
notifyItemChanged(adapterPosition, parentPayload)
notifyItemRangeRemoved(adapterPosition + 1, size)
} else {
notifyDataSetChanged()
......@@ -477,7 +489,8 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
private fun expand(@IntRange(from = 0) position: Int,
isChangeChildExpand: Boolean = false,
animate: Boolean = true,
notify: Boolean = true): Int {
notify: Boolean = true,
parentPayload: Any? = null): Int {
val node = this.data[position]
if (node is BaseExpandNode && !node.isExpanded) {
......@@ -485,7 +498,7 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
node.isExpanded = true
if (node.childNode.isNullOrEmpty()) {
notifyItemChanged(adapterPosition)
notifyItemChanged(adapterPosition, parentPayload)
return 0
}
val items = flatData(node.childNode!!, if (isChangeChildExpand) true else null)
......@@ -493,7 +506,7 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
this.data.addAll(position + 1, items)
if (notify) {
if (animate) {
notifyItemChanged(adapterPosition)
notifyItemChanged(adapterPosition, parentPayload)
notifyItemRangeInserted(adapterPosition + 1, size)
} else {
notifyDataSetChanged()
......@@ -513,8 +526,9 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
@JvmOverloads
fun collapse(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true): Int {
return collapse(position, false, animate, notify)
notify: Boolean = true,
parentPayload: Any? = null): Int {
return collapse(position, false, animate, notify, parentPayload)
}
/**
......@@ -526,8 +540,9 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
@JvmOverloads
fun expand(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true): Int {
return expand(position, false, animate, notify)
notify: Boolean = true,
parentPayload: Any? = null): Int {
return expand(position, false, animate, notify, parentPayload)
}
/**
......@@ -537,26 +552,35 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
* @param notify Boolean
*/
@JvmOverloads
fun expandOrCollapse(@IntRange(from = 0) position: Int, animate: Boolean = true, notify: Boolean = true): 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) {
collapse(position, false, animate, notify)
collapse(position, false, animate, notify, parentPayload)
} else {
expand(position, false, animate, notify)
expand(position, false, animate, notify, parentPayload)
}
}
return 0
}
@JvmOverloads
fun expandAndChild(@IntRange(from = 0) position: Int, animate: Boolean = true, notify: Boolean = true): Int {
return expand(position, true, animate, notify)
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): Int {
return collapse(position, true, animate, notify)
fun collapseAndChild(@IntRange(from = 0) position: Int,
animate: Boolean = true,
notify: Boolean = true,
parentPayload: Any? = null): Int {
return collapse(position, true, animate, notify, parentPayload)
}
/**
......@@ -572,9 +596,11 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
isExpandedChild: Boolean = false,
isCollapseChild: Boolean = true,
animate: Boolean = true,
notify: Boolean = true) {
notify: Boolean = true,
expandPayload: Any? = null,
collapsePayload: Any? = null) {
val expandCount = expand(position, isExpandedChild, animate, notify)
val expandCount = expand(position, isExpandedChild, animate, notify, expandPayload)
if (expandCount == 0) {
return
}
......@@ -597,7 +623,7 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
// 从顶部开始位置循环
var i = firstPosition
do {
val collapseSize = collapse(i, isCollapseChild, animate, notify)
val collapseSize = collapse(i, isCollapseChild, animate, notify, collapsePayload)
i++
// 每次折叠后,重新计算新的 Position
newPosition -= collapseSize
......@@ -616,7 +642,7 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
if ((newPosition + expandCount) < lastPosition) {
var i = newPosition + expandCount + 1
while (i <= lastPosition) {
val collapseSize = collapse(i, isCollapseChild, animate, notify)
val collapseSize = collapse(i, isCollapseChild, animate, notify, collapsePayload)
i++
lastPosition -= collapseSize
}
......
......@@ -73,22 +73,22 @@ open class BaseViewHolder(view: View) : RecyclerView.ViewHolder(view) {
return this
}
open fun setImageDrawable(@IdRes viewId: Int, drawable: Drawable?): BaseViewHolder? {
open fun setImageDrawable(@IdRes viewId: Int, drawable: Drawable?): BaseViewHolder {
getView<ImageView>(viewId).setImageDrawable(drawable)
return this
}
open fun setImageBitmap(@IdRes viewId: Int, bitmap: Bitmap?): BaseViewHolder? {
open fun setImageBitmap(@IdRes viewId: Int, bitmap: Bitmap?): BaseViewHolder {
getView<ImageView>(viewId).setImageBitmap(bitmap)
return this
}
open fun setBackgroundColor(@IdRes viewId: Int, @ColorInt color: Int): BaseViewHolder? {
open fun setBackgroundColor(@IdRes viewId: Int, @ColorInt color: Int): BaseViewHolder {
getView<View>(viewId).setBackgroundColor(color)
return this
}
open fun setBackgroundResource(@IdRes viewId: Int, @DrawableRes backgroundRes: Int): BaseViewHolder? {
open fun setBackgroundResource(@IdRes viewId: Int, @DrawableRes backgroundRes: Int): BaseViewHolder {
getView<View>(viewId).setBackgroundResource(backgroundRes)
return this
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册