Is it good or bad practice to create alternate styles of rows(eg:alternate background color) in ListView using separate xml?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/355072

  •  17-01-2021
  •  | 
  •  

Question

For example,to create alternate layout in ListView which uses different alternate color :

<LinearLayout
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textview_publish_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </TextView>
</LinearLayout>

I can set the color at the getView method:

MyActivity.this.findViewById(R.id.view1).setBackgroundColor(position%2=0?Color.RED.Color.BLUE); 

But I would rather copy and place same layout file:

row_style1.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textview_publish_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </TextView>
</LinearLayout>

row_style2.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#0000FF"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textview_publish_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </TextView>
</LinearLayout>

and then inflate different layouts at the getView method:

view=LayoutInflater.from(MyFragment.this.getActivity()).inflate(i%2==0?R.layout.row_style1:R.layout.row_style2,null);

I think it is more maintainable because it separate more style concerns from code, especially when there is more different styles between different parts, eg:

findViewById(R.id.view1).setBackgroundColor(position%2=0?Color.RED.Color.BLUE);
findViewById(R.id.view2).setBackgroundColor(position%2=0?Color.YELLOW.Color.GREEN);  

which can be simplified by inflating different layouts. Is it good or bad practice to do this?

Était-ce utile?

La solution

Generally speaking, if it's reasonable to expect that the overall structure of each row is going to remain the same in both cases, then by introducing repetition (two separate, but similar, layout files), you also introduce a dependency between the two (you need to remember to change both files every time). If you consider just the layout files by themselves, that's less maintainable. But you may be willing to pay that cost if it gets you some other, sufficiently valuable design benefit. (As a side note: When it comes to good/bad practices, try to understand the "why" behind them; once you do that, you can decide if they make sense for your particular case. Depending on your experience, you may not always be able to do that, but it's a good idea to try.)

You could also use a single layout file, that leaves android:background undefined. Then you'd define the two colors as resources (in res/values/colors.xml), and set them from code, by either getting preexisting views as before, or by inflating the single layout, and then setting the background color on the resulting view. This way you avoid having two codependent layout files, and you avoid hardcoding the background colors.

Another approach would be (I should mention) to create a custom component, where you'd have full control, but that's probably not worth the effort in this case.

Licencié sous: CC-BY-SA avec attribution
scroll top