Example code of android GridView multi-Choice

Source: Internet
Author: User

The Code is as follows:

Main. xml

Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Xmlns: tools = "http://schemas.android.com/tools"

Android: background = "#000000"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Tools: context = ". MainActivity">

<GridView

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: id = "@ + id/grid"

Android: verticalSpacing = "3dp"

Android: horizontalSpacing = "3dp"

Android: numColumns = "3"

> </GridView>

</LinearLayout>

Item. xml

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"

Android: orientation = "vertical">

<ImageView

Android: scaleType = "fitXY"

Android: padding = "3dp"

Android: layout_height = "70dp"

Android: layout_width = "70dp"

Android: id = "@ + id/image_item"

/>

</LinearLayout>

MainActivity. javaCopy codeThe Code is as follows: package com. imageview;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. GridView;
Import android. widget. AdapterView. OnItemClickListener;

Public class MainActivity extends Activity {
Private Adpter adpter;
Private GridView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Int [] image = {R. drawable. dog1, R. drawable. m2, R. drawable. m3, R. drawable. m4, R. drawable. m5, R. drawable. dog2 };
Adpter = new Adpter (image, this );
GridView = (GridView) findViewById (R. id. grid );
GridView. setAdapter (adpter );
GridView. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1,
Int position, long arg3 ){
Adpter. chiceState (position );
}
});
}
}

Adpter. java

Copy codeThe Code is as follows: package com. imageview;

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. util. Log;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. ImageView;

Public class Adpter extends BaseAdapter {
Private int [] image;
Private boolean isChice [];
Private Context context;

Public Adpter (int [] im, Context context ){
This. image = im;
Log. I ("hck", im. length + "lenght ");
IsChice = new boolean [im. length];
For (int I = 0; I <im. length; I ++ ){
IsChice [I] = false;
}
This. context = context;
}

@ Override
Public int getCount (){
Return image. length;
}

@ Override
Public Object getItem (int arg0 ){
Return image [arg0];
}

@ Override
Public long getItemId (int arg0 ){
Return arg0;
}

@ Override
Public View getView (int arg0, View arg1, ViewGroup arg2 ){
View view = arg1;
GetView getView = null;
If (view = null ){
View = LayoutInflater. from (context). inflate (R. layout. item, null );
GetView = new GetView ();
GetView. imageView = (ImageView) view. findViewById (R. id. image_item );
View. setTag (getView );
} Else {
GetView = (GetView) view. getTag ();
}
GetView. imageView. setImageDrawable (getView (arg0 ));

Return view;
}

Static class GetView {
ImageView imageView;
}

// The following code is used.
Private LayerDrawable getView (int post ){

Bitmap bitmap = (BitmapDrawable) context. getResources (). getDrawable (image [post]). getBitmap ();
Bitmap bitmap2 = null;
LayerDrawable la = null;
If (isChice [post] = true ){
Bitmap2 = BitmapFactory. decodeResource (context. getResources (),
R. drawable. editable_mode_checked_tag );
}
If (bitmap2! = Null ){
Drawable [] array = new Drawable [2];
Array [0] = new BitmapDrawable (bitmap );
Array [1] = new BitmapDrawable (bitmap2 );
La = new LayerDrawable (array );
La. setLayerInset (0, 0, 0, 0, 0); // The distance between the edges of the first graph.
La. setLayerInset (1, 0, 65, 65, 0 );
}
Else {
Drawable [] array = new Drawable [1];
Array [0] = new BitmapDrawable (bitmap );
La = new LayerDrawable (array );
La. setLayerInset (0, 0, 0, 0, 0 );
}
Return la; // return the superimposed graph.
}
Public void chiceState (int post)
{
IsChice [post] = isChice [post] = true? False: true;
This. notifyDataSetChanged ();
}
}

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.