ScrollView不适用于整个布局

时间:2013-02-11 03:28:26

标签: android android-linearlayout android-xml android-scrollview android-gridlayout

我想向LinearLayout添加ScrollView,其中LinearLayout的孩子是一群GridLayouts,包含4列。 GridLayout中的项目是从代码中动态填充的。 GridLayout可以有2行或1行,具体取决于MainRows类中设置的内容数量。

现在,由于可能有多套GridLayout,因此需要ScrollView。问题是,ScrollView仅滚动2行网格。这两行是在一组MainRows中,如果这是有道理的。我想滚动布局中包含的所有网格。

这是我的 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>

从代码我动态添加和设置这样的视图:

            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);
                    }
                }

如果问题不够明确,请告诉我。我将不胜感激任何帮助。 谢谢!

2 个答案:

答案 0 :(得分:1)

如果我理解正确,你会遇到问题,因为GridView本身就有一个滚动视图。因此,将GridView放在ScrollView中实际上会使SrollView的目的无效,因为GridView的滚动行为胜过。看看这个问题和答案,因为它解决了这个问题。 How to put GridView inside ScrollView

答案 1 :(得分:1)

我不明白为什么你只能使用网格布局。为什么不这样做,只要确保framelayout的宽度是screenWidth / 4。

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