gpt4 book ai didi

java - 具有无限滚动优化的 GridView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:33 25 4
gpt4 key购买 nike

我创建了一个简单的应用程序,它从 Pixabay 获取图像,然后将它们显示在具有无限滚动功能的 GridView 中。

我的OnScrollListener:

public class BasicOnScrollListener implements AbsListView.OnScrollListener {

private IOnScroll onScroll;

public BasicOnScrollListener(IOnScroll action) {
this.onScroll = action;
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount >= totalItemCount - visibleItemCount)
onScroll.onReachedEnd();
}
}

负责数据处理的代码:

private List<Image> images = new ArrayList<>();

....

private void init() {
this.imageAdapter = new ImageAdapter(this, images);
this.gridView.setAdapter(imageAdapter);

populateGridView();
this.gridView.setOnScrollListener(new BasicOnScrollListener(() -> {
populateGridView();
}));
}

private void populateGridView() {
if (!isLoading) {
isLoading = true;
this.progressBar.setVisibility(View.VISIBLE);
imagesRepository.getImages(currentPage, PAGE_SIZE, new IOnRepositoryDataReturn<ImagesList>() {
@Override
public void onData(ImagesList data) {
clearIfNeeded();
images.addAll(data.images);
imageAdapter.notifyDataSetChanged();
onFinish();
}

@Override
public void onError(String error) {
Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show();
}

private void clearIfNeeded() {
if (images.size() > 1000) {
images.subList(0, 300).clear();
}
}

private void onFinish() {
progressBar.setVisibility(View.INVISIBLE);
isLoading = false;
currentPage = currentPage + 1;
}
});
}
}

一切正常,但我想对其进行优化。当 GridView 中已经有超过 1000 个项目时,我想删除前 300 个项目,这样我就不会遇到内存不足的问题。

问题是,当我简单地从列表中删除前 300 项时(如 IOnRepositoryDataReturn 实现中的 clearIfNeeded() 方法所示),屏幕会移动。我不再看到删除前看到的项目。

示例图像。 images 列表中的第一行(第 1-2 项)被删除的情况。

  • 左图 - 删除前的网格
  • 居中 - 删除后的网格(就目前而言,删除顶部的项目会将所有项目向上移动)
  • 正确 - 我希望它如何表现(移除某处的项目不会影响所见)

黑色方 block 代表 GridView

enter image description here

我想以某种方式调整 GridView 中的查看位置,这样我仍然会看到与删除之前相同的图像。

布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/ProgressSpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/GridView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/GridView" />

<GridView
android:id="@+id/GridView"
android:layout_width="match_parent"
android:layout_height="406dp"
android:layout_marginBottom="8dp"
android:numColumns="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</GridView>

Grid View 是否可行,还是我应该寻找其他可能性?

最佳答案

When there are already more than 1000 items in the GridView I'd like to remove the first 300 items, so I won't run into out of memory problems. Is it even possible with Grid View or should I look for some other possibility?

这是我的 2 美分。如果我理解正确,您的关注点似乎是防止 OutOfMemory 异常并提高内存使用率和性能。如果您使用 RecyclerViewGridLayoutManager 实现照片网格,它将减少许多内存问题。您尝试删除屏幕外的 300 个项目的方法似乎非常麻烦且容易出错。这是官方文档的摘录,标题为 Create a List with RecyclerView :

回收 View :

The RecyclerView uses a layout manager to position the individual items on the screen and determine when to reuse item views that are no longer visible to the user. To reuse (or recycle) a view, a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset. Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensive findViewById() lookups.

网格布局管理器:

GridLayoutManager arranges the items in a two-dimensional grid, like the squares on a checkerboard. Using a RecyclerView with GridLayoutManager provides functionality like the older GridView layout.

您可以在快速搜索中找到许多这方面的教程,例如:Android GridLayoutManager with RecyclerView

关于java - 具有无限滚动优化的 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53268545/

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