Question

I want to add a LinearLayout to ScrollView where the children of LinearLayout are a bunch of GridLayouts containing 4 columns. The items in the GridLayout is dynamically populated from the code. The GridLayout can have either 2 or 1 rows depending upon the number of content set in MainRows class.

Now, since there can be many sets of GridLayout, so a ScrollView is required. The problem is, ScrollView only scrolls 2 rows of Grids. This 2 rows is in one set of MainRows, if that makes sense. I want to scroll all the Grids contained in the layout.

Here is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:id="@+id/parent">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/topEight"
            android:layout_marginTop="5dp">
        </TextView>
    </LinearLayout>
</ScrollView>

single_video_grid.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview0"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:numColumns="4">
</GridView>

And from the code I am dynamically adding and setting views like this:

            ArrayList<MainRows> rows = JSONVideoDataHandler.getRowElements();
            for (MainRows row : rows) {
                LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent);
                if(row instanceof Ads){
                    // TODO implement ads layout
                }else{
                    int numberOfItems = row.getNumberOfItems();
                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    if(numberOfItems > 4){
                        GridView gridview = (GridView) inflater.inflate(R.layout.single_video_grid, null);
                        // if number of vids exceeds 4, 2 rows are required.
                        // therefore setting 8 imageplaceholders.
                        gridview.setAdapter(new ImageAdapter(this, row, 8));
                        gridview.setOnItemClickListener(new OnItemClickListener() {
                            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                                Toast.makeText(activity, "haha " + position,
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                        parentLayout.addView(gridview);
                    }else{
                        GridView gridview = (GridView) inflater.inflate(R.layout.single_video_grid, null);
                        // if number of vids does not exceed 4, 1 row is required.
                        // therefore setting 4 imageplaceholders.
                        gridview.setAdapter(new ImageAdapter(this, row, 4));
                        gridview.setOnItemClickListener(new OnItemClickListener() {
                            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                                Toast.makeText(activity, "haha " + position,
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
//                      linearLayout = (LinearLayout) findViewById(R.id.parent);
                        parentLayout.addView(gridview);
                    }
                }

Let me know if the question is not clear enough. I would appreciate any kind of help. Thank you!

Was it helpful?

Solution

I don't see why you exclusively have to use Grid Layouts. Why not do like this, just make sure framelayout's width is screenWidth / 4.

<scrollview>
 <linearlayout> // vertical
  for i < categories {
   <linerlayout> // horizontal
    for j < videos {
     <framelayout>
      // the content you want to show
     </framelayout>
    }
   </linearlayout>
  }
 </linearlayout>
</scrollview>

OTHER TIPS

If I am understanding correctly, youare running into issues because a GridView inherently has a scroll view within itself. So putting a GridView inside a ScrollView really nullifies the purpose of the SrollView as the GridView's scrolling behavior trumps. Take a look at this question and answer as it addresses this very problem. How to put GridView inside ScrollView

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