2016-06-24 10 views
0

Ich entwickle eine App, die die Wettervorhersage von 4 verschiedenen Regionen in 5 Tagen in der Zukunft zeigt. Jede Region zeigt 5 Tage an und jeder Tag hat seine eigenen Daten: ein TextView mit dem Namen des Tages, ein TextView mit der Hauptwetterbedingung für diesen Tag, ein ImageView mit dem Wettersymbol und zwei TextView mit dem Min Temp und Max Temp für diesen Tag. Wie folgt aus:Mehrmals dieselbe Ansicht hinzufügen - Layout erneut verwenden

enter image description here

ich realisiert dies in einer SUPER AWFUL layout.xml Datei, jede Textview-Behandlung und Image für alle 20 Tage der 4 Regionen getrennt:

<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> 

Auf der Java Datei Ich habe 100 Deklarationen von Ansichten und verwende 100 Zeilen, um die Instanz der Ansichten zu erhalten, und weitere 100 Setzmethoden, um Texte und Bilder zu setzen ... Ich weiß, super/unglaublich schrecklich.

Dies muss sicher schlechtes Design sein. Es muss eine Möglichkeit geben, eine einzelne Ansicht (Tag-Name, Bedingung, Bild, minimale Temperatur und maximale Temperatur) in ein Layout einzufügen und wiederzuverwenden. Irgendeine Idee?

+1

Verwenden Sie '' Tag – pskink

Antwort

1

Sie könnten eine benutzerdefinierte Ansicht erstellen, die all Ihre Präsentationslogik kapselt und fügen Sie einfach eine Anzahl von diesen in Ihrem LinearLayout hinzu.

Sie vermeiden die XML-Code-Duplizierung und Sie werden in der Lage sein, ein einzelnes, gut definiertes Widget, das Ihren Anforderungen entspricht. Außerdem wird dadurch der Java-Code reduziert, den Sie benötigen, um Ihre Daten an die vorhandenen Ansichten zu binden.

1

Angenommen, die Daten stammen von einer Art Wetter-Api oder Ihrer eigenen Datenbank, die hier implementiert wurde.

Ich habe 2 Lösungen diesen

  1. Nested RecyclerView zu erreichen: Horizontal RecyclerView innerhalb einer Vertikal RecyclerView

    • Sie

      enter image description here

      ein horizontales vertikales Layout würden

      Dieses Layout würde 7 Mal für jeden Tag wiederholt werden.

    • Your Vertical RecyclerView würde eine Sammlung solcher Horizontal RecyclerView (für Regionen)

    • ein Arraylist verwenden und Ihre RecyclerView Bestücken.

Dies ist ein Reiniger aber ein Komplizierte Art und Weise zu erreichen, was Sie erreichen wollen.

  1. Einzel Vertikal RecyclerView

    • ein einzelnes Layout erstellen mit einem horizontalen Linearlayout Ihre Ansichten zu speichern.

      Layout-Struktur würde wie folgt sein (7-mal wiederholt)

      Linearlayout (Horizontal)

      • Linearlayout (vertikal)
      • 5 Textviews
    • Bestücken Sie Ihre Recyclerview mit einem Arraylist.

Persönlich würde ich für die erste gehen, aber Sie können alles wählen, das Ihnen hilft.

Verwandte Themen