BanzaiTokyo
BanzaiTokyo

Reputation: 1404

2 dimensional scroll for a grid in Android

I have tried making a (potentially infinite) grid to scroll without success so far. I have a trouble rendering a GridView that is out of the screen. That's why I would like to ask if there is any other component in android that could be used for this task?

grid to scroll in Android

Upvotes: 3

Views: 1670

Answers (3)

Gaket
Gaket

Reputation: 6839

Depending on your requirements, it looks like either a table-like RecyclerView with infinite scrolling either some 2d game engine (in case if it's worth to bring it for this feature) might be an optimal solution for you.

RecyclerView

In case if you just want to store some objects (graphical or not - does not really matter, if you can pack them to the cells), you can use some kind of RecyclerView adapter with an extension for boundless scrolling.

This way you can save some memory using the views recycling mechanism. Using the detailed notification mechanisms (like item moved/deleted) you even can add some animations if the objects are moving.

In addition to a proposed solution like evrencoskun/TableView, I would suggest to glance on how the way how GridLayoutManger works overall, here is the first post out of three in a relatively deep review of how the RecyclerView and its LayoutManagers work`, so you could tailor more lightweight solution for your particular case. There is a simple demo app, "Fixed two-way list" with "Large Grid" option may give you some idea of what will be built. You will need to extend it to work with infinite lists, but it should be simpler when you understand the internals.

2D engine

If the idea is to have some graphics over the tiles with arbitrary sizes and the feature is core for your app, it might be a good idea to consider using some 2d engine like libdx. They have many examples how to use it but of course it will cost you some storage and time to learn it: libgdx wike

Own solution

If neither of both suits your needs, I think, you will have to find your own solution from scratch. Don't forget to optimize it, as every infinite thing in Android, especially bound to graphics, may easily lead to Out of memory errors.

Upvotes: 4

May Rest in Peace
May Rest in Peace

Reputation: 2207

I would like to suggest this evrencoskun/TableView.

It allows you to scroll both horizontally and vertically.

<com.evrencoskun.tableview.TableView
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Upvotes: 2

Rahul
Rahul

Reputation: 1422

might this link will be helpful for you

https://github.com/jess-anders/two-way-gridview

<?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"/>

Upvotes: 0

Related Questions