Custom antenna container (hexagonal) with adapter

I want to create a custom container for Android. Where I can easily add and remove objects. The container must place objects inside the hexagons. The order in which the locations of the objects are really important is shown in the image below. Objects in this adapter are clickable ImageViews (circular). Can you even do something similar for Android?

Hexagon-circle

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

Probably more and more people are looking for additional custom containers like the one I'm trying to make. Not standard, as in other applications: GridsView, ListView, etc.

What i have already done

I decided to use RecyclerView and custom RecyclerView.LayoutManagers. Also write an ImageViews positioning algorithm. Unfortunately, I am not familiar with LayoutManager and don’t know how I can determine places using the interface.

Recyclerview

Here is the 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

+5
source share
2 answers

The best solution is to create a custom layout ( http://lucasr.org/2014/05/12/custom-layouts-on-android/ ), but this is also the most expensive way (execution cost) ...

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

all you need to know about hex cards: http://www.redblobgames.com/grids/hexagons/

Why is a layout better than a custom drawing view? it can be packaged into a library and used for any other application, while the custom drawing view is more likely tied to the application ...

+1
source

you can check out this library that does exactly what you ask for https://github.com/xresco/Hexagon-Recyclerview

It is very easy 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 configure it using the following four options:

Application: items_count_in_row Application: items_horizontal_spacing Application: items_vertical_spacing Application: orientation

+1
source

Source: https://habr.com/ru/post/1242768/


All Articles