java - Android GridView 可二维滚动

标签 java android gridview scroll

我想制作一个由按钮组成的棋盘的游戏,按钮之间没有间距,并且棋盘必须同时在二维上滚动。当我尝试制作嵌套容器时,我可以滚动,例如垂直滚动,但水平滚动会被锁定。

  1. 如何制作可滚动板?
  2. 如何完全消除按钮之间的间距?

最佳答案

要实现这两种滚动行为,您可以实现以下 XML:

现在使用 ScrollView 作为父布局以实现双向滚动。

<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;
    }

在这里找到供引用:Diagonal scrolling

请告诉我这是否有效。

关于java - Android GridView 可二维滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44806658/

相关文章:

java - 循环和概率

java - Kotlin/Java 是否使用缓存系统来提高迭代数组时的性能?

java - MongoDB 的 Map Reduce OutputType 之间的差异

android - 在 Android 上使用 Facebook API 创建自定义墙贴

Android - 在 gridview 中的特定图像上覆盖小复选图标或更改边框

java - 如何在 freebase 中搜索建筑物附近的地点?

java - Android抽屉导航点击事件问题

android - 已安装应用程序但图标未出现在启动器屏幕或应用程序列表中

android - onRotate getactivity 为空

WPF 从 GridView 的选择/鼠标上移除光泽效果