简体   繁体   English

多次添加同一视图-重复使用布局

[英]Add same view multiple times - Reuse a layout

I'm developing an app that shows the weather forecast of 4 different regions, 5 days in the future. 我正在开发一个应用,该应用可以显示未来5天的4个不同地区的天气预报。 Every region displays 5 days and every day has its own data: a TextView with the name of the day, a TextView with the main weather condition for that day, an ImageView that shows the weather icon, and two TextView with the min temp and max temp for that day. 每个区域显示5天,并且每天都有自己的数据:带有日期名称的TextView,具有当天主要天气状况的TextView,显示天气图标的ImageView和带有最小温度和最大温度的两个TextView那天的温度。 Like this: 像这样:

在此处输入图片说明

I implemented this in a SUPER AWFUL layout.xml file, treating each TextView and ImageView for all the 20 days of the 4 regions separately: 我在SUPER AWFUL layout.xml文件中实现了这一点,在4个区域的所有20天里分别处理了每个TextView和ImageView:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_weather_forecats_title"
            style="@style/tv_weather_11"
            android:text="@string/extended_forecast" />

        <TextView
            android:id="@+id/tv_fore_title_city"
            style="@style/tv_weather_6"
            android:text="@string/title_city" />

        <LinearLayout style="@style/ll_weather_1">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day1_city"
                    style="@style/tv_weather_7"
                    android:text="Mon"/>

                <TextView
                    android:id="@+id/tv_fore_cond1_city"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day1_city"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds" />

                <TextView
                    android:id="@+id/tv_fore_min_day1_city"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day1_city"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day2_city"
                    style="@style/tv_weather_7"
                    android:text="Tue"/>

                <TextView
                    android:id="@+id/tv_fore_cond2_city"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day2_city"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day2_city"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day2_city"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day3_city"
                    style="@style/tv_weather_7"
                    android:text="Wed"/>

                <TextView
                    android:id="@+id/tv_fore_cond3_city"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day3_city"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day3_city"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day3_city"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day4_city"
                    style="@style/tv_weather_7"
                    android:text="Thu"/>

                <TextView
                    android:id="@+id/tv_fore_cond4_city"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day4_city"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day4_city"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day4_city"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day5_city"
                    style="@style/tv_weather_7"
                    android:text="Fri"/>

                <TextView
                    android:id="@+id/tv_fore_cond5_city"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day5_city"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day5_city"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day5_city"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@color/colorDarkTextDivider" />

        <TextView
            android:id="@+id/tv_fore_title_east"
            style="@style/tv_weather_6"
            android:text="@string/title_east" />

        <LinearLayout style="@style/ll_weather_1">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day1_east"
                    style="@style/tv_weather_7"
                    android:text="Mon"/>

                <TextView
                    android:id="@+id/tv_fore_cond1_east"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day1_east"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day1_east"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day1_east"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day2_east"
                    style="@style/tv_weather_7"
                    android:text="Tue"/>

                <TextView
                    android:id="@+id/tv_fore_cond2_east"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day2_east"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day2_east"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day2_east"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day3_east"
                    style="@style/tv_weather_7"
                    android:text="Wed"/>

                <TextView
                    android:id="@+id/tv_fore_cond3_east"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day3_east"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day3_east"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day3_east"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day4_east"
                    style="@style/tv_weather_7"
                    android:text="Thu"/>

                <TextView
                    android:id="@+id/tv_fore_cond4_east"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day4_east"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds" />

                <TextView
                    android:id="@+id/tv_fore_min_day4_east"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day4_east"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day5_east"
                    style="@style/tv_weather_7"
                    android:text="Fri"/>

                <TextView
                    android:id="@+id/tv_fore_cond5_east"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day5_east"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day5_east"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day5_east"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@color/colorDarkTextDivider" />

        <TextView
            android:id="@+id/tv_fore_title_south"
            style="@style/tv_weather_6"
            android:text="@string/title_south" />

        <LinearLayout style="@style/ll_weather_1">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day1_south"
                    style="@style/tv_weather_7"
                    android:text="Mon"/>

                <TextView
                    android:id="@+id/tv_fore_cond1_south"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day1_south"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day1_south"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day1_south"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day2_south"
                    style="@style/tv_weather_7"
                    android:text="Tue"/>

                <TextView
                    android:id="@+id/tv_fore_cond2_south"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day2_south"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds"/>

                <TextView
                    android:id="@+id/tv_fore_min_day2_south"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day2_south"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day3_south"
                    style="@style/tv_weather_7"
                    android:text="Wed"/>

                <TextView
                    android:id="@+id/tv_fore_cond3_south"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day3_south"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds" />

                <TextView
                    android:id="@+id/tv_fore_min_day3_south"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day3_south"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day4_south"
                    style="@style/tv_weather_7"
                    android:text="Thu"/>

                <TextView
                    android:id="@+id/tv_fore_cond4_south"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day4_south"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds" />

                <TextView
                    android:id="@+id/tv_fore_min_day4_south"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day4_south"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fore_day5_south"
                    style="@style/tv_weather_7"
                    android:text="Fri"/>

                <TextView
                    android:id="@+id/tv_fore_cond5_south"
                    style="@style/tv_weather_8"
                    android:text="Light Rain"/>

                <ImageView
                    android:id="@+id/iv_fore_ic_day5_south"
                    style="@style/iv_weather_1"
                    android:src="@drawable/ic_main_broken_clouds" />

                <TextView
                    android:id="@+id/tv_fore_min_day5_south"
                    style="@style/tv_weather_9"
                    android:text="5"/>

                <TextView
                    android:id="@+id/tv_fore_max_day5_south"
                    style="@style/tv_weather_10"
                    android:text="15"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

