Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Views Between Items in GridView

I have a scrollable GridView where the items have a decent amount of empty space between them. for some of the objects in the grid, I'd like to add arrow images leading from one to the other

For reference to what I mean, think of the World of Warcraft talent trees (http://calculators.iradei.eu/talents/mage). You'll see some of the talents have arrows leading from one item to the next.

The layout I'm dealing with just contains a gridview in a framelayout, nothing else

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="4"
        android:scrollbars="none"
        android:id="@+id/talent_tree_grid_view">
    </GridView>

</FrameLayout>

I'm trying to add these ImageViews in code, as I don't think there is any way I could add this logic into the adapter for the GridView.

I've tried adding them as children of the parent FrameLayout, based on the x and y coords of the GridView items, and this works, but when scrolling the GridView, the arrows do not also scroll

I've also tried adding them directly as children of the GridView, but it does not support this behavior (it throws java.lang.UnsupportedOperationException)

Are there any ideas as to how I could add these imageviews between the gridview items?

like image 803
Dportology Avatar asked May 28 '17 21:05

Dportology


People also ask

How to use GridView layout?

Create an XML file for each grid item to be displayed in GridView. Click on the app > res > layout > Right-Click > Layout Resource file and then name the file as card_item.

What is difference between ListView and GridView?

The main difference between ListView and GridView is how it lays out its child. With ListView you are laying your children one by one either vertically or horizontally only. With GridView, its a combination of both. It lays its children horizontally first.

What is GridLayout in Android?

android.widget.GridLayout. A layout that places its children in a rectangular grid. The grid is composed of a set of infinitely thin lines that separate the viewing area into cells. Throughout the API, grid lines are referenced by grid indices.


1 Answers

This sounds very similar to Romain Guy's "Shelves" app where a custom GridView is used and the background image of a bookshelf scrolls. Take a look at the project here. Source for the custom GridView can be found in the file ShelvesView.java in the source code. A quick intro from Guy Romain is here;

For the arrows, you may want to consider a 9-patch drawable.


I have investigated the above approach a little more and have a sample app based upon Google's Hello GridView example.

Taking this example app, I have built a custom GridView (DogsGridView.java) and connected the photos across empty cells similar to your example of World of Warcraft talent trees. I just use simple lines, but the class can be modified to connect cells with arrows or another graphic.

Here is a quick video with connecting lines. Here is another with 9-patch arrows.

The project source can be found here.

like image 176
Cheticamp Avatar answered Sep 19 '22 08:09

Cheticamp