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?
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;
}
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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With