简体   繁体   中英

Grid View with Horizontal scrolling in android

I want to show images in a gridview and want to show them in a single row. No of columns in the gridview will be equal to no of images. Now what I want is when the no. of images are more and taking the width more than the screen size, I want to enable horizontal scrolling to the gridview, so that the images will be visible.

How can I achieve this, please help me if you have any idea. I have already tried Gallery widget but it is not giving me the desired results.

Here is a Library called Two Way Grid View You can use this GridView in Both Orientations (Horizontal and Vertical)

Link to the Library :
https://github.com/jess-anders/two-way-gridview

Sample Usage:

<?xml version="1.0" encoding="utf-8"?>
<com.jess.ui.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E8E8E8"
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:columnWidth="80dp"
app:rowHeight="80dp"
app:numColumns="auto_fit"
app:numRows="auto_fit"
app:verticalSpacing="16dp"
app:horizontalSpacing="16dp"
app:stretchMode="spacingWidthUniform"
app:scrollDirectionPortrait="vertical"
app:scrollDirectionLandscape="horizontal"
app:gravity="center"/>

I hope this solves your problem.

PS: You can also check out the sample provided with the Library for further usage demo.

You can achieve the functionality using RecyclerView with spanCount (number of rows in your horizontal case)

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, spancount, GridLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(gridLayoutManager);

If you need Staggered behavior the use StaggeredGridLayoutManager

GridLayoutManager StaggeredGridLayoutManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM