gpt4 book ai didi

java - RecyclerView 反向无限滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:27 24 4
gpt4 key购买 nike

我正在创建一个聊天应用程序,我正在尝试以相反的方式为 RecyclerView 实现无限滚动,比如从底部开始向上滚动,当到达顶部时,加载更多。

当用户打开聊天屏幕时,应用会获取最后 20 条消息并默认滚动到底部。我想在用户向上滚动时从服务器获取更多消息。

以下只是我正在测试的通用无限滚动代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

layoutManager = new LinearLayoutManager(this);
recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);


adapter = new RecyclerViewAdapter<String, ItemViewHolder>(String.class,R.layout.item,ItemViewHolder.class,list) {
@Override
protected void populateViewHolder(ItemViewHolder viewHolder, String model, int position) {
if(model != null){
viewHolder.textView.setText(model);
}
}
};
recyclerView.setAdapter(adapter);
recyclerView.setOnScrollListener(new EndlessReverseRecyclerViewScrollListener(layoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
loadMore();
}
});
loadInit();
}

private void loadInit(){
for(int x = 0; x < 20;x++){
list.add(list.size()+": "+getRandomString(8));
adapter.notifyDataSetChanged();
}
layoutManager.scrollToPosition(list.size() -1);
}

private void loadMore(){
for(int x = 0; x < 20;x++){
list.add(list.size()+": "+getRandomString(8));
adapter.notifyDataSetChanged();
}
}

无尽的滚动类

public EndlessReverseRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
this.layoutManager = layoutManager;
}

// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
int lastVisibleItemPosition = 0;
int totalItemCount = layoutManager.getItemCount();



if (layoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null);
// get maximum element within the list
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
} else if (layoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
} else if (layoutManager instanceof LinearLayoutManager) {
lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
}

Log.d(TAG," *** last visible: "+lastVisibleItemPosition);
Log.d(TAG," *** total: "+totalItemCount);
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) {
this.loading = true;
}
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}

// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
// threshold should reflect how many total columns there are too
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount, view);
loading = true;
}
}

// Call this method whenever performing new searches
public void resetState() {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = 0;
this.loading = true;
}

// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);

这非常适合从上到下滚动,有人可以帮助我如何让它从下到上工作。谢谢

附言ReverseLayout 不是我正在寻找的解决方案,因为它只是颠倒了我的项目的顺序,而这不是我想要实现的。

最佳答案

RecyclerView 的 LayoutManager 上的 reverseLayout 属性将执行您想要的操作。

Kotlin :

myRecyclerView.layoutManager = LinearLayoutManager(
context = this,
orientation = LinearLayoutManager.VERTICAL,
reverseLayout = true
).apply { stackFromEnd = true }

XML:

<androidx.recyclerview.widget.RecyclerView
...
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:reverseLayout="true"
app:stackFromEnd="true"
android:orientation="vertical />

如果您不希望您的项目从底部堆叠,您可以将 stackFromEnd 设置为 false

关于java - RecyclerView 反向无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com