3

如何在 Harmony OS(JAVA) 中实现 GridView 项目的页面滑动?这是我的代码示例。

public class GridView extends TableLayout {
    public GridView(Context context) {
        super(context);
    }

    public GridView(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public GridView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    void setAdapter(GridAdapter adapter, LongClickedListener longClickedListener) {
        removeAllComponents();
        for (int i = 0; i < adapter.getComponentList().size(); i++) {
            adapter.getComponentList().get(i).setLongClickedListener(longClickedListener);
            addComponent(adapter.getComponentList().get(i));
        }
    }
}
4

1 回答 1

0

可以在 GridView 外面包裹一个 ScrollView,将 ScrollView 的宽高设置为固定值或者match_parent实现滑动效果。

下面以GridView继承的TableLayout为例:

<ScrollView
    ohos:height="match_parent"
    ohos:width="match_parent">

    <TableLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:background_element="#87CEEB"
        ohos:padding="8vp"
        >
     ...
     </<TableLayout>
<ScrollView>

如果问题仍然存在,请发布 GridAdapter 代码供我们检查。

于 2021-10-20T02:50:35.360 回答