Question

I have XML:

   <RelativeLayout
        android:id="@+id/IcdSearchMainPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="UselessParent" >

            <own_components.SearchOutput
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top"
                android:orientation="vertical" >

                <!-- HERE ARE ADDED ROWS WITH RESULTS IN JAVA CODE (look into SearchOutput.java) -->

            </own_components.SearchOutput>
        </ScrollView>
    </RelativeLayout>

I have class SearchOutput.java (which extends LinearLayout) with such a method (generally it adds some graphical components to row and then this row to a ScrollView):

public void setResultOutput(ResultContainer result)
    {
        for (int i = 0; i < result.size(); i++)
        {
            LinearLayout resultRow = new LinearLayout(getContext());
            resultRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            resultRow.setOrientation(LinearLayout.HORIZONTAL);
            resultRow.setGravity(Gravity.CENTER_VERTICAL);
            if (i % result.getIterationStep() == 0)
            {
                resultRow.setPadding(0, 0, 0, 10);
            }
            addView(resultRow);

            ImageView langFlag = new ImageView(resultRow.getContext());
            langFlag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            try
            {
                Bitmap bitmap = BitmapFactory.decodeStream(MainClass.getAssetManager().open(Pathes.FLAGS_DIR + result.get(i)[0] + ".gif"));
                langFlag.setImageBitmap(bitmap);
                langFlag.setPadding(10, 0, 0, 0);
            }
            catch (IOException e)
            {
                Log.e("IMAGE_OPEN_EXC", e.toString());
            }
            resultRow.addView(langFlag);

            TextView number = new TextView(resultRow.getContext());
            number.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            number.setPadding(10, 0, 0, 0);
            number.setText(result.get(i)[1]);
            resultRow.addView(number);

            TextView text = new TextView(resultRow.getContext());
            text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            text.setPadding(20, 0, 0, 0);
            text.setText(result.get(i)[2]);
            resultRow.addView(text);
        }
    }

PROBLEM:
No matter how many elements are in result variable there is always one row showned:
enter image description here
QUESTION:
What i do wrong? In my opinion logically everything is all right.
Another question: is this way efficient? Maybe i should add results in different way? I ask because there can be plenty of them.

Was it helpful?

Solution

A ScrollView can have only one child, so wrap your layouts in another big layout which you add to your ScrollView.

But as suggested in the comments, use a ListView!!! That's what it's for...especially when you have many rows to display. ListView will recycle unused views and definitely yield a better performance than your approach.

OTHER TIPS

Scroll view is a container that make its own child scrollable, so, you will have to add a child to the scrollview, later add the items to that child. No master what is the child, it can be list view, can be linear layout, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top