通过观察ViewModel在RecyclerView中搜索PagedList的LiveData

2024-04-19

使用 android Paging 库,可以很容易地从数据库中分块加载数据,并且 ViewModel 提供自动 UI 更新和数据保存。所有这些框架模块帮助我们在 Android 平台上创建一个出色的应用程序。

典型的 Android 应用程序必须显示项目列表并允许用户搜索该列表。这就是我想通过我的应用程序实现的目标。所以我通过阅读很多文档、教程甚至stackoverflow的答案来完成了一个实现。但我不太确定我是否做得正确或者我应该如何做。下面,我展示了使用 ViewModel 和 RecyclerView 实现分页库的方法。

请检查我的实施并纠正我的错误或告诉我应该如何做。我认为有许多像我一样的新 Android 开发人员仍然对如何正确执行它感到困惑,因为没有单一来源可以回答您关于此类实现的所有问题。

我只展示我认为重要的东西。我正在使用房间。这是我正在使用的实体。

@Entity(tableName = "event")
public class Event {
    @PrimaryKey(autoGenerate = true)
    public int id;

    public String title;
}

这是事件实体的 DAO。

@Dao
public interface EventDao {
    @Query("SELECT * FROM event WHERE event.title LIKE :searchTerm")
    DataSource.Factory<Integer, Event> getFilteredEvent(String searchTerm);
}

Here is 视图模型延伸Android视图模型它允许通过提供阅读和搜索LiveData>所有事件或根据搜索文本过滤的事件。我真的很挣扎,每次当filterEvent发生变化时,我都会创建新的LiveData,这可能是多余的或不好的。

private MutableLiveData<Event> filterEvent = new MutableLiveData<>();
private LiveData<PagedList<Event>> data;

private MeDB meDB;

public EventViewModel(Application application) {
    super(application);
    meDB = MeDB.getInstance(application);

    data = Transformations.switchMap(filterEvent, new Function<Event, LiveData<PagedList<Event>>>() {
        @Override
        public LiveData<PagedList<Event>> apply(Event event) {
            if (event == null) {
                // get all the events
                return new LivePagedListBuilder<>(meDB.getEventDao().getAllEvent(), 5).build();
            } else {
                // get events that match the title
                return new LivePagedListBuilder<>(meDB.getEventDao()
                          .getFilteredEvent("%" + event.title + "%"), 5).build();
            }
        }
    });
}

public LiveData<PagedList<Event>> getEvent(Event event) {
    filterEvent.setValue(event);
    return data;
}

对于搜索事件,我正在使用搜索视图。在 onQueryTextChange 中,我编写了以下代码来搜索或在未提供搜索词时显示所有事件,这意味着搜索已完成或取消。

Event dumpEvent;

@Override
public boolean onQueryTextChange(String newText) {

    if (newText.equals("") || newText.length() == 0) {
        // show all the events
        viewModel.getEvent(null).observe(this, events -> adapter.submitList(events));
    }

    // don't create more than one object of event; reuse it every time this methods gets called
    if (dumpEvent == null) {
        dumpEvent = new Event(newText, "", -1, -1);
    }

    dumpEvent.title = newText;

    // get event that match search terms
    viewModel.getEvent(dumpEvent).observe(this, events -> adapter.submitList(events));

    return true;
}

谢谢乔治·马奇比亚 https://stackoverflow.com/users/12681044/george-machibya为了他伟大的answer https://stackoverflow.com/a/59712085/1162154。但我更喜欢对其进行一些修改,如下所示:

  1. 在内存中保留未过滤的数据以使其更快或每次加载它们以优化内存之间存在权衡。我更喜欢将它们保留在内存中,所以我更改了部分代码,如下所示:
listAllFood = Transformations.switchMap(filterFoodName), input -> {
            if (input == null || input.equals("") || input.equals("%%")) {
                //check if the current value is empty load all data else search
                synchronized (this) {
                    //check data is loaded before or not
                    if (listAllFoodsInDb == null)
                        listAllFoodsInDb = new LivePagedListBuilder<>(
                                foodDao.loadAllFood(), config)
                                .build();
                }
                return listAllFoodsInDb;
            } else {
                return new LivePagedListBuilder<>(
                        foodDao.loadAllFoodFromSearch("%" + input + "%"), config)
                        .build();
            }
        });
  1. 拥有去抖器有助于减少对数据库的查询数量并提高性能。所以我开发了DebouncedLiveData如下所示的类并从中进行去抖实时数据filterFoodName.
public class DebouncedLiveData<T> extends MediatorLiveData<T> {

    private LiveData<T> mSource;
    private int mDuration;
    private Runnable debounceRunnable = new Runnable() {
        @Override
        public void run() {
            DebouncedLiveData.this.postValue(mSource.getValue());
        }
    };
    private Handler handler = new Handler();

    public DebouncedLiveData(LiveData<T> source, int duration) {
        this.mSource = source;
        this.mDuration = duration;

        this.addSource(mSource, new Observer<T>() {
            @Override
            public void onChanged(T t) {
                handler.removeCallbacks(debounceRunnable);
                handler.postDelayed(debounceRunnable, mDuration);
            }
        });
    }
}

然后像下面这样使用它:

listAllFood = Transformations.switchMap(new DebouncedLiveData<>(filterFoodName, 400), input -> {
...
});
  1. 我通常更喜欢使用数据绑定 https://developer.android.com/topic/libraries/data-binding在安卓中。通过使用两种方式的数据绑定,您不需要使用TextWatcher更多,您可以直接将 TextView 绑定到 viewModel。

