Skip to content

Guardiola31337/HexGrid

Repository files navigation

HexGrid

Build Status Maven Central Hex.pm Platform

Android Open Source library providing an Hexagonal Grid for Android.

Screenshots

Demo Screenshot

Usage

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).

  1. 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"/>
  2. Calculate spacingInPixels (separation between hexagons in a grid), create a new SpacesItemDecoration with the spacing previously calculated and add it to the view.

    tilesView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
  3. 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);
  4. Instantiate a new HexAdapter with a CustomItemClickListener and set the adapter to the view.

    tilesAdapter = new HexAdapter(tiles, onClickListener);
    tilesView.setAdapter(tilesAdapter);

Add it to your project

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>

Do you want to contribute? TODO

  • 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.

Developed By

Twitter Linkedin

License

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.

About

Hexagonal Grid for Android

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages