GithubHelp home page GithubHelp logo

linving / androidgridviewcompatlib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paramvir-b/androidgridviewcompatlib

0.0 1.0 0.0 486 KB

Multi-select grid view for android api level 4 and above

Java 100.00%

androidgridviewcompatlib's Introduction

AndroidGridViewCompatLib

Multi-select grid view for android API level 7 and above. (NOTE: if you are running against API level 4 use tag v1.0.1

Motivation

Working on one project I need to use the multi-selection GridView. I looked into the android docs and I found it. I tried few hours with it and realized that the documentation is wrong and all multi-selection related apis came after API level 10 to GridView (even though docs says API level 1). Initially these apis are part of ListView and later after API level 10 they are moved to AbsListView. There is an issue also opened for the same.

So I sat down and wrote this class to support GridView with multi-selection for even older platforms.

Where to get

And obviously you can clone, from above mentioned tags.

Usage for project

  • One way to use is to have your project use it as a library. (Pls refer referencing library)
  • Second way is to copy the GridViewCompat file to which ever package you want to and change the package of the class file to your package.

Usage inside project

Since ADT 21 complains if you are compiling against higher API level and using apis available only in API level above your minSdkVersion, we can make GridViewCompat a drop-in replacement for GridView. So no more 'C' suffixed apis. Just change the import packages and you are good. But to support this your project needs to be compiled against API 11 or higher. Also need ADT 21 and above version.

Example

There is a sample project also which can be helpful.

layout.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/doneButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:enabled="false"
        android:text="Done" />

    <com.rokoder.android.lib.support.v4.widget.GridViewCompat
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/doneButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:choiceMode="multipleChoice"
        android:columnWidth="120dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </com.rokoder.android.lib.support.v4.widget.GridViewCompat>

</RelativeLayout>

YourActivity.java

@Override
protected void onCreate(Bundle saveInstance) {
    ...
    ...
    gridView = (GridViewCompat) findViewById(R.id.gridView);

    // NOTE: We are using setChoiceMode, as I said, its a drop-in replacement
    gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    gridView.setAdapter(imageAdapter);
    ...
    ...
}

Benefits

  • Needless to say, you will get multi-selection.
  • Only one class file, so make it easy to integrate with projects.
  • Thanks to ADT 21 its now drop in replacement for GridView. NOTE: you need to compile your project against API 11 or higher. If you want reflection based solution which does not depend upon the API level try tag 'v1.0.0'. Also needs ADT 21 and above.
  • If the apis are available it will shift to native ones. So compatibility mode is only when needed.
  • It does not require any compatibility jars also.

Caveats

  • As of now, the only way you can setChoiceMode is from the code. XML attribute is NOT supported.
  • Does NOT support CHOICE_MODE_MULTIPLE_MODAL for setChoiceMode. Which should not be a problem as such.
  • If you see lint errors in your code, you might need to use @TargetAPi to suppress it. Example: Lint Error - Call requires API level 11 (current min is 4): android.widget.GridView#getCheckedItemPositions Try (As we are using in the sample app):
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public View getView(int position, View convertView, ViewGroup parent) {
    ...
    ...
}

androidgridviewcompatlib's People

Contributors

paramvir-b 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.