Preview the image library in android GridView. Check the upper right corner of the Multi-choice mode.

Source: Internet
Author: User

You can see that there is a need for beginners to: Use the GridView to preview the image library. In the multi-choice mode, check the upper right corner. (When you select multiple preview charts in the 4.0 image library, a blue border is added, which is actually a blue background.) The GridView has never been used in actual development and you just want to implement it, write a demo for beginners. Come First:
It is not complicated to implement. There are only two files. First, let's take a look at the GridView inheritance relationship: java. lang. object upload android. view. view every android. view. viewGroup uses android. widget. adapterView <T extends android. widget. adapter> logging android. widget. absListView uses android. widget. gridView we know that ListView has the single-choice mode and multi-choice mode [AbsListView. setChoiceMode (AbsListView. CHOICE_MODE_SINGLE/AbsListView. CHOICE_MODE_MULTIPLE_MODAL)] The GridView also supports multiple selection modes. Don't talk nonsense. I believe you know it too! Paste the code. main layout file: main. xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <GridView android: id = "@ + id/gridview" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: columnWidth = "75dip" android: gravity = "center" android: horizontalSpacing = "2dip" android: numColumns = "4" android: verticalSpacing = "2dip"/> </ LinearLayout> main Activity: HomeActivity [java] package com. xyz. gridview; import java. util. hashMap; import java. util. map; import java. util. set; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. actionMode; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view Group; import android. widget. absListView; import android. widget. absListView. layoutParams; import android. widget. absListView. multiChoiceModeListener; import android. widget. baseAdapter; import android. widget. checkable; import android. widget. frameLayout; import android. widget. gridView; import android. widget. imageView; import android. widget. listAdapter; import android. widget. textView; public class Hom EActivity extends Activity implements MultiChoiceModeListener {private GridView mGridView; private GridAdapter mGridAdapter; private TextView mActionText; private static final int MENU_SELECT_ALL = 0; private static final int completion = MENU_SELECT_ALL + 1; private Map <Integer, Boolean> mSelectMap = new HashMap <Integer, Boolean> (); private int [] mImgIds = new int [] {R. drawable. img_1, R. Drawable. img_2, R. drawable. img_3, R. drawable. img_4, R. drawable. img_5, R. drawable. img_6, R. drawable. img_7, R. drawable. img_8, R. drawable. img_9, R. drawable. img_1, R. drawable. img_2, R. drawable. img_3, R. drawable. img_4, R. drawable. img_5, R. drawable. img_6, R. drawable. img_7};/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (saved InstanceState); setContentView (R. layout. main); mGridView = (GridView) findViewById (R. id. gridview); mGridView. setChoiceMode (GridView. CHOICE_MODE_MULTIPLE_MODAL); mGridAdapter = new GridAdapter (this); mGridView. setAdapter (mGridAdapter); mGridView. setMultiChoiceModeListener (this);}/** Override MultiChoiceModeListener start **/@ Override public boolean onCreateActionMode (ActionMode mode, Menu menu) {// TODO Auto-generated method stub View v = LayoutInflater. from (this ). inflate (R. layout. actionbar_layout, null); www.2cto.com mActionText = (TextView) v. findViewById (R. id. action_text); mActionText. setText (formatString (mGridView. getCheckedItemCount (); mode. setCustomView (v); getMenuInflater (). inflate (R. menu. action_menu, menu); return true;} @ Override public boolean onPrepareActionMode (ActionMode Mode, Menu menu) {// TODO Auto-generated method stub menu. getItem (MENU_SELECT_ALL). setEnabled (mGridView. getCheckedItemCount ()! = MGridView. getCount (); return true ;}@ Override public boolean onActionItemClicked (ActionMode mode, MenuItem item) {// TODO Auto-generated method stub switch (item. getItemId () {case R. id. menu_select: for (int I = 0; I <mGridView. getCount (); I ++) {mGridView. setItemChecked (I, true); mSelectMap. put (I, true);} break; case R. id. menu_unselect: for (int I = 0; I <mGridView. getCount (); I ++) {mGri DView. setItemChecked (I, false); mSelectMap. clear ();} break;} return true;} @ Override public void onDestroyActionMode (ActionMode mode) {// TODO Auto-generated method stub mGridAdapter. policydatasetchanged () ;}@ Override public void onItemCheckedStateChanged (ActionMode mode, int position, long id, boolean checked) {// TODO Auto-generated method stub mActionText. setText (formatString (mGridView. ge TCheckedItemCount (); mSelectMap. put (position, checked); mode. invalidate ();}/** Override MultiChoiceModeListener end **/private String formatString (int count) {return String. format (getString (R. string. selection), count);} private class GridAdapter extends BaseAdapter {private Context mContext; public GridAdapter (Context ctx) {mContext = ctx ;}@ Override public int getCount () {// TODO Auto- Generated method stub return mImgIds. length ;}@ Override public Integer getItem (int position) {// TODO Auto-generated method stub return Integer. valueOf (mImgIds [position]);} @ Override public long getItemId (int position) {// TODO Auto-generated method stub return position;} @ Override public View getView (int position, view convertView, ViewGroup parent) {// TODO Auto-generated method stub GridI Tem item; if (convertView = null) {item = new GridItem (mContext); item. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT);} else {item = (GridItem) convertView;} item. setImgResId (getItem (position); item. setChecked (mSelectMap. get (position) = null? False: mSelectMap. get (position); return item ;}} GridItem is a self-encapsulated class: GridItem. java [java] package com. xyz. gridview; import android. content. context; import android. util. attributeSet; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. widget. checkable; import android. widget. imageView; import android. widget. relativeLayout; public class GridIte M extends RelativeLayout implements Checkable {private Context mContext; private boolean mChecked; private ImageView mImgView = null; private ImageView mSecletView = null; public GridItem (Context context) {this (context, null, 0);} public GridItem (Context context, AttributeSet attrs) {this (context, attrs, 0);} public GridItem (Context context Context, AttributeSet attrs, int defStyle) {super (context, Attrs, defStyle); // TODO Auto-generated constructor stub mContext = context; LayoutInflater. from (mContext ). inflate (R. layout. grid_item, this); mImgView = (ImageView) findViewById (R. id. img_view); mSecletView = (ImageView) findViewById (R. id. select) ;}@ Override public void setChecked (boolean checked) {// TODO Auto-generated method stub mChecked = checked; setBackgroundDrawable (checked? GetResources (). getDrawable (R. drawable. background): null); mSecletView. setVisibility (checked? View. VISIBLE: View. GONE) ;}@ Override public boolean isChecked () {// TODO Auto-generated method stub return mChecked ;}@ Override public void toggle () {// TODO Auto-generated method stub setChecked (! MChecked);} public void setImgResId (int resId) {if (mImgView! = Null) {mImgView. setBackgroundResource (resId) ;}} a layout referenced by this class: grid_item.xml [html] <? 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"> <ImageView android: id = "@ + id/img_view" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: scaleType = "fitXY"/> <ImageView android: id = "@ + id/select" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: Allow = "true" android: background = "@ drawable/icon_choice" android: visibility = "gone"/> </RelativeLayout>

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.