如何在 Android 中创建垂直画廊?

发布于 2024-10-10 18:42:21 字数 78 浏览 2 评论 0原文

我想创建一个行为类似于图库小部件但垂直滚动而不是水平滚动的小部件。也就是说图库中的图像应该垂直放置在屏幕上并且可以垂直滚动。有人可以帮助我吗?

I want to create a widget that behaves similar to the gallery widget but scrolls vertically instead of horizontally. That is images in gallery should be vertically placed on screen and can be scroll vertically.Does anybody help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-10-17 18:42:21

假设您想要一列图像并且想要垂直滚动。检查以下示例

   <GridView android:id="@+id/gridView1" 
    android:layout_width="70dp"  android:layout_height="fill_parent"
    android:verticalSpacing="2dp"   android:numColumns="1" 
    android:stretchMode="columnWidth" android:layout_marginLeft="1dp"
    android:layout_alignParentTop="true"  android:scrollingCache="true"
    android:scrollbars="vertical" android:fadeScrollbars="false"
    android:scrollbarAlwaysDrawVerticalTrack="true">
</GridView>

Assume that you want to have a single column of images and want to scroll vertically. Check the following example

   <GridView android:id="@+id/gridView1" 
    android:layout_width="70dp"  android:layout_height="fill_parent"
    android:verticalSpacing="2dp"   android:numColumns="1" 
    android:stretchMode="columnWidth" android:layout_marginLeft="1dp"
    android:layout_alignParentTop="true"  android:scrollingCache="true"
    android:scrollbars="vertical" android:fadeScrollbars="false"
    android:scrollbarAlwaysDrawVerticalTrack="true">
</GridView>
執念 2024-10-17 18:42:21

我设法使用 ListView 创建一个简单的解决方案,并在滚动停止时将其滚动到最近的位置。只需创建一个 ListView 并添加此 OnScrollListener

编辑: 我已将代码更新为更好的实现

lv.setOnScrollListener(new OnScrollListener(){

        private boolean handleScrollChange = true;

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
          }

       public void onScrollStateChanged(final AbsListView view, int scrollState) {
            if (!handleScrollChange)
                return;
            if(scrollState == OnScrollListener.SCROLL_STATE_IDLE){
                View centerView;
                if (view.getLastVisiblePosition() - view.getFirstVisiblePosition() > 1) { //if shows more than 2 items, display the middle one
                    centerView = view.getChildAt( Math.round((view.getChildCount())/2));
                }
                else { //shows 2 items, check which one is closer to the middle
                    View bottomview = view.getChildAt(view.getChildCount()-1); 
                    if (bottomview.getTop() < bottomview.getHeight() / 2) {
                        centerView = bottomview;
                    }
                    else {
                        centerView = view.getChildAt(0);
                    }
                }

                int wantedOffset = (view.getHeight() - centerView.getHeight()) / 2 ;
                final int scrollby = (int) centerView.getY() - wantedOffset;
                //we want to prevent the smoothScroll from calling this function again
                handleScrollChange = false;

                view.post(new Runnable() {
                    @Override
                    public void run() {
                        view.smoothScrollBy(scrollby,300);
                        view.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                handleScrollChange = true;
                            }
                        }, 1000);
                    }
                });
            }
          }
        });

I've managed to create a simple solution using a ListView, and scrolling it to the closest position when the scroll is stopped. Simply create a ListView and add this OnScrollListener:

EDIT: I've updated the code to a better implementation

lv.setOnScrollListener(new OnScrollListener(){

        private boolean handleScrollChange = true;

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
          }

       public void onScrollStateChanged(final AbsListView view, int scrollState) {
            if (!handleScrollChange)
                return;
            if(scrollState == OnScrollListener.SCROLL_STATE_IDLE){
                View centerView;
                if (view.getLastVisiblePosition() - view.getFirstVisiblePosition() > 1) { //if shows more than 2 items, display the middle one
                    centerView = view.getChildAt( Math.round((view.getChildCount())/2));
                }
                else { //shows 2 items, check which one is closer to the middle
                    View bottomview = view.getChildAt(view.getChildCount()-1); 
                    if (bottomview.getTop() < bottomview.getHeight() / 2) {
                        centerView = bottomview;
                    }
                    else {
                        centerView = view.getChildAt(0);
                    }
                }

                int wantedOffset = (view.getHeight() - centerView.getHeight()) / 2 ;
                final int scrollby = (int) centerView.getY() - wantedOffset;
                //we want to prevent the smoothScroll from calling this function again
                handleScrollChange = false;

                view.post(new Runnable() {
                    @Override
                    public void run() {
                        view.smoothScrollBy(scrollby,300);
                        view.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                handleScrollChange = true;
                            }
                        }, 1000);
                    }
                });
            }
          }
        });
倾`听者〃 2024-10-17 18:42:21

您必须扩展 Gallery 类,并在 Draw 过程中将画布旋转 90 度。然后只需要进行一些修改,例如修改 onTouch 事件等等。此后,布局将会出现一些问题(因为它仍然想在其参数中绘制布局)。所以我把它放在 LinearLayout 中并固定其中的布局大小。
所以最终的垂直画廊实际上是一个线性布局,里面有一个画廊。我已经实现了,效果非常好。您只需将放入其中的所有内容向另一个方向旋转 90 度即可。权衡确实很小,因此您可以扩展要放入其中的每个视图,然后在绘制过程中将其旋转到另一个方向。

You have to extend the Gallery class and in the Draw procedure rotate the canvas for 90 degrees. Then just a few adoptions like modifying the onTouch event and a few more is required. After this there will be a few problems with the layout (since it still wants to draw in the layout in its parameters). So I put it inside a LinearLayout and fixed the layout size in that.
So the final vertical gallery is actually a linear layout which has a gallery put inside it. I have implemented it, and it works quite well. You will only need to rotate everything you put in it for 90 degrees to the other direction. The trade off is really a little thus you can extend every view you want to put inside it and just rotate it to the other direction in the draw procedure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文