Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom container (hexagonal) withe adapter

I want to create a custom android container. Where I can easily add and remove objects. The container should place objects inside hexagons. The order in which objects places is really important and shown in the image below. Objects in this adapter are click-able ImageViews (circular). It is even possible to make something like this for Android?

Hexagon-Circle

I know that there were similar questions like mine, but there are not even close to what I want to achieve.

Probably more and more people where looking for more custom containers like that one I'm trying to make. Not the standard one like in other apps: GridsView, ListView, etc.

What I've already made

I decided to use RecyclerView and custom RecyclerView.LayoutManagers. Also write an algorithm to define a position of ImageViews. Unfortunately I'am not familiar with LayoutManager and not sure how should I define places using it Interface.

RecyclerView

Here is algorithm:

List<Object> list; 
int nuberOfElements = list.size();
int layerNr = 0;
int radius = 0;
int angle = 0;

//handle first middel element postion(0,0)
nuberOfPlaceElements --;
radius += r;

for(layerNr=1; nuberOfElements > 0; layerNr ++){
    for(int elementInLayer = 0; elementInLayer < layerNr * 6; elemnetInLayer ++){ 
        //layerNr *6 -> define how many elements in layer

        angle += 360/layerNr * 6
        //handle the postion of elemnts in Layer

        nuberOfElements--;
    }
    radius += r;
    angle = 0;
}

Hexagon-Order

like image 252
Minis Avatar asked Feb 11 '16 11:02

Minis


2 Answers

best solution is to create a custom layout (http://lucasr.org/2014/05/12/custom-layouts-on-android/) but that's also the most expensive way (cost on time to implement)...

alternative you can create a custom view and draw images directly there (http://developer.android.com/training/custom-views/custom-drawing.html)

all you ever want to know about hex-maps: http://www.redblobgames.com/grids/hexagons/

why is layout better than a custom drawing view? it can be packed in a library and used for any other application whereas a custom drawing view is rather bound to the application...

like image 65
Martin Frank Avatar answered Sep 27 '22 20:09

Martin Frank


you can check this library which does exactly what you are asking for https://github.com/xresco/Hexagon-Recyclerview

It's super simple to use. Instead of using the default recyclerview (or listview) just use

<com.abed.hexagonrecyclerview.view.HexagonRecyclerView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/rvItems" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_centerVertical="true" app:items_count_in_row="3" app:items_horizontal_spacing="20dp" app:items_vertical_spacing="20dp" app:orientation="horizontal" />

you can customize it using the following four parameters:

app:items_count_in_row app:items_horizontal_spacing app:items_vertical_spacing app:orientation

like image 35
Abed Almoradi Avatar answered Sep 27 '22 20:09

Abed Almoradi