水平和垂直滚动的GridView的Android

问题描述 投票:0回答:1

我创建使用GridView控件创建如Excel表格中的组成部分。在这里,我起初并随后创建一个2x2的网格,当用户点击一个按钮,额外的行并添加列。

这是我做的添加新列: -

int numColumns = GVParticipantTable.getNumColumns() + 1;
                double numRows = Math.ceil((double) GVParticipantTable.getCount() / (double) GVParticipantTable.getNumColumns());
                int totalItems = (int) numRows * numColumns;

                if (participantDataList.size() > 4)
                {
                    totalItems = totalItems - participantDataList.size();
                }
                for (int i = 0; i < totalItems; i++)
                {
                    participantDataList.add("");

                }

                GVParticipantTable.setNumColumns(numColumns);
                mAdapterParticipantTable.notifyDataSetChanged();

这工作得很好,并增加了一个额外的列。问题是,当添加列,前面的专栏收缩,以适应新列使列每次添加新的列时看起来更小。我想在提前水平显示在屏幕上只有2列,后来让用户滚动查看更多的列。眼下电网只能垂直滚动。同样地,我希望这种情况发生时,添加新行,用户应该能够垂直滚动看到更多行。

android gridview scroll horizontal-scrolling vertical-scrolling
1个回答
0
投票

按照我知道你不应该使用一个Horizo​​ntalScrollView有一个ListView,因为ListView控件需要其自身滚动的照顾。最重要的是,这样做违背了所有的ListView重要的优化用于处理大名单,因为它有效地迫使ListView控件来显示两个项目的整个列表,以填补由Horizo​​ntalScrollView提供无限的容器。

http://developer.android.com/reference/android/widget/HorizontalScrollView.html

因为你可能会被迫使用二维滚动视图,您可以考虑使用此:blog.gorges.us/2010/06/android-two-dimensional-scrollview/的互联网档案

我没有使用过这一点,但它可能是一个合理的方法。

也许尝试通过在XML文件中加入这一点,并在你的JAVA

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ScrollView 
        android:id="@+id/scrollVertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        //WateverViewYouWant

    </ScrollView>
</HorizontalScrollView>

和代码的onCreate / onCreateView

final HorizontalScrollView hScroll = (HorizontalScrollView) value.findViewById(R.id.scrollHorizontal);
    final ScrollView vScroll = (ScrollView) value.findViewById(R.id.scrollVertical);
    vScroll.setOnTouchListener(new View.OnTouchListener() { //inner scroll listener         
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return false;
        }
    });
    hScroll.setOnTouchListener(new View.OnTouchListener() { //outer scroll listener         
        private float mx, my, curX, curY;
        private boolean started = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            curX = event.getX();
            curY = event.getY();
            int dx = (int) (mx - curX);
            int dy = (int) (my - curY);
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    if (started) {
                        vScroll.scrollBy(0, dy);
                        hScroll.scrollBy(dx, 0);
                    } else {
                        started = true;
                    }
                    mx = curX;
                    my = curY;
                    break;
                case MotionEvent.ACTION_UP: 
                    vScroll.scrollBy(0, dy);
                    hScroll.scrollBy(dx, 0);
                    started = false;
                    break;
            }
            return true;
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.