Android GridView Item Selectors change position and duplicate -


i have gridview layout 3 columns , several rows contains pictures taken facebook. when select image, selector appears item in gridview normal. (i don't use android selector custom layout linked below.)

the problem this: if select 1 or more items, when scroll down screen see more pictures discover other items have been selected! moreover, if scroll see items i've selected, selectors change position. seems me index of each selector update every time scroll screen.

i created personal imageadapter inflate pictures facebook. here getview:

public view getview(final int position, view convertview, viewgroup parent) {          view v;         imageview imageview = null;         if(convertview==null){             layoutinflater li = getlayoutinflater();             v = li.inflate(r.layout.photos_item, null);         }else{             v = convertview;         }              imageview = (imageview)v.findviewbyid(r.id.image);         v.settag(imageview);         if (arrayphotos.get(position).getpicturesource() != null){             log.i(tag, "position in gridview = " + integer.valueof(position).tostring());             imageloader.displayimage(arrayphotos.get(position).getpicturesource(), imageview);         }         return v;      } 

here onitemclick:

public void onitemclick(adapterview<?> parent, view view, int position, long id) {  view checkeditem;      checkeditem = (view) view.findviewbyid(r.id.check);      fbpicture = arrayfbpictures.get(position);     log.i(tag, "position = " + arrayfbpictures.indexof(fbpicture));     log.i(tag, "picid = " + fbpicture.getpictureid());      if( fbpicture.ischoosed() ){          log.i(tag, "checkicon disactivated");         fbpicture.setchoosed(false);         checkeditem.setvisibility(view.invisible);         parent.invalidate();     }     else{          log.i(tag, "checkicon activated");         fbpicture.setchoosed(true);         checkeditem.setvisibility(view.visible);         parent.invalidate();     } } 

that xml of gridview:

<gridview     android:id="@+id/gridview"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_gravity="center"     android:gravity="center"     android:numcolumns="auto_fit"      android:columnwidth="96dp"     android:stretchmode="spacingwidth"     android:horizontalspacing="8dp"     android:verticalspacing="8dp"     android:paddingtop="8dp"     android:paddingleft="8dp"     android:paddingright="8dp" > </gridview> 

and xml of custom selector:

<relativelayout    android:id="@+id/check"    android:layout_width="96dp"    android:layout_height="96dp"    android:background="@color/black_trasparency_light"    android:visibility="invisible" >       <imageview         android:id="@+id/image_check"         android:layout_width="48dp"         android:layout_height="48dp"         android:layout_alignparentright="true"         android:layout_alignparentbottom="true"         android:scaletype="center"         android:src="@drawable/ic_check_colored_48" />  </relativelayout> 

thank you.

the reason happening because gridview uses view recycling. when scroll down gridview, instead of creating new views put in bottom, gridview reuses ones disappear top. means have remember set state of view inside call getview().

that's little hard digest, here's code:

public view getview(final int position, view convertview, viewgroup parent) {   ...    view checkeditem = (view) v.findviewbyid(r.id.check);    fbpicture = arrayfbpictures.get(position);   if (fbpicture.ischoosed()) {      checkeditem.setvisibility(view.visible);   } else {      checkeditem.setvisibility(view.invisible);   }    return v; } 

the code above setting checked state of convertview every time getview() called, recycled view not retain of previous state.

ps. done conserve memory , improve performance of gridview , listview. can watch awesome presentation more information.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -