不同的 Android Studio 中的 RecyclerView 是否应编写多个适配器?

huangapple go评论79阅读模式
英文:

should I write multiple adapters for similar recycler views in android studio?

问题

我想在我的应用中创建不同的可循环视图(Recycler Views)。它们都非常相似,因为每个可循环视图都使用相同的数据,只是设计不同。
是使用多个适配器(adapters)好呢,还是只创建一个适配器,然后让这个适配器处理不同的视图?

英文:

I want to create different recycler views in my app. They are all very similar, as every recycler view is using the same data. Only the design changed.
Is it better to use multiple adapters or to just create one adapter and let this adapter handle different views ?

答案1

得分: 1

一般来说,保持代码整洁简单是更好的做法。如果你拥有相同功能但不同显示效果的情况,最好使用一个适配器并具备不同的视图类型。

英文:

Generally it's better to keep your code clean and simple. if you have same functionality with different views, it's better to have one adapter with different view types.

答案2

得分: 1

这真的取决于情况 - 你可以为每种表示都创建独特的适配器,也可以使用一个带有通用功能的抽象类,在其中需要在每个子类中实现一些自定义的显示方法,或者你可以传入一个处理特定显示方式的对象 - 或者你可以只有一个带有一些选项的适配器,根据选项显示这个或那个,加载这个布局或那个布局,无论是什么。

你选择哪种方式取决于你的代码 - 减少重复的想法是为了让代码更易于阅读和维护。如果这些表示方式都相当不同(使用相同的数据无关紧要,重要的是你需要如何处理它),那么一个大的适配器中有很多条件逻辑来处理它们将会很复杂 - 如果你需要添加另一种显示类型,那么使用起来会很棘手。

个别的适配器是最简单的,但如果它们基本上都是重复的相同内容,那么这将是很多重复代码 - 并且更难维护。

我个人喜欢组合的方式,将实际的显示工作委托给某个组件,因此只有一个适配器和一个呈现组件,你可以将其传入以使其呈现某种方式。但同样,这可能会使事情过于复杂化。

不要让生活变得困难!也不要仅仅因为将来可能想要适应和重用某些东西而使其可适应 - 大多数情况下,你永远不会这样做,所以不要给自己比现在需要做的工作更多的任务。

英文:

It really depends - you could have unique adapters for each representation, you could have an abstract class with the general functionality where you need to implement some custom display method in each subclass, or you could pass in an object that handles displaying things a certain way - or you could just have one adapter with some options, and depending on the options you show this or that, load this layout or that one, whatever

Which one you go with depends on your code - the idea with cutting down on repetition is to make it easier to read and maintain. If the representations are all fairly different (doesn't matter if they're using the same data, it's what you have to do with it) then one big adapter with lots of conditional stuff to handle them all is going to be complex - and hard to work with if you need to add another type of display

Individual adapters is simplest, but it's a lot of wasted code if they're all mostly the same thing repeated - and harder to maintain.

I like the composition thing personally, having something you delegate the actual display stuff to, so there's one adapter and one presentation component you hand in to make it look one way or another. But again, that might be overcomplicating things

Don't make your life hard! And don't make something adaptable just because you might want to adapt it and reuse it in future - most of the time, you never will, so don't give yourself more work than you need to do right now

huangapple
  • 本文由 发表于 2020年8月22日 02:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定