* 判断是否是同一个item * * @param oldItem New data * @param newItem old Data * @return */ @Override public boolean areItemsTheSame(@NonNull DiffEntity oldItem, @NonNull DiffEntity newItem) { return oldItem.getId() == newItem.getId(); } /** * When it is the same item, judge whether the content has changed. *
* 当是同一个item时,再判断内容是否发生改变
*
* @param oldItem New data
* @param newItem old Data
* @return
*/
@Override
public boolean areContentsTheSame(@NonNull DiffEntity oldItem, @NonNull DiffEntity newItem) {
return oldItem.getTitle().equals(newItem.getTitle())
&& oldItem.getContent().equals(newItem.getContent())
&& oldItem.getDate().equals(newItem.getDate());
}
/**
* Optional implementation
* Implement this method if you need to precisely modify the content of a view.
* If this method is not implemented, or if null is returned, the entire item will be refreshed.
*
* 可选实现
* 如果需要精确修改某一个view中的内容,请实现此方法。
* 如果不实现此方法,或者返回null,将会直接刷新整个item。
*
* @param oldItem Old data
* @param newItem New data
* @return Payload info. if return null, the entire item will be refreshed.
*/
@Override
public Object getChangePayload(@NonNull DiffEntity oldItem, @NonNull DiffEntity newItem) {
return null;
}
}
================================================
FILE: app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/differ/adapter/DiffUtilAdapter.java
================================================
package com.chad.baserecyclerviewadapterhelper.activity.differ.adapter;
import android.content.Context;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.entity.DiffEntity;
import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.viewholder.QuickViewHolder;
/**
* Create adapter
*/
public class DiffUtilAdapter extends BaseQuickAdapter> = moshi.adapter(type)
return jsonAdapter.fromJson(JSON) ?: throw IllegalStateException("json 解析出错")
}
companion object {
// 此种格式的json,在业务场景中比较常见
private const val JSON = """
[{
"group_name": "1",
"group_list": [{
"title": "patton",
"content": "this is content"
},
{
"title": "nicole",
"content": "this is content"
},
{
"title": "anthony",
"content": "this is content"
}
]
},
{
"group_name": "2",
"group_list": [{
"title": "zane",
"content": "this is content"
},
{
"title": "venus",
"content": "this is content"
},
{
"title": "yahya",
"content": "this is content"
},
{
"title": "starlight",
"content": "this is content"
},
{
"title": "twinkle",
"content": "this is content"
}
]
},
{
"group_name": "3",
"group_list": [{
"title": "esther",
"content": "this is content"
},
{
"title": "asta",
"content": "this is content"
},
{
"title": "gary",
"content": "this is content"
}
]
},
{
"group_name": "4",
"group_list": [{
"title": "peter",
"content": "this is content"
},
{
"title": "aldrich",
"content": "this is content"
}
]
},
{
"group_name": "5",
"group_list": [{
"title": "edgar",
"content": "this is content"
},
{
"title": "danika",
"content": "this is content"
},
{
"title": "clement",
"content": "this is content"
}
]
}
]
"""
}
}
================================================
FILE: app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/scene/adapter/GroupAdapter.kt
================================================
package com.chad.baserecyclerviewadapterhelper.activity.scene.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.chad.baserecyclerviewadapterhelper.R
import com.chad.baserecyclerviewadapterhelper.databinding.ItemGroupTypeBinding
import com.chad.baserecyclerviewadapterhelper.entity.GroupDemoEntity
import com.chad.baserecyclerviewadapterhelper.utils.dp
import com.chad.library.adapter4.BaseQuickAdapter
/**
* 每一组的Adapter
*
*/
class GroupAdapter : BaseQuickAdapter
// * 配合Brvah的Section使用,不对Head生效,仅对每个Head的子Grid列表生效
// * Section Grid中Item的宽度应设为MATCH_PARAENT
// *
// * @author : renpeng
// * @since : 2018/9/29
// */
//public class GridSectionAverageGapItemDecoration extends RecyclerView.ItemDecoration {
//
// private class Section {
// public int startPos = 0;
// public int endPos = 0;
//
// public int getCount() {
// return endPos - startPos + 1;
// }
//
// public boolean contains(int pos) {
// return pos >= startPos && pos <= endPos;
// }
//
// @Override
// public String toString() {
// return "Section{" +
// "startPos=" + startPos +
// ", endPos=" + endPos +
// '}';
// }
// }
//
// private float gapHorizontalDp;
// private float gapVerticalDp;
// private float sectionEdgeHPaddingDp;
// private float sectionEdgeVPaddingDp;
// private int gapHSizePx = -1;
// private int gapVSizePx = -1;
// private int sectionEdgeHPaddingPx;
// private int eachItemHPaddingPx; //每个条目应该在水平方向上加的padding 总大小,即=paddingLeft+paddingRight
// private int sectionEdgeVPaddingPx;
// private List