Android GridView可以二维滚动

时间:2017-06-28 15:21:21

标签: java android gridview scroll

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

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

1 个答案:

答案 0 :(得分:0)

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

在此处找到参考资料:Diagonal scrolling

让我知道这是否有效。

相关问题