(Android) supports single-or multi-choice in the gridview

Source: Internet
Author: User

Recently, when I was working on a project, I had to use the gridview and single-choice or multi-choice functions. I searched a lot of resources online, but none of them were my desired results, so I designed a solution on my own. Do not say anything, first ^ _ ^: Demo source code download: http://download.csdn.net/down/3435752/anndaming

First, you must construct an imageadapter class that inherits the baseadapter class and is used as an adapter for resources in the gridview. The Code is as follows:

Package COM. demo; import Java. util. vector; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable; import android. graphics. drawable. layerdrawable; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. gridview; imp ORT android. widget. imageview; public class imageadapter extends baseadapter {private context mcontext; // defines context private vector <integer> mimageids = new vector <integer> (); // define a vector as the image source private vector <Boolean> mimage_bs = new vector <Boolean> (); // define a vector as the selected container private int lastposition =-1; // record the last selected image position.-1 indicates that no image is selected. Private Boolean multichoose; // indicates whether the current adapter allows multiple public imageadapter (context c, Boole An ismulti) {mcontext = C; multichoose = ismulti; // load the resource mimageids. add (R. drawable. item1); mimageids. add (R. drawable. item2); mimageids. add (R. drawable. item3); mimageids. add (R. drawable. item4); mimageids. add (R. drawable. item5); For (INT I = 0; I <5; I ++) mimage_bs.add (false) ;}@ overridepublic int getcount () {// todo auto-generated method stubreturn mimageids. size () ;}@ overridepublic object getitem (INT posi Tion) {// todo auto-generated method stubreturn position;} @ overridepublic long getitemid (INT position) {// todo auto-generated method stub return position ;} @ overridepublic view getview (INT position, view convertview, viewgroup parent) {// todo auto-generated method stubimageview imageview; If (convertview = NULL) {imageview = new imageview (mcontext); // set the imageview resource for imageview. setlayoutparams (ne W gridview. layoutparams (50, 50); // sets the layout image imageview. setscaletype (imageview. scaletype. fit_center); // set the display ratio type} else {imageview = (imageview) convertview;} imageview. setimagedrawable (makebmp (mimageids. elementat (position), mimage_bs.elementat (position); Return imageview;} private layerdrawable makebmp (int id, Boolean ischosen) {bitmap mainbmp = (bitmapdrawable) mcontext. getresources (). getdrawable (ID )). getbitmap (); // select bitmap seletedbmp Based on ischosen; If (ischosen = true) seletedbmp = bitmapfactory. decoderesource (mcontext. getresources (), R. drawable. btncheck_yes); else seletedbmp = bitmapfactory. decoderesource (mcontext. getresources (), R. drawable. btncheck_no); // generate a stacked graph drawable [] array = new drawable [2]; array [0] = new bitmapdrawable (mainbmp); array [1] = new bitmapdrawable (seletedbmp ); la Yerdrawable LA = new layerdrawable (array); La. setlayerinset (0, 0, 0, 0, 0); La. setlayerinset (1, 0,-5, 60, 45); return la; // return the superimposed graph} // modify the selected state public void changestate (INT position) {// If (multichoose = true) {mimage_bs.setelementat (! Mimage_bs.elementat (position), position); // directly reverse} // else {If (lastposition! =-1) mimage_bs.setelementat (false, lastposition); // cancel the last selected status mimage_bs.setelementat (! Mimage_bs.elementat (position), position); // you can reverse it to lastposition = position; // record the selected position} notifydatasetchanged (); // notify the adapter to update }}

Next, configure the main. xml file:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <linearlayout Android: orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center_horizontal"> <textview Android: text = "Single-choice function" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: textsize = "40dp" Android: gravity = "center"/> <gridview Android: id = "@ + ID/gridview_radio" Android: layout_width = "250dp" Android: layout_height = "150dp" Android: numcolumns = "4" Android: padding = "10dp" Android: horizontalspacing = "4dp" Android: verticalspacing = "4dp" Android: gravity = "center"/> </linearlayout> <linearlayout Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center_horizontal"> <textview Android: text = "multiple choice function" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: textsize = "40dp" Android: gravity = "center"/> <gridview Android: Id = "@ + ID/gridview_check" Android: layout_width = "250dp" Android: layout_height = "150dp" Android: numcolumns = "4" Android: padding = "10dp" Android: horizontalspacing = "4dp" Android: verticalspacing = "4dp" Android: gravity = "center"/> </linearlayout>

Finally, write an activity for testing:

Package COM. demo; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterview; import android. widget. gridview; import android. widget. adapterview. onitemclicklistener; public class demoactivity extends activity {gridview gridview_radio; // One-choice lattice gridview gridview_check; // multiple choice lattice imageadapter ia_radio; // The adapter that stores the image source (single choice) imageadapter ia_check; // adapter for storing image sources (multiple choice)/** call Ed when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // The grid gridview_radio = (gridview) findviewbyid (R. id. gridview_radio); ia_radio = new imageadapter (this, false); gridview_radio.setadapter (ia_radio); // set click to listen to gridview_radio.setonitemclicklistener (New onitemclicklistener () {@ over Ridepublic void onitemclick (adapterview <?> Arg0, view arg1, int position, long arg3) {// todo auto-generated method stubia_radio.changestate (position) ;}}); // multiple-choice grid gridview_check = (gridview) findviewbyid (R. id. gridview_check); ia_check = new imageadapter (this, true); gridview_check.setadapter (ia_check); // set the listener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Arg0, view arg1, int position, long arg3) {// todo auto-generated method stubia_check.changestate (position );}});}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.