Android Open Source library providing an Hexagonal Grid for Android.
As HexGrid is developed on top of RecyclerView
in order to obtain a Hexagonal Grid, you only have to implement a normal RecyclerView using the custom elements provided (SpacesItemDecoration
, HexAdapter
and CustomItemClickListener
).
-
You have to create a layout file which contains the RecyclerView.
<android.support.v7.widget.RecyclerView android:id="@+id/tiles" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/sea"/>
-
Calculate
spacingInPixels
(separation between hexagons in a grid), create a newSpacesItemDecoration
with the spacing previously calculated and add it to the view.tilesView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
-
Instantiate a
LayoutManager
, establish the number of spans occupied by each item in the adapter (e.g. 1-2-1-...-1-2-1) and set the manager to the view.layoutManager = new GridLayoutManager(this, SPAN_COUNT, LinearLayoutManager.VERTICAL, false); layoutManager.setSpanSizeLookup(spanSeries); tilesView.setLayoutManager(layoutManager);
-
Instantiate a new
HexAdapter
with aCustomItemClickListener
and set the adapter to the view.tilesAdapter = new HexAdapter(tiles, onClickListener); tilesView.setAdapter(tilesAdapter);
If you are working with gradle, add the dependency to your build.gradle file:
dependencies{
compile 'com.pguardiola:hexgrid:0.0.4'
}
if you are working with maven, do it into your pom.xml
<dependency>
<groupId>com.pguardiola</groupId>
<artifactId>hexgrid</artifactId>
<version>0.0.4</version>
<type>aar</type>
</dependency>
- Calculate the correct spacing and fix
SpacesItemDecoration
to put the hexagon items closely (as a real grid) - Define custom attributes
- Support multiple hexagon custom views in
HexAdapter.ViewHolder
- Support background images in hexagon custom views
Feel free to report or add any useful feature to the library.
- Pablo Guardiola Sánchez - guardiola31337@gmail.com
Copyright 2016 Pablo Guardiola Sánchez.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.