android中ScrollView里面GridView的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21264951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:15:17  来源:igfitidea点击:

Problems with GridView inside ScrollView in android

androidgridview

提问by Igor Ronner

I'm trying I put the GridView inside ScrollView in android. When I put the GridView not work.

我正在尝试将 GridView 放在 android 中的 ScrollView 中。当我把 GridView 不工作时。

Here is layout.

这里是布局。

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

        <RelativeLayout 
            android:id="@+id/layout_home" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">       

              <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/programacao_grid"
                android:numColumns="auto_fit"
                android:gravity="center"
                android:columnWidth="50dp"
                android:stretchMode="columnWidth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/calendario_programacao"
             >       
            </GridView> 

    </RelativeLayout>

</ScrollView>

After many search a found the answer and it is below

经过多次搜索找到了答案,它在下面

回答by Igor Ronner

After search i found this project link:-

搜索后我找到了这个项目链接:-

ExpandableHeightGridViewclass

ExpandableHeightGridView班级

package xx.xxx.xx.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;

public class ExpandableHeightGridView extends GridView {

boolean expanded = false;

public ExpandableHeightGridView(Context context)
{
    super(context);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs,
        int defStyle)
{
    super(context, attrs, defStyle);
}

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
} }

layout.xmlfile:

layout.xml文件:

<ScrollView
    android:id="@+id/sc_spots"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >


        <xx.xxx.xx.view.ExpandableHeightGridView
            android:id="@+id/spotsView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:horizontalSpacing="10dp"
            android:isScrollContainer="false"
            android:numColumns="5"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />
 </ScrollView>

Using GridViewclass

使用GridView

mGridView = (ExpandableHeightGridView)
getView().findViewById(R.id.spotsView);
mGridView.setExpanded(true);
SpotsAdapter adapter = new SpotsAdapter(getActivity(),R.layout.spot_item,params);
mGridView.setAdapter(adapter);
adapter.notifyDataSetChanged();

回答by Tharindu Bandara

This works for me

这对我有用

   // Setting on Touch Listener for handling the touch inside ScrollView

       gridView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        return false;
                    }

                });

回答by Stephen Liu

Just to mention that, the solution provided by @Igor Ronner is great, but still did a half part in my app for unknown reason -- within a scroll view, the grid view is sitting flat like it does not exist at all.

顺便提一下,@Igor Ronner 提供的解决方案很棒,但由于未知原因,我的应用程序仍然占了一半——在滚动视图中,网格视图平躺着,就像它根本不存在一样。

It took me almost half a day with numerous' attempts to fill the other half -- use a relative layout to wrap to grid view to let it auto grow, like this (the actual code that finally works for me!!!):

我花了将近半天的时间来填充另一半 - 使用相对布局包装到网格视图让它自动增长,就像这样(最终对我有用的实际代码!!!):

<RelativeLayout
    android:id="@+id/grid_view_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/text_catalog"
    >

    <com.takken.app.android.component.framework.ExpandableGridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:horizontalSpacing="12dp"
        android:verticalSpacing="12dp"
        />

</RelativeLayout>