On the Java file I have 100 declarations of views and use 100 lines to get the instance of the views, and another 100 set methods to set texts and images... I know, super/unbelievably awful. 在Java文件上,我有100个视图声明,并使用100行来获取视图实例,还有另外100个set方法来设置文本和图像...我知道,这太令人难以置信了。

This must be bad design for sure. 这肯定是错误的设计。 There must be a way to put a single view (day name, condition, image, min temp and max temp) in a layout and reuse it. 必须有一种方法可以在布局中放置单个视图(日期名称,条件,图像,最低温度和最高温度)并重新使用它。 Any idea? 任何想法?

You could create a custom view, encapsulating all your presentation logic and just add a number of those in your LinearLayout . 您可以创建一个自定义视图,封装所有演示逻辑,然后在LinearLayout添加一些逻辑。

You will avoid the XML code duplication and you will be able to have a single, well defined widget that suits your needs. 您将避免XML代码重复,并且您将能够拥有一个满足您需求的定义明确的小部件。 Also, this will reduce the amount of java code you need to bind your data to the existing views. 另外,这将减少将数据绑定到现有视图所需的Java代码量。

Assuming the data comes from some kind of Weather Api or your own implemented database over here. 假设数据来自某种类型的Weather Api或您自己在此处实现的数据库。

I have 2 solutions to achieve this 我有2个解决方案来实现这一目标

  1. Nested RecyclerView : Horizontal RecyclerView inside a Vertical RecyclerView 嵌套的RecyclerView:垂直RecyclerView中的水平RecyclerView

    • you would have a horizontal vertical layout 你会有一个水平的垂直布局

      在此处输入图片说明

      This layout would be repeated 7 times for each day. 每天将重复此布局7次。

    • Your Vertical RecyclerView would be a collection of such Horizontal RecyclerView (for regions) 您的Vertical RecyclerView将是此类Horizo​​ntal RecyclerView的集合(用于区域)

    • Use a ArrayList and Populate your RecyclerView. 使用ArrayList并填充RecyclerView。

This is a cleaner but a Complicated way to achieve what you are trying to achieve. 这是一种更干净复杂的方法,可以实现您要实现的目标。

  1. Single Vertical RecyclerView 单个立式回收机视图

    • Create a single layout with a Horizontal LinearLayout storing your Views. 使用水平LinearLayout创建单个布局来存储视图。

      Layout Structure would be like this (repeated 7 times) 布局结构将是这样(重复7次)

      LinearLayout(Horizontal) LinearLayout(水平)

      • LinearLayout(Vertical) LinearLayout(垂直)
      • 5 Textviews 5个文本视图
    • Populate your Recyclerview with a Arraylist. 用Arraylist填充Recyclerview。

Personally i would go for the first one, but you can choose anything that helps you. 就个人而言,我会去第一个,但您可以选择任何可以帮助您的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM