Comments (15)
我在 sample 中提供了一个 one2many 示例,讲解的就是如何通过一个整形 int type 字段来区分 items,没有 gson 序列化拦截,没有复杂的处理逻辑,各位可以再看看:
https://github.com/drakeet/MultiType/tree/master/sample/src/main/java/me/drakeet/multitype/sample/one2many
@burgessjp @lizhangqu @lixi0912
from multitype.
-
不改源码的话,就是创建在包一层,具体可以参考 Message - MessageContent 的包装
传送门: TimeMachine -
修改源码的话~
a) 修改 MultiPool 的机制,将两个 Array 替换成 SparseArrayCompat,由 viewType 去对应 viewProvider。
b) 修改 MultiTypeAdapter
from multitype.
感谢 @lixi0912 帮忙解答!
题主这是需要定制二层分发的需求,在 MultiType 2.0 提供了 FlatTypeAdapter
用于扁平化二级 index, example by TimeMachine:
public class MessageAdapter extends MultiTypeAdapter {
public MessageAdapter(@NonNull List<Message> messages) {
super(messages);
}
@NonNull @Override public Class onFlattenClass(@NonNull Item message) {
return ((Message) message).content.getClass();
}
}
onFlattenClass
接口方法中,可以对 Item
判断是否进行二级定位。
from multitype.
大概懂了。
是不是大概是这样的:还是以NewsItem来说,数据结构大致是这样的,
public class NewsItem implements Item
{
public String title;
public String content;
public int itemType
}
比如说我有3个不同的itemType,就需要去新建3个不同的类去继承于NewsItem,然后重写MultiTypeAdapter 中的onFlattenClass去根据itemType去得到相应的子类,是吗?
from multitype.
不是这样,我的 Message - Content 不是继承关系,而是包含、组合关系
from multitype.
你的数据结构比较令人混乱不清,你可以重新理清楚一下。
from multitype.
数据结构没问题,我知道你的Message - Content 是包含、组合关系,我这里和你那个有点不同,问题我更新了下。
from multitype.
我明白了,你的做法很聪明!如果你已经这么做了:
新建 3 个不同的类去继承于 NewsItem
那么是可以如此:
@NonNull @Override public Class onFlattenClass(@NonNull Item item) {
if (item.type == 1) {
return ItemOne.class;
} else if (item.type == 2) {
return ItemTwo.class;
}
...
}
from multitype.
那就差不多知道了,我也是看你的这个库设计的很精妙,准备用到项目中去,谢谢你的解答。
from multitype.
不过话说回来,你的需求还是略奇怪,哈哈,怎么会有这样的需求呢(思考状...
我这个小库子也是极力追求清晰、简单和灵活,能不做过多封装就不做,你看不到什么空白页、什么 Header View、下拉刷新等等封装。所以其实它的代码很容易读懂也能很快读完,因此,如果有什么没有提供的功能,或想自定义成更加简单的方式(大多情况,更加简单意味着灵活性的丢失),大可随意、大胆地去继承各个类进行拓展和实现——由于它的精巧设计,这么做通常是很直接和容易的,这是原本就留出来的。
from multitype.
我现在遇到的基本都是这种,我感觉这是很常规的做法,可能是我习惯了,哈哈。准备去拜读一下你的TimeMachine了。
from multitype.
@NonNull @Override public Class onFlattenClass(@NonNull Item item) {
if (item.type == 1) {
return ItemOne.class;
} else if (item.type == 2) {
return ItemTwo.class;
}
...
}
@drakeet 这段代码是有问题的,会存在一个不能转型的异常。
而且这种数据结构是很正常的
public class MessageItem
{
public String title;
public String content;
public int roleType
}
根据roleType判断使用哪种布局
from multitype.
onFlattenClass 不应该做数据类型分发而应该用来做数据扁平化。
from multitype.
@Override
public WeiboContent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
Gson gson = GsonProvider.gson;
JsonObject jsonObject = (JsonObject) json;
final String contentType = stringOrEmpty(jsonObject.get("content_type"));
WeiboContent content = null;
if (contentType.equals(SimpleText.TYPE)) {
content = gson.fromJson(json, SimpleText.class);
} else if (contentType.equals(SimpleImage.TYPE)) {
content = gson.fromJson(json, SimpleImage.class);
}
return content;
}
这种方式,是在反序列化的时候处理掉,在用gson的时候,会显得很方便,但是使用fastjson时,分type解析显得有点过于麻烦,往往有时候需要解析两次,会不合理。
from multitype.
那只能怪 fastjson,如果硬要使用 fastjson,那就考虑不要采用这种抽象嵌套设计。另外,有问题欢迎开 issue,不要在这个 issue 下讨论无关主题的内容,保持纯粹。
from multitype.
Related Issues (20)
- ViewDelegate#getAdapterPosition NPE
- 嵌套使用,ItemViewDelegate对象出现多次重复加载,一直死循环。 HOT 7
- 在 ConcatAdapter 中,itemViewType获取错误导致的崩溃 HOT 6
- 在用MultiType来实现一个app的二级评论,如何处理点击事件呢? HOT 1
- Types接口是否支持oneToMany注册Type?
- 使用OneToMany问题 HOT 1
- 我该如何在 ItemViewDelegate 中操作 items ? HOT 2
- 大家好,我使用MultiTypeAdapter的时候itme复用是数据混乱了,请问有人遇到吗? HOT 2
- release模式下,混淆失败 HOT 2
- Rename & set typealias
- 多个布局作为一个itemview会显示不全 HOT 1
- 恕我直言,多类型实体类实现过于复杂!虽然item多样式,但后台返回外层json类型是一个,不可能根据你item的不同拆分bean的不同,不如MultiAdapter库。 HOT 2
- 怎么添加头尾布局?
- java.lang.VerifyError: Rejecting class that attempts to sub-type erroneous class com.drakeet.multitype.ItemViewBinder
- 请问 Demo中 BilibiliActivity 中嵌套的横向 PostAdapter 可以是 MultiTypeAdapter 吗?
- 在kotlin ItemViewBinder中, 在binder内部实现自我删除。items为List,不是mutableList。不能删除数据。items的类型 是否改为MutableList或者ArrayList<>
- 啥时候出一版支持Jetpack Compose呀?!
- 请问MultiType与AsyncListDiffer如何结合使用
- 我正在开发OpenHarmony应用,使用JS语言,你们能否支持JS版本?我愿意参与开发,期待回复。 HOT 2
- link 自定义问题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multitype.