android - Optimal way to handle a large list of Textviews -
i have make in xml:
at moment have this:
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include layout="@layout/view_general_properties" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="15dp" /> <include layout="@layout/view_property_interior_properties" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="15dp" /> </linearlayout> </scrollview>
this view_general_properties:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingleft="12dp" android:paddingright="12dp"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:singleline="true" android:text="@string/general_properties" android:textallcaps="true" android:textsize="@dimen/title_fontsize" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" android:text="@string/total_living_area" /> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:text="220 m²" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" android:text="@string/number_of_fronts" /> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:text="1" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" android:text="@string/number_of_accommodation_units" /> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:text="2" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" android:text="@string/number_of_floors" /> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:text="4" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" android:text="@string/condition_of_the_building" /> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:text="uitstekend" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" android:text="@string/frontage_street" /> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:text="6m" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" android:text="@string/year" /> <textview style="@style/property_detail_label_style" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:text="1902" /> </linearlayout> </linearlayout>
this works fine, since have lot of duplicate code wondering if there better solution scenario.
thanks!
there many ways in can improved.
trivially, common style attributes each textview
can pulled out style. see developer docs styles , themes.
i'd go further, , create separate layout resource file 1 single line of content, containing 2 textview
s. leave out text values, assign id
values each textview
.
my_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview style="@style/property_detail_label_style" android:id="@+id/label" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.66" /> <textview style="@style/property_detail_label_style" android:id="@+id/value" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" /> </linearlayout>
then, in code, manually inflate 1 of these each data item, , add list dynamically.
view item; ( /* each data item in list */ ) { item = inflater.inflate(r.layout.my_list_item, containerview, true); textview label = (textview) item.findviewbyid(r.id.label); textview value = (textview) item.findviewbyid(r.id.value); label.settext(dataitem.label); value.settext(dataitem.value); }
where containerview
outer linearlayout
holds list.
Comments
Post a Comment