侦听器/观察者实现RecyclerView列表和详细信息活动的逻辑模式 [英] Logical pattern for listeners/observers to implement RecyclerView list and details activities

查看:129
本文介绍了侦听器/观察者实现RecyclerView列表和详细信息活动的逻辑模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有多种方法可以实现RecyclerView列表,其中某些方法比其他方法更具逻辑性.超出简单列表的范围会导致数据更改,从而增加了复杂性.实施查看列表项详细信息的功能会带来更多的复杂性.

There seems to be various ways to implement RecyclerView lists, some more logical than others. Going beyond a simple list to one where the data changes increases the complexity. Additional complexity comes from implementing the ability to view the details of the list items.

尽管我在以这种方式实现列表方面取得了一些成功,但我感到我提出的内容不是有效的,也不是设计框架时想要的.看着我使用的各种方法,我一直说:这不是他们想要我做的方式."

Although I have had some success at implementing lists in this manner, I feel that what I've come-up with is not efficient and not what was intended when the framework was designed. Looking at the various methods I've used, I keep saying "this can't be the way they want me to do it".

我要检查的基本应用程序是一个显示来自SQLite数据库的记录的可滚动列表,让用户从列表中选择项目以查看详细信息,并让用户长按以切换属性一个物品.当然,通过各种视图,滚动,重新显示等,显示也应保持一致.

The basic application I wish to examine is one that displays records from a SQLite database in a scrollable list, let's a user select items from the list to see details, and lets a user long-click to toggle an attribute of an item. And of course, the display should remain consistent through the various views, scrolling, redisplays, etc.

此图显示了一个基本用例,没有任何基础数据更改.蓝色字是需要实现细节的区域.在这种情况下,点击"可能需要将模型和位置放入明细活动中,也许使用intent.putExtra().

This image shows a basic use-case that does not have any underlying data changes. The words in blue are areas where implementation details are needed. In this case, "click" would require getting the model and the position into the detail activity, perhaps with intent.putExtra().

与必须管理数据更改相比,上述功能非常简单.在下面的场景中,我们保持在同一活动中,但用户长按一下以采取行动来更新数据:

The above functionality is fairly straight-forward when compared to having to manage changes to the data. Below we have a scenario where we remain in the same activity, but the user takes action to update the data using a long click:

听众或旁听者的最佳去处是哪里?需要注意哪些物体?当然,视图需要更新(如何)?如何管理数据库更新?我们如何确保视图将正确重绘?

Where is the best place for the listener or observer? What objects need attention? Certainly the view needs to be updated (how)? How will the update to the database be managed? How can we make sure that the view will be redrawn properly?

下面我们有两个动作.长按类似于上面的长按,但是在明细活动中,我们是否使用模型列表的序列化副本进行操作?如果是这样,这些更改如何返回到真实"模型和数据库?

Below we have two actions. The long-click is similar to the long-click, above, but when in the detail activity, are we operating with a serialized copy of the model list? If so, how do those changes get back to the 'real' model and the database?

谁在监听,传递了哪些参数,这些参数如何用于保持数据同步?放置侦听器代码,使数据库和所有视图保持顺序的代码最合理,最易维护的位置在哪里?

Who is listening, what parameters are delivered, and how are those parameters used to keep the data synchronized? Where is the most logical and maintainable place to put the listener code, the code that keeps the database and views all in order?

该框架的设计者打算采用什么逻辑方法来处理这种看似直接的功能?应用程序类中是否应该有一些单实例霸主函数?我还没有看到有关此类事情的示例,但可以选择.

What was the logical approach, intended by the designers of the framework, for handing this seemingly straight-forward functionality? Should there be some single instance overlord function in the application class? I haven't seen examples with that kind of thing, but might be an option.

如果结果变得简单",我会感到惊讶,但它比我一直在处理的混乱混乱要容易得多.

I'd be surprised if this turned-out to be 'easy', but it has just got to be easier than the convoluted mess that I've been working through.

推荐答案

从应用程序中多个位置管理数据库的最简单方法之一是使用ContentProvider并为您表中的每个表指定content:// URI数据库.

One of the easiest ways to manage your database from multiple locations in an app is to use a ContentProvider and designate content:// URI's for each table in your database.

要维护主"列表视图:

  • 假设您有一个名为"animals"的数据库表,并且通过ContentProvider访问该表的URI为content://myPackage/animals.对于RecyclerView活动,在onCreate中,您将在content://myPackage/animals URI上启动CursorLoader.
  • 假设您正确设计了ContentProvider(即,插入,删除和更新调用以ContentProvider.notifyChange()结尾),则加载器将在表更改时自动查询和重新查询数据库.从加载器的onLoadFinished()回调中,获取它返回的游标,并使用它更新您的回收者视图适配器.尽管解释有些简化,但是即使在应用程序的其他部分对数据库进行了更改,这仍然是保持主列表更新的基本条件.
  • Suppose you have a database table called "animals" and the URI to access the table via the ContentProvider is content://myPackage/animals. For your RecyclerView activity, in onCreate you would start a CursorLoader on the content://myPackage/animals URI.
  • Assuming you design your ContentProvider correctly (ie. insert, delete and update calls end with ContentProvider.notifyChange()), your loader will automatically query and requery the database any time the table changes. From the loader's onLoadFinished() callback, you take the cursor it returns and update your recycler view adapter with it. Although a somewhat simplified explanation, this is pretty much the essentials to keep the master list updated even as changes to the database are made in other parts of the app.

要处理列表中的点击/长按:

To handle clicks/long clicks in the list:

  • 不一定有最佳方法"来执行此操作,但是在适配器的onCreateViewHolder()方法中,我通常采用某项的基本视图,并将包含该视图的ViewHolder设置为其单击侦听器.这是因为单击该项目时,ViewHolder知道有关该项目在列表中的位置的重要信息(使用getAdapterPosition()getItemId()).
  • 例如,如果用户长时间单击ID为3的项目,我将使用ContentResolver.update()和URI content://mypackage/animals/3更新数据库.更新成功后,主列表将自动重新查询数据库,并以ID为3的项目的新状态刷新列表.
  • There is not necessarily a "best way" to do this, but in the adapter's onCreateViewHolder() method I usually take the base view for an item and set the ViewHolder containing the view as its on click listener. This is because when the item is clicked, the ViewHolder knows important information about where it is within the list (using getAdapterPosition() or getItemId()).
  • If for example the user long clicked an item with an ID of 3, I would update the database using ContentResolver.update() and a URI of content://mypackage/animals/3. After the update was successful, the master list would then automatically requery the database and refresh the list with the new state for the item with ID #3.

这篇关于侦听器/观察者实现RecyclerView列表和详细信息活动的逻辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