顺便说一句,我修改了 George Machibya 解决方案并将其推送到我的 Github 中。更详细的你可以看看here https://github.com/mirmilad/PagingSearchFood.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过观察ViewModel在RecyclerView中搜索PagedList的LiveData 的相关文章

随机推荐

  • Heroku CLI 突然损坏

    突然 从一个命令到下一个命令 Heroic CLI 停止工作 无论我在命令行中输入什么内容 我都会收到相同的错误 见下文 heroku help commands is not a heroku command Perhaps you me
  • boto3 S3 对象解析

    我正在尝试编写一个 Python 脚本来处理存储在 S3 上的音频数据 我有一个 S3 对象 我正在使用它调用 def grabAudio filename directory obj s3client get object Bucket
  • 什么是 java.lang.Class[]

    What is java lang Class Thanks Puneet 参数化类型的类型参数不限于具体的 类或接口 Java 允许使用类型通配符作为 参数化类型的类型参数 通配符是类型参数 以 的形式 可能有上限或下限 鉴于 通配符表示
  • Rscript执行错误:没有那个文件或目录

    Rscript 的二进制文件可用 但当我尝试使用它时 我得到 Rscript helloworld r Rscript execution error No such file or directory 如果我只做 Rscript 它会带来
  • 使用 openCV 查找图像轮廓

    因为我想跟踪物体的运动 所以我需要图像序列的轮廓 有人知道怎么做这个吗 轮廓蒙版是一个二值图像 在运动发生处具有非零像素 您可以使用以下技术背景扣除 这里有两种方法 从当前帧中减去前一帧 只有两个帧中未发生变化的像素才会导致零 看cvSub
  • 使用 Gmail 的 PHP 邮件

    在我的 PHP Web 应用程序中 我希望在发生某些错误时通过电子邮件收到通知 我想使用我的 Gmail 帐户发送这些内容 这怎么可能做到呢 Gmail 的 SMTP 服务器需要非常具体的配置 From Gmail 帮助 http mail
  • 如何将文件从 Ansible Tower 上传到 Artifactory?

    我每次都会收到此错误 但仅在尝试从 Ansible Tower 上传到 Artifactory 时出现在特定文件上 这些文件都是在清单主机上创建的Oracle Collection Tool 然后在上传到 Artifactory 之前获取到
  • 在 Typescript 中声明并初始化字典

    给出以下代码 interface IPerson firstName string lastName string var persons id string IPerson p1 firstName F1 lastName L1 p2 f
  • Bootstrap 容器流体填充

    以下 HTML 生成不需要的填充 div class container fluid div class row div class col xs 12 test div div div 显示问题的屏幕截图 这里的答案都没有帮助我使用 Bo
  • Jenkins 服务始终在 minikube 上挂起

    我安装了minikube在本地 仪表板是 192 168 99 100 30000 我通过 helm 安装了 Jenkins helm install stable jenkins 然后服务始终挂起 kubectl get services
  • 我怎样才能得到dag中的execution_date?运算符的外部?

    我怎样才能获得execution date参数在 dag 之外 execution min execution date strftime M if execution min 00 logging info YES It s 00 fin
  • 如何获取文件相对于 Git 存储库根的路径?

    Example cd lib git absolute path test c how to do this lib test c Use git ls files https www git scm com docs git ls fil
  • 为什么需要锁来实现 readonly int 属性?

    我是线程新手 我遇到了一个自定义线程池实现示例blog http ferruh mavituna com net multithreading ve basit bir threadpool implementasyonu oku 我只粘贴
  • iPad 状态栏有时会消失,留下空白 - 但我从不隐藏它!

    我从不隐藏状态栏 但有时它会消失 这种模式似乎是在一些全屏转换之后发生的 从一个视图水平翻转到另一个视图 使用 QLPreviewController 当它被关闭时 状态栏就会消失 但是 我无法重现它 它只是有时会发生 永远不会在模拟器上
  • 强制 iOS 将 Unicode 符号渲染为平面/文本

    iPhone 现在可以自动将 2D unicode 符号转换为 3D 表情符号 例如 我需要 unicode 以传统的黑白字形格式呈现 而且 我无法轻松地将 Unicode 符号替换为图像 因为这是在 UITableView 行的 edit
  • @Request Mapping 中的生产和消费是什么

    我的项目中有以下代码行 RequestMapping value UrlHandler GET AUTHENTICATION produces application json consumes application json metho
  • 如何从不同列条件下的 PySpark 数据帧中提取数组元素?

    我有以下内容PySpark 输入数据框 index valuelist 1 0 10 20 30 2 0 11 21 31 0 0 14 12 15 Where 索引 双精度型 值列表 类型Vector it s 非数组 从上面的输入数据框
  • 程序加载期间添加的菜单项的 Vcl 样式问题

    我有一个 Delphi 10 4 2 程序 32 位 其中在程序加载期间添加菜单项 Application OnActivate 事件 编码为仅运行一次 如果没有 vcl 样式 新项目将正确显示 但是当应用样式时 例如屏幕截图中非常漂亮的
  • 受约束的成员函数和显式模板实例化

    G 和 Clang 一致认为以下代码片段不是有效的 C template
  • 通过观察ViewModel在RecyclerView中搜索PagedList的LiveData

    使用 android Paging 库 可以很容易地从数据库中分块加载数据 并且 ViewModel 提供自动 UI 更新和数据保存 所有这些框架模块帮助我们在 Android 平台上创建一个出色的应用程序 典型的 Android 应用程序