title: Scrollview and OnClick handlers not working when view is cleared and re-created
body: <p>I'm having an odd problem and I am not sure where the issue is.</p>
  
  <p>I have an XML file which contains two linear layouts, only one is visible at a time depending on what setting has been used. Both views are dynamically generated and set an on click listener and onlongclicklistener. </p>
  
  <p>Below is the XML layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"&gt;
      &lt;LinearLayout android:id="@+id/resultTableContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:visibility="gone"&gt;
          &lt;ScrollView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:scrollbars="horizontal"
              android:fillViewport="true"&gt;
              &lt;HorizontalScrollView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:fillViewport="true"&gt;
                  &lt;TableLayout android:id="@+id/resultTable"
                      android:stretchColumns="1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:shrinkColumns="false"&gt;
                  &lt;/TableLayout&gt;
              &lt;/HorizontalScrollView&gt;
          &lt;/ScrollView&gt;
      &lt;/LinearLayout&gt;
      &lt;LinearLayout android:id="@+id/formattedResultContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:visibility="gone"&gt;
          &lt;ScrollView android:id="@+id/formattedScrollContainer"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:scrollbars="vertical"&gt;
              &lt;LinearLayout android:id="@+id/formattedResult"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"/&gt;
          &lt;/ScrollView&gt;
      &lt;/LinearLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>For simplicity I'll call the first linear layout container view 1 and the second linear layout container view 2. </p>
  
  <p>Both views have their visibility set to gone by default, and when the screen first loads, view 1's visibility is set to visible and the view is dynamically generated using the following code: </p>
  
  <pre><code>@Override
      public void processResult(final View.OnClickListener editRowClickListener, final View.OnLongClickListener columnLongClickListener)
      {
          try
          {
              this.getActivity().runOnUiThread(new Runnable()
              {
                  @Override
                  public void run()
                  {
                      if (shouldExistingResultBeCleared())
                      {
                          resultView.removeAllViews();
                      }
                      if (getSettings().getInt(Defines.SharedPreferenceSettings.APPLICATION_THEME, com.BoardiesITSolutions.Library.R.style.LibAppTheme)
                              == com.BoardiesITSolutions.Library.R.style.LibAppTheme)
                      {
                          resultView.setBackgroundColor(Color.WHITE);
                      }
                      else
                      {
                          resultView.setBackgroundColor(Color.BLACK);
                      }
                  }
              });
  
              this.showDialog(getJSONResult().length());
  
              new Thread(new Runnable()
              {
                  @Override
                  public void run()
                  {
                      try
                      {
                          for (int i = 0; i &lt; getJSONResult().length(); i++)
                          {
                              HashMap&lt;Integer, String&gt; fieldIndexAndValue = new HashMap&lt;Integer, String&gt;();
                              final TableRow tr;
                              if (!getSettings().getBoolean("IsDarkTheme", false))
                              {
                                  tr = (TableRow) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.result_table_row_light_theme, resultView, false);
                              } else
                              {
                                  tr = (TableRow) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.result_table_row_dark_theme, resultView, false);
                              }
                              if (i == 0)
                              {
                                  tr.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.appPrimaryColour));
                              }
                              else if (i % 2 == 0)
                              {
                                  if (!getSettings().getBoolean("IsDarkTheme", false))
                                  {
                                      tr.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.resultRowLightThemeAlternateRow));
                                  } else
                                  {
                                      tr.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.resultRowDarkThemeAlternateRow));
                                  }
                              }
                              ImageButton imageButton = (ImageButton) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.row_edit_image, tr, false);
                              imageButton.setOnClickListener(editRowClickListener);
                              tr.addView(imageButton);
  
                              if (i == 0)
                              {
                                  imageButton.setVisibility(View.INVISIBLE);
                              }
  
                              JSONArray array = getJSONResult().getJSONArray(i);
                              String currentField = null;
                              for (int j = 0; j &lt; array.length(); j++)
                              {
                                  final TextView textView;
                                  textView = (TextView) getFragment().getLayoutInflater(getFragment().getArguments()).inflate(R.layout.result_textview, tr, false);
                                  textView.setText(array.getString(j));
                                  if (!getSettings().getBoolean(Defines.SharedPreferenceSettings.MULTILINE_RESULT, true))
                                  {
                                      textView.setSingleLine(true);
                                  }
                                  if (i == 0)
                                  {
                                      textView.setTypeface(null, Typeface.BOLD);
                                      //Get the fields into a index and field hash map
                                      addIndexAndField(j, array.getString(j));
                                  } else
                                  {
                                      textView.setOnLongClickListener(columnLongClickListener);
                                      fieldIndexAndValue.put(j, array.getString(j));
                                  }
                                  getActivity().runOnUiThread(new Runnable()
                                  {
  
                                      @Override
                                      public void run()
                                      {
                                          tr.addView(textView);
                                      }
                                  });
                              }
                              imageButton.setTag(fieldIndexAndValue);
  
                              getActivity().runOnUiThread(new Runnable()
                              {
  
                                  @Override
                                  public void run()
                                  {
                                      resultView.addView(tr);
                                  }
                              });
                              updateProgressDialog();
                              //handler.sendMessage(handler.obtainMessage());
                          }
                          closeDialog();
                          //((ResultProcessor)resultProcessor).closeDialog();
                      }
                      catch (JSONException ex)
                      {
  
                      }
                  }
              }).start();
          }
          catch (Exception ex)
          {
  
          }
      }
  </code></pre>
  
  <p>This works perfectly fine. The user presses an option to switch to view 2, so view 1 is cleared, visibility set to gone ans view 2 is set to visible. This view is then dynamically generated and the on click listeners are set to the view accordingly. (I haven't included the code for this view as I don't believe it is related and this view always works without exception). </p>
  
  <p>If the user then switches back to view 1, view 2 is cleared, and visibility set to gone and view 1 is back to being visible and the same method, as shown above, is used to re-create the view dynamically and assign the on click listeners. </p>
  
  <p>The view itself looks fine, however, there is no user interaction possible, the scroll view no longer scrolls vertically or horizontally and neither of the click handlers works. </p>
  
  <p>As view 1 work once, I don't understand why it won't when it is re-generated, but view 2 always works no matter what. </p>