Sunday, 30 May 2021

Converting TableLayout to RecyclerView

I'm trying to convert my TableLayout to a RecyclerView. The TableLayout is fine so far as it goes, but some of the tables have lots of rows, and are really slow to inflate, and I think that a RecyclerView would be the more efficient model to use (and would allow easy access to searching/filtering functionality).

The issue I'm having is that my TableLayout is defined as a set of custom views or compound controls, with a few standard views thrown in, like so:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    style="@style/MySection"
    android:id="@+id/section_cape" >

    <TableLayout style="@style/MyTable"
        android:id="@+id/table_cape" >

        <com.example.myapp.CompoundSwitch
            android:id="@+id/cape"
            custom:switch_label_tag="label_show"
            custom:switch_label_text="@string/label_show"
            custom:switch_indented="false"
            android:visibility="visible" />

        <TextView
            style="@style/MyTextView"
            android:id="@+id/info_capeNotAvailable"
            android:text="@string/info_notAvailable" />

        <com.example.myapp.CompoundSpinner
            android:id="@+id/capeProvider"
            custom:spinner_label_tag="label_variableProvider"
            custom:spinner_label_text="@string/label_provider"
            custom:spinner_indented="false"
            android:visibility="visible" />

        <com.example.myapp.CompoundSlider
            android:id="@+id/capeTransition"
            custom:slider_label_tag="label_providerTransition"
            custom:slider_label_text="@string/label_transition"
            custom:slider_indented="true"
            custom:slider_range="false"
            android:visibility="gone" />

        ... etc ...

Each of the custom views is defined as a TableRow.

I'm struggling to understand what to inflate in the onCreateViewHolder() method of my Adapter. Of course, each custom view does have a layout, but for some of these custom views, there are several layouts and a different layout is used depending on what custom attributes are defined in the xml.

So how do I map my rows of custom views (each with its own set of attributes) into the RecyclerView structure?

My current thinking is that I would have to move each row (custom view) statement into its own separate layout xml file, and then inflate the appropriate one of these in the onCreateViewHolder() method (based on viewType) but I'm not sure that this is the correct approach... and there would be a lot of very small layout files... seems unnecessarily unwieldy.



from Converting TableLayout to RecyclerView

No comments:

Post a Comment