0

Ich bin dynamisch Erstellen von Tabellenzeilen und Textansichten von meinen JSON-Daten, die ich von einer API bekomme. Momentan werden die Daten korrekt angezeigt, aber irgendwie funktioniert meine Scrollview nicht. Es scrollt nicht. Ich habe eine ScrollView hinzugefügt, aber ich vermute, dass ich dort etwas falsch mache.Android Scrollview funktioniert nicht auf dynamischen Inhalt

Mein Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     android:orientation="vertical" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <TableLayout 
       android:id="@+id/tlMarksTable" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

      </TableLayout> 

     </LinearLayout> 

    </ScrollView> 

</RelativeLayout> 

Und der Teil, in dem ich die Ansichten erzeugen sieht wie folgt aus:

jsonArray = new JSONArray(result); 

TableRow tableRow = new TableRow(context); 
setContentView(tableRow); 
tableRow.setOrientation(LinearLayout.VERTICAL); 

for(int i= 0; i < jsonArray.length(); i++) { 
    JSONObject jsonobject = jsonArray.getJSONObject(i); 
    String id   = jsonobject.getString("id"); 
    String content = jsonobject.getString("content"); 

    TextView textView = new TextView(context); 
    textView.setText(content); 
    if (Build.VERSION.SDK_INT < 23) { 
     textView.setTextAppearance(getApplicationContext(), R.style.boldText); 
    } else { 
     textView.setTextAppearance(R.style.boldText); 
    } 
    textView.setBackgroundResource(R.color.highlightedTextViewColor); 
    textView.setPadding(10, 10, 10, 10); 
    textView.setTextSize(getResources().getDimension(R.dimen.joke_content_font_size)); 
    tableRow.addView(textView); 
} 
+0

Warum verwenden Sie nicht einfach eine Gridview? – tyczj

+0

@tyczj Tut mir leid, ich habe nicht viel Erfahrung in diesem, könnten Sie weiter erklären, was Sie meinen? – utdev

+0

https://developer.android.com/guide/topics/ui/layout/gridview.html – tyczj

Antwort

0

haben Ihre innere Komponente wie folgt konfiguriert:

  <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <TableLayout 
       android:id="@+id/tlMarksTable" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

      </TableLayout> 

     </LinearLayout> 

durch die Art, wie die äußere ViewGroup nicht viel macht

+0

Hallo danke für die Antwort, ich entfernte das äußere 'RelativeLayout' und ersetzte das XML mit Ihrem zur Verfügung gestellten, aber ich kann es immer noch nicht Scroll noch irgendwelche anderen Ideen? – utdev

+0

ist ScrollView Ihre Top ViewGroup? – Pooya

+0

ja ScrollView ist meine oberste ViewGroup – utdev

0

Das Hauptproblem, das ich hier sehe, ist, dass die LinearLayout, die Ihre TableLayout einkapselt, android:layout_height="match_parent" hat. Dies bedeutet, dass das innere Kind des ScrollView dieselbe Höhe wie das Elternelement hat, was zu einem Layout ohne Bildlauf führt.

Warum versuchen Sie nicht, das LinearLayout zu entfernen, und die TableLayout als das einzige Kind zu verlassen? Denken Sie daran, die Höhe dieses untergeordneten Elements auf android:layout_height="wrap_content" festzulegen.

Übrigens, ich empfehle die Verwendung einer ListView für die Anzeige großer Datenmengen.

Eine letzte Sache: Es ist nicht notwendig, den XML-Namespace (xmlns:android="http://schemas.android.com/apk/res/android") in jeder Layout-Deklaration zu deklarieren. Nur mit einer Deklaration am Wurzelelement wird es gut gehen;)

0

Warum verwenden Sie keine Recyclerview? Sie sollten Retrofit verwenden, um WS zu verbrauchen

+0

Was meinst du mit WS? – utdev

+0

Web-Service, Daten von der API – Woz

Verwandte Themen