当前位置:   article > 正文

分而治之_使用ConcatAdapter进行分而治之

concatadapter

分而治之

TL;DR: With ConcatAdapter we can breakdown a screen into small pieces, so, we can manage each piece as a feature and adapt to changes. It’s like playing with Lego blocks!

TL; DR: 使用ConcatAdapter,我们可以将屏幕分解为小块,因此,我们可以将每个块作为功能进行管理并适应变化。 就像玩乐高积木一样!

What is ConcatAdapter?

什么是ConcatAdapter?

ConcatAdapter allows us to display the contents of multiple adapters, in a sequence.

ConcatAdapter允许我们按顺序显示多个适配器的内容。

Benefits of using ConcatAdapter:

使用ConcatAdapter的好处:

  1. Keep our layouts simple, this is with fewer lines of XML, therefore, readable and easier to maintain.

    保持布局简单,这是因为使用更少的XML行,因此可读性强并且易于维护。
  2. Each adapter has a single responsibility and can be re-usable.

    每个适配器都有一个责任,并且可以重复使用。
  3. Adapt to changes: adding new features and applying changes to the screen will be easier.

    适应更改:添加新功能并在屏幕上应用更改将更加容易。

Real-Life Use Cases

现实生活中的用例

Let’s see the following Product Detail screen:

让我们看下面的“产品详细信息”屏幕:

Image for post

The layout would be something like this (some attributes are omitted for simplicity):

布局将如下所示(为简单起见,省略了一些属性):

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout><ScrollView>   <androidx.constraintlayout.widget.ConstraintLayout>      <ImageView                android:id="@+id/iv_product"      ... />      <TextView               android:id="@+id/tv_manufacturer"               tools:text="Beanfields"      ... />      <TextView               android:id="@+id/tv_product_name"               tools:text="Jalapeño Nacho Bean Chips"      .../>      <TextView               android:id="@+id/tv_short_description"               tools:text="5.5 oz bag"      ... />      <TextView               android:id="@+id/tv_rating"               tools:text="4.62"      ... />      <TextView               android:id="@+id/tv_price"               tools:text="$2.99"      ... />      <TextView               android:id="@+id/tv_discount"               tools:text="$3.79"      ... />      <TextView               android:id="@+id/tv_description_title"               tools:text="Description"      ... />      <TextView               android:id="@+id/tv_description"               tools:text="A description"      ... />      <TextView               android:id="@+id/tv_ingredients_title"               tools:text="Ingredients"      ... />      <TextView               android:id="@+id/tv_ingredients"               tools:text="Ingredients list"      ... />       </androidx.constraintlayout.widget.ConstraintLayout>     </ScrollView>   <Button       android:id="@+id/btn_add_to_cart"       tools:text="Add to Cart"   ... /></androidx.constraintlayout.widget.ConstraintLayout>

Now, we want to add the following features:

现在,我们要添加以下功能:

  1. Product image gallery.

    产品图片库。
  2. Product out of stock section.

    产品缺货部分。
Image for post

Adding the new features to the existing layout is possible (for sure) but we will end up having a layout that is difficult to follow up and to maintain.

可以(肯定)将新功能添加到现有布局中,但是最终我们将面临难以跟进和维护的布局。

I know what you’re thinking: we can create separate layouts and use the include XML tag. That’s true, but the final layout will be large as well. There is a better option

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/286584
推荐阅读
相关标签