GithubHelp home page GithubHelp logo

irmobydick / fluidgridadapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from moshdev/fluidgridadapter

0.0 1.0 0.0 6.64 MB

An Android listview adapter that arranges images in a fluid grid.

License: Apache License 2.0

Java 100.00%

fluidgridadapter's Introduction

FluidGridAdapter

A listview adapter that arranges images in a fluid grid. Callbacks for loading the image into the view and for when a single cell is tapped. Allows for custom cell padding, row height, and cell background.

Display a grid of photos Adjust padding and row height Set the cell background color

Example Usage

Create a FluidGridAdapter by passing in an array of objects that have:

  1. an image url/tag/identifier
  2. an aspect ratio OR width & height

Here's using an example using a library such as Picasso

////////////////////////////////////////////////////////////////////////////////
//Query the MediaStore for device photos and build an array of ImageData objects
////////////////////////////////////////////////////////////////////////////////
String[] projection = { MediaStore.Images.Thumbnails._ID,
    MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.HEIGHT,
    MediaStore.Images.Thumbnails.WIDTH };

Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
    projection, null, null, null);
ArrayList imageDatas = new ArrayList();
int photoHeightIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.HEIGHT);
int photoWidthIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.WIDTH);
int fileLocationIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
while(cursor.moveToNext()) {
  int photoHeight = cursor.getInt(photoHeightIndex);
  int photoWidth = cursor.getInt(photoWidthIndex);
  String fileLocation = cursor.getString(fileLocationIndex);

  if(photoWidth > 0 && photoHeight > 0) {
    ImageData imageData = new ImageData(fileLocation, photoWidth, photoHeight);
    imageDatas.add(imageData);
  }
}

/////////////////////////////////////////////////////////
//Pass the array of ImageData objects into the adapter
/////////////////////////////////////////////////////////
FluidGridAdapter fluidGridAdapter = new FluidGridAdapter(this, imageDatas){
  @Override
  protected void onSingleCellTapped(ImageData imageData) {
    Log.d(TAG,"Single cell tapped");
  }

  @Override
  protected void loadImageIntoView(String photoUrl, int cellWidth, int cellHeight, ImageView imageHolder) { 
    Picasso.with(context).load(photoUrl).resize(cellWidth, cellHeight).into(imageHolder);
  }
};
listview.setAdapter(fluidGridAdapter);

Compatibility

Compatible and tested with Android 4.0 and above.

fluidgridadapter's People

Contributors

taylorhughes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.