title: Solution to build dynamic forms with Android
body: <p>I am actually developing an Android application on which I should display dynamic forms based on metadata contained inside JSON documents. Basically the way it works (without the details) is that a JSON document represent the structure of a form:</p>
  
  <pre><code>{
      "fields": [
          {
              "name": "fieldA",
              "type": "STRING",
              "minCharacters": 10,
              "maxCharacters": 100
          },
          {
              "name": "fieldB",
              "type": "INTEGER",
              "min": 10,
              "max": 100
          },
          {
              "name": "fieldC",
              "type": "BOOLEAN_CHECKBOX",
              "defaultValue": true
          }
          ...
      ],
      "name": "Form A"
  }
  </code></pre>
  
  <p>What I'm doing actually when the application receive one of those JSON documents is that it loop through each fields and parse it to the appropriate view (EditText, Checkbox, custom view, etc.), adding a tag to the view (to be able to retrieve it easily) and add the view to a <code>LinearLayout</code>. Here is a pseudo-code of how it is working actually:</p>
  
  <pre><code>//Add form title
  linearLayout.addView(new TextView(form.name));
  //Add form fields
  for(Field field: form.fields) {
      View view;
      switch(field.type){
          case STRING: view = new EditText();
          ...
      }
      view.setTag(field.id);
      linearLayout.addView(view);
  }
  </code></pre>
  
  <p>The issue with this is that with large forms (like >20 fields), it need to inflate lot of views and the UI thread suffer a lot. Another point to take into account is that a single screen may have multiple forms (one after another vertically sorted).</p>
  
  <p>To avoid overloading the UI thread I thought of 2 possible solutions:</p>
  
  <ol>
  <li><strong>Using a RecyclerView</strong>.</li>
  <li><strong>Using <a href="https://fblitho.com/" rel="noreferrer">Litho by Facebook</a></strong>.</li>
  </ol>
  
  <p>But multiple questions comes to me when considering these 2 solutions:</p>
  
  <ol>
  <li>Is it a good use case to use Litho? Or using a RecyclerView is enough?</li>
  <li>What about the state of my views? If I use a Recycling pattern, would I be able to keep the state of each of my fields (even those off-screen) and so being able to save the form without losing data?</li>
  <li>If I use a Recycling pattern to display one form, how would I handle multiple forms? Can we have nested RecyclerView? Forms need to be displayed one after another like inside a vertical RV but if forms themselves are RV, how should I handle this?</li>
  </ol>
  
  <p>This is more a "good practice" question and giving the right way or one of the right way of achieving my goal than a need of a specific answer with code example, etc.</p>
  
  <p>Thank's in advance for your time.</p>