【问题标题】:GridView inside NestedScrollViewNestedScrollView 内的 GridView
【发布时间】:2023-03-19 20:30:01
【问题描述】:

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

【问题讨论】:

  • 您要解决的具体问题是什么?
  • 当我滚动 gridView 以查看底部的点时,nestedscrollview 也在滚动,我如何在滚动 gridview 时修复或禁用滚动。
  • 使用NestedScrollView scrollview = (NestedScrollView) findViewById(R.id.nestedScrollId); 检索nestedScrollView 然后通过调用scrollview.setNestedScrollingEnabled(boolean enabled) 禁用它

标签: android gridview scroll android-nestedscrollview


【解决方案1】:

这个解决方案对我有用。

    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)

【讨论】:

  • 行 = (int) (x + 1);下面有 1 个额外的空行。相反 rows = (int) x;对我来说工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 2017-02-15
  • 2017-05-28
  • 1970-01-01
  • 2020-03-29
  • 2019-03-21
相关资源
最近更新 更多