繁体   English   中英

Android GridView可以二维滚动

[英]Android GridView scrollable in two dimensions

我想制作一个由按钮组成的棋盘游戏,它们之间没有间距,棋盘必须同时在2个维度上滚动。 当我试图制作嵌套容器时,我可以滚动,例如verticaly但水平然后被锁定。

  1. 我怎么做可滚动板?
  2. 如何完全删除按钮之间的间距?

要实现两种滚动行为,您可以在下面实现此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;
    }

在此处供参考: 对角滚动

让我知道这个是否奏效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM