Android Grid View 水平滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12576952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:04:21  来源:igfitidea点击:

Android Grid View Scroll Horizontally

androidgridviewhorizontal-scrolling

提问by wolverine

I am doing a task which retrieves images from server and displays in GridView in the application. This Grid view is scrolling up and down. But i want to scroll this view left to right as the menu screen scrolls. Is it possible with grid view? Or Is there any better way to do this? please help me in doing this.

我正在执行从服务器检索图像并在应用程序的 GridView 中显示的任务。此网格视图正在上下滚动。但我想在菜单屏幕滚动时从左到右滚动此视图。网格视图可以吗?或者有没有更好的方法来做到这一点?请帮我做这件事。

thanks in advance.

提前致谢。

回答by Anton I. Sipos

This isn't easily possible with the stock Android GridView. Try using this library: two-way-gridview

使用库存的 Android GridView 不容易做到这一点。尝试使用这个库: 双向网格视图

(I found this library in this other answer: Horizontal scrolling grid view)

(我在另一个答案中找到了这个库:水平滚动网格视图

回答by Evin1_

Is there any better way to do this?

有没有更好的方法来做到这一点?

Yes, there is.

是的,有。

You can achieve this in 3 lines using a RecyclerViewwith a horizontal GridLayoutManager:

您可以使用带有水平GridLayoutManagerRecyclerView在 3 行中实现此目的:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(new CustomAdapter(arrayList));

The RecyclerViewsupports applications built with the SDK 7 or larger.

RecyclerView与SDK 7以上的内置支持的应用程序。

If you want to make it even easier, take a look at the HorizontalGridViewclass if you are working with an application that is built for the API 17 or larger.

如果您想让它更容易,如果您正在使用为 API 17 或更大版本构建的应用程序,请查看Horizo​​ntalGridView类。



Here's a linkof an example of a simple RecyclerView Adapter.

是一个简单 RecyclerView 适配器示例的链接

回答by Harish

Try with below layout

尝试以下布局

 <HorizontalScrollView android="http://schemas.android.com/apk/res/android" 
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <GridView android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/gridview" android:columnWidth="50dp" android:padding="10dp" 
            android:horizontalSpacing="8dp" android:verticalSpacing="12dp" 
            android:numColumns="3" android:scrollbars="horizontal"/>
    </HorizontalScrollView>