CymChad / CymChad/BaseRecyclerViewAdapterHelper
UpFetch聊天消息列表 下拉无限加载
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 24.6k
- Forks
- 5.2k
- PR merge metrics
- No merged PRs in 30d
Description
我想做一个聊天消息对话框,下拉时可以加载更多历史的聊天记录,使用upFetchModule时遇到无限加载的问题:
`BaseUpFetchModule upFetchModule = messageAdapter.getUpFetchModule();
upFetchModule.setUpFetchEnable(true);
upFetchModule.setOnUpFetchListener(() -> {
if(!upFetchModule.isUpFetching()){
upFetchModule.setUpFetching(true);
rvMessage.postDelayed(()->{
upFetchModule.setUpFetching(false);
List<Message> messages=new ArrayList<>();
for (int i = 0; i < 9; i++) {
Message m = new Message();
m.setType((byte) 1);
m.setSenderId(98689);
m.setContent(String.valueOf(i));
messages.add(m);
}
messageAdapter.addData(0,messages);
rvMessage.scrollToPosition(messages.size());
if(messageAdapter.getData().size()>100){
upFetchModule.setUpFetchEnable(false);
}
},1000);
}
});
`
而且我也知道问题原理,就是因为messageAdapter.addData(0,messages)执行后会自动滚动到顶部,导致adapter需要加载第一项数据,从而触发OnUpFetch,导致无限循环!
目前我的解决方案:rvMessage.scrollToPosition(messages.size());
让recycleview加载完数据后滚动到“下面”,这样就不会触发adapter加载第一项数据。
但是这个前提条件是新加载到recycleview的数据(List messages)超过了一屏,感觉还是不太靠谱。。。
所以想请教一下有没有更好的解决方案。
PS:我使用的版本是:3.0.4
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the infinite loading behavior with BaseUpFetchModule, messageAdapter.addData(0, messages), and rvMessage using version 3.0.4. Read the up-fetch callback and adapter insertion behavior first; done means loading older messages does not recursively trigger another fetch, including when the inserted list is shorter than one screen.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100