【问题标题】:ScrollView is not applied to whole layoutScrollView 不适用于整个布局
【发布时间】:2013-02-11 03:28:26
【问题描述】:

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

现在,由于GridLayout 可以有很多组,所以需要ScrollView。问题是,ScrollView 只能滚动 2 行网格。如果有意义的话,这 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);
                    }
                }

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

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

    • 这是一个不错且简单的想法。但我真的很想看看为什么我的 GridLayout 不起作用。感谢您指出这一点。
    • @siz 解释了原因。如果您非常想使用 GridLayouts,请强制它们停止滚动。在您的网格视图上尝试this
    • 嗯,没错。我想我必须改变我的布局。再次感谢!
    【解决方案2】:

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

    【讨论】:

    • 嗯我猜这就是为什么GridView 的两行滚动,而不是整个布局。更清楚地说,我想要这样的结构: 如果我们有很多网格视图就像我在上面展示的那样,我希望它全部滚动而不是滚动 gridview 本身的行。
    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多