Android GridView可以在两个方向上滚动

3

我希望制作一款游戏,其中棋盘由没有间隔的按钮组成,并且棋盘必须能够在两个方向上同时滚动。当我尝试创建嵌套容器时,例如垂直滚动是可以的,但是水平滚动就会被锁定。

  1. 我该如何实现可滚动的棋盘?
  2. 如何完全消除按钮之间的间隔?

这仍然不起作用。我可以垂直或水平绘制,但不能同时在任何方向上进行。我想要的效果就像缩放图像并移动它一样。还有,如何将AppCompatButton调整为正方形? - bolex5
已更新答案,我没有意识到你想要自由移动的东西。请查看更新后的答案是否满足您的需求。 - A. Petrizza
最终我成功创建了外观符合要求的GridView和按钮,但是当我尝试添加自定义滚动视图时,每行只能看到最后一个按钮。我试着扩展ScrollView和GridView,但是没有用。我该如何将它们组合起来?https://ibb.co/cNr405 - bolex5
而 MotionEvent.ACTION_DOWN 没有被调用。 - bolex5
但我避免了那个。http://www.wklej.org/id/3211983/ - bolex5
显示剩余4条评论
1个回答

1
为了实现两种滚动行为,您可以实现以下XML:
现在,这是使用滚动视图作为父布局来实现双向滚动。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical">

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320px" android:layout_height="fill_parent">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linlay" android:layout_width="320px"
        android:layout_height="fill_parent" android:stretchColumns="1"
        android:background="#000000"/>

</HorizontalScrollView>

然后要启用水平滚动条,请使用以下内容:
android:scrollbarAlwaysDrawHorizontalTrack="true"

关于按钮上没有间距的问题,您可以通过确保它们与邻居之间没有填充或边距来轻松实现此目标。只需根据需要调整其大小,以确保它们在所需设计中适合屏幕。要使用GridView,您可以像这样做:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/schemeGridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:numColumns="1" >
    </GridView>
</LinearLayout>

</HorizontalScrollView>

为了解决对角线滚动的问题,我认为您需要处理实际的触摸事件来启动滚动。
尝试这样做:
@Override
    public boolean onTouchEvent(MotionEvent event) {
        float curX, curY;

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                mx = event.getX();
                my = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                curX = event.getX();
                curY = event.getY();
                vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                mx = curX;
                my = curY;
                break;
            case MotionEvent.ACTION_UP:
                curX = event.getX();
                curY = event.getY();
                vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                break;
        }

        return true;
    }

这里提供参考链接:对角线滚动

如果可以,请告诉我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接