When using the GridView, you sometimes need to select multiple classes shown above, such as deleting the pictures shown above in batches and uploading images in batches. In this case, we can use stacked graphs to achieve the following results:
The Code is as follows:
Main. xml
<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
<? 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. java
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
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 ();
}
}