NestedScrollView中的GridView

时间:2017-06-09 13:42:52

标签: android gridview scroll android-nestedscrollview

我正在尝试构建一个Android应用程序,我在NestedScrollView中有一个GridView,我的问题是gridview没有显示所有元素,我解决了向grivView添加滚动的问题。但现在我有了另一个问题是当我有时滚动nestedScrollView移动时,有时是gridView。enter image description here

1 个答案:

答案 0 :(得分:2)

这个解决方案让我感到高兴。

    public void setGridViewHeightBasedOnChildren(GridView gridView, int columns) {
        ListAdapter listAdapter = gridView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int items = listAdapter.getCount();
        int rows = 0;

        View listItem = listAdapter.getView(0, null, gridView);
        listItem.measure(0, 0);
        totalHeight = listItem.getMeasuredHeight();

        float x = 1;
        if( items > columns ){
            x = items/columns;
            rows = (int) (x + 1);
            totalHeight *= rows;
        }

        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight;
        gridView.setLayoutParams(params);

}

在gridview上调用setAdapter后,只需调用

即可

setGridViewHeightBasedOnChildren(your girdview object, no of grid view columns)