【问题标题】:Problems with GridView inside ScrollView in androidandroid中ScrollView中的GridView问题
【发布时间】:2014-02-11 10:47:38
【问题描述】:

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

这是布局。

<?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>

经过多次搜索找到了答案

【问题讨论】:

  • 将gridview移出scrollview
  • 但我需要 GridView 留在 ScrollView 内

标签: android gridview


【解决方案1】:

搜索后发现这个项目link:-

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.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>

使用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();

【讨论】:

  • 我有同样的用例,这个解决方案效果很好。
  • @Igor Ronner 来自上面的回答,SpotsAdapter,R.layout.spot_item,params 代表什么。?
  • 老兄的答案很好:)
  • 是现在滚动还是占据整个高度?
  • 谢谢!像魅力一样工作
【解决方案2】:

这对我有用

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

                });

【讨论】:

    【解决方案3】:

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

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

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多