Skip to content

Commit

Permalink
# fix:
Browse files Browse the repository at this point in the history
- `setList` bug
  • Loading branch information
limuyang committed Mar 28, 2020
1 parent 3573400 commit 90f360e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
initAdapter();
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
Tips.show("onItemClick " + position);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public ItemImgTextViewBinding onCreateViewBinding(@NotNull LayoutInflater layout

@Override
public void convert(@NotNull BinderVBHolder<ItemImgTextViewBinding> holder, Video data) {
holder.getViewBinding().tv.setText(data.getName());
holder.getViewBinding().tv.setText("(ViewBinding) " + data.getName());
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.adapter.node.tree.NodeTreeAdapter;
import com.chad.baserecyclerviewadapterhelper.entity.node.tree.FirstNode;
import com.chad.baserecyclerviewadapterhelper.entity.node.tree.SecondNode;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class FirstProvider extends BaseNodeProvider {
Expand Down Expand Up @@ -77,6 +79,8 @@ private void setArrowSpin(BaseViewHolder helper, BaseNode data, boolean isAnimat
@Override
public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
// 这里使用payload进行增量刷新(避免整个item刷新导致的闪烁,不自然)
getAdapter().expandOrCollapse(position, true, true, NodeTreeAdapter.EXPAND_COLLAPSE_PAYLOAD);
// getAdapter().expandOrCollapse(position, true, true, NodeTreeAdapter.EXPAND_COLLAPSE_PAYLOAD);
getAdapter().nodeAddData(data,new SecondNode(new ArrayList<>(),"p:"+position));
getAdapter().notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ public void convert(@NotNull BaseViewHolder helper, @NotNull BaseNode data) {

@Override
public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
SecondNode entity = (SecondNode) data;
if (entity.isExpanded()) {
getAdapter().collapse(position);
} else {
getAdapter().expandAndCollapseOther(position);
}
// SecondNode entity = (SecondNode) data;
// if (entity.isExpanded()) {
// getAdapter().collapse(position);
// } else {
// getAdapter().expandAndCollapseOther(position);
// }

// BaseNode fNode = getAdapter().findParentNode(position);
// getAdapter().nodeRemoveData();
// getAdapter().notifyItemRemoved(position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import com.chad.library.adapter.base.viewholder.BaseViewHolder
* 使用 Binder 来实现adapter,既可以实现单布局,也能实现多布局
* 数据实体类也不存继承问题
*
* 当有多种条目的时候,避免在convert()中做太多的业务逻辑,把逻辑放在对应的 BaseItemBinder 中。
* 适用于以下情况:
* 1、实体类不方便扩展,此Adapter的数据类型可以是任意类型,默认情况下不需要实现 getItemType
* 2、item 类型较多,在convert()中管理起来复杂
*
* ViewHolder 由 [BaseItemBinder] 实现,并且每个[BaseItemBinder]可以拥有自己类型的ViewHolder类型。
*
* 数据类型为Any
*/
open class BaseBinderAdapter(list: MutableList<Any>? = null) : BaseQuickAdapter<Any, BaseViewHolder>(0, list) {
Expand Down Expand Up @@ -79,9 +86,7 @@ open class BaseBinderAdapter(list: MutableList<Any>? = null) : BaseQuickAdapter<
}

override fun getDefItemViewType(position: Int): Int {
val type = mTypeMap[data[position].javaClass]
checkNotNull(type) { "getDefItemViewType: index: $position - type: ${data[position].javaClass} Not Find!" }
return type
return findViewType(data[position].javaClass)
}

override fun bindViewClickListener(viewHolder: BaseViewHolder, viewType: Int) {
Expand All @@ -105,6 +110,12 @@ open class BaseBinderAdapter(list: MutableList<Any>? = null) : BaseQuickAdapter<
return getItemBinderOrNull(holder.itemViewType)?.onFailedToRecycleView(holder) ?: false
}

protected fun findViewType(clazz : Class<*>):Int {
val type = mTypeMap[clazz]
checkNotNull(type) { "findViewType: ViewType: $clazz Not Find!" }
return type
}

protected open fun bindClick(viewHolder: BaseViewHolder) {
if (getOnItemClickListener() == null) {
//如果没有设置点击监听,则回调给 itemProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.chad.library.adapter.base.viewholder.BaseViewHolder
* @param T data 数据类型
* @constructor
*/
@Deprecated("please use Class BaseBinderAdapter")
abstract class BaseProviderMultiAdapter<T>(data: MutableList<T>? = null) :
BaseQuickAdapter<T, BaseViewHolder>(0, data) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Modifier
import java.lang.reflect.ParameterizedType
import java.util.*
import kotlin.collections.ArrayList

/**
* 获取模块
Expand Down Expand Up @@ -1158,17 +1159,24 @@ abstract class BaseQuickAdapter<T, VH : BaseViewHolder>
* @param list Collection<T>?
*/
open fun setList(list: Collection<T>?) {
// 不是同一个引用才清空列表
if (list !== this.data) {
this.data.clear()
if (!list.isNullOrEmpty()) {
this.data.addAll(list)
}
mLoadMoreModule?.reset()
mLastPosition = -1
notifyDataSetChanged()
mLoadMoreModule?.checkDisableLoadMoreIfNotFullPage()
} else {
if (!list.isNullOrEmpty()) {
val newList = ArrayList(list)
this.data.clear()
this.data.addAll(newList)
} else {
this.data.clear()
}
}
mLoadMoreModule?.reset()
mLastPosition = -1
notifyDataSetChanged()
mLoadMoreModule?.checkDisableLoadMoreIfNotFullPage()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public interface OnItemClickListener {
* will be a view provided by the adapter)
* @param position The position of the view in the adapter.
*/
void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position);
void onItemClick(@NonNull BaseQuickAdapter<?,?> adapter, @NonNull View view, int position);
}

0 comments on commit 90f360e

Please sign in to comment.