android - image viewer not remove dynamically at current position -


when click current delete image icon of image viewer delete other position image want delete particular position image delete of image viewer. if image size 3. found image position 1 2 2. again found position 2 0 0.

my activity :

    public class onlyselectgridviewscreen extends baseactivity {      public static onlyselectedfullimageadapter adapter;     public static viewpager viewpager;     private static final int take_photo_code = 0;     private arraylist<string> imagepaths;     private static final string tag = "onlyselectgridviewscreen";     private textview tvheader;     private gridview gridview;     private int columnwidth;     string[] selectimages;     private int imgposition = -1;     private string locname, deptname, cusfoldername, realimagesdirectory;     private imageview btndelete;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.full_image_screen);          imagepaths = new arraylist<string>();         intent = getintent();         locname = i.getstringextra("locname");         deptname = i.getstringextra("deptname");         cusfoldername = i.getstringextra("cusfoldername");         realimagesdirectory = i.getstringextra("realimagesdirectory");          selectimages = i.getstringarrayextra("selectimages");         toast.maketext(getbasecontext(), tag+"\n selectimages size :"+selectimages.length, toast.length_long).show();         list<string> l = arrays.<string> aslist(selectimages);          // if list<string> isn't specific enough:         imagepaths = new arraylist<string>(l);         tvheader = (textview) findviewbyid(r.id.tvheader);         tvheader.settext("" + locname + " / " + deptname + " / "                 + cusfoldername);         viewpager = (viewpager) findviewbyid(r.id.pager);          int position = i.getintextra("position", 0);          adapter = new onlyselectedfullimageadapter(                 onlyselectgridviewscreen.this, imagepaths, locname, deptname,                 cusfoldername, realimagesdirectory);         if (imagepaths.size() > 0) {             viewpager.clearfocus();             adapter.notifydatasetchanged();         }         viewpager.setadapter(adapter);          // displaying selected image first         viewpager.setcurrentitem(position);         btnback = (imageview) findviewbyid(r.id.backnow);         btndelete = (imageview) findviewbyid(r.id.btndelete);           // delete current image          btndelete.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                  file file = new file(imagepaths.get(imgposition));                 boolean deleted = file.delete();                 if (deleted) {                     toast.maketext(onlyselectgridviewscreen.this,                             "image delete successfully.", toast.length_long)                             .show();                     imagepaths.remove(imgposition);                     // start of update part                     if (imagepaths.size() == 0) {                         toast.maketext(                                 getbasecontext(),                                 "not image found, add image or previous page.",                                 toast.length_long).show();                     } else {                          int position = 0;                          adapter = new onlyselectedfullimageadapter(                                 onlyselectgridviewscreen.this, imagepaths,                                 locname, deptname, cusfoldername,                                 realimagesdirectory);                         adapter.notifydatasetchanged();                         viewpager.clearfocus();                         viewpager.setadapter(adapter);                          // displaying selected image first                         viewpager.setcurrentitem(position);                         // end of update part                     }                 } else {                     toast.maketext(onlyselectgridviewscreen.this,                             "sorry, image delete fails.", toast.length_long)                             .show();                  }              }         });      }      public class onlyselectedfullimageadapter extends pageradapter {          private activity _activity;         private arraylist<string> _imagepaths;         private layoutinflater inflater;         private string locname, deptname, foldername, realimagesdirectory;          // constructor         public onlyselectedfullimageadapter(activity activity,                 arraylist<string> imagepaths, string _mlocationname,                 string _mdeptname, string _mfoldername,                 string realimagesdirectory) {             this._activity = activity;             this._imagepaths = imagepaths;             this.locname = _mlocationname;             this.deptname = _mdeptname;             this.foldername = _mfoldername;             this.realimagesdirectory = realimagesdirectory;         }          @override         public int getcount() {             return this._imagepaths.size();         }          @override         public boolean isviewfromobject(view view, object object) {             return view == ((relativelayout) object);         }          @override         public object instantiateitem(viewgroup container, final int position) {             touchimageview imgdisplay;             imgposition = position;             inflater = (layoutinflater) _activity                     .getsystemservice(context.layout_inflater_service);             view viewlayout = inflater.inflate(                     r.layout.layout_fullscreen_image, container, false);              imgdisplay = (touchimageview) viewlayout                     .findviewbyid(r.id.imgdisplay);             // dimensions of view             // int targetw = imgdisplay.getwidth();             // int targeth = imgdisplay.getheight();              int targetw = 100;             int targeth = 100;              // dimensions of bitmap             bitmapfactory.options bmoptions = new bitmapfactory.options();             bmoptions.injustdecodebounds = true;             bitmapfactory.decodefile(_imagepaths.get(position), bmoptions);             int photow = bmoptions.outwidth;             int photoh = bmoptions.outheight;              // determine how scale down image             int scalefactor = math.min(photow / targetw, photoh / targeth);              // decode image file bitmap sized fill view             bmoptions.injustdecodebounds = false;             bmoptions.insamplesize = scalefactor;             bmoptions.inpurgeable = true;              bitmap bitmap = bitmapfactory.decodefile(_imagepaths.get(position),                     bmoptions);             imgdisplay.setimagebitmap(bitmap);              ((viewpager) container).addview(viewlayout);              return viewlayout;         }          @override         public void destroyitem(viewgroup container, int position, object object) {             ((viewpager) container).removeview((relativelayout) object);          }      }    } 

full_image_screen.xml file below:

     <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <!-- header aligned top -->      <include         android:id="@+id/header"         layout="@layout/header" >     </include>      <!-- footer aligned bottom -->      <relativelayout         android:id="@+id/footer"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparentbottom="true"         android:background="#0c95d4"         android:gravity="center" >          <linearlayout             android:layout_width="match_parent"             android:layout_height="50dp"             android:gravity="center" >                <imageview                 android:id="@+id/btndelete"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_gravity="center"                 android:layout_weight="1"                 android:background="@drawable/img_selector"                 android:clickable="true"                 android:src="@drawable/delete_icon_black" />             </linearlayout>     </relativelayout>      <!-- content below header , above footer -->      <relativelayout         android:id="@+id/content"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_above="@id/footer"         android:layout_below="@id/header" >          <android.support.v4.view.viewpager             android:id="@+id/pager"             android:layout_width="fill_parent"             android:layout_height="fill_parent" />     </relativelayout>  </relativelayout> 

and adapter layout_fullscreen_image.xml file beow:

    <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <com.databizsoftware.cg.helpers.touchimageview         android:id="@+id/imgdisplay"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:scaletype="fitcenter" />  </relativelayout> 

and header.xml file below :

    <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="50dp"     android:layout_alignparenttop="true"     android:background="#fff"     android:gravity="center"     android:orientation="vertical" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:orientation="horizontal"         android:weightsum="5" >          <textview             android:id="@+id/tvheader"             android:layout_width="0dp"             android:layout_height="wrap_content"             android:layout_weight="4"             android:paddingleft="8dp"             android:text="fixed header"             android:textcolor="#33b5e5"             android:textsize="12sp" />          <imageview             android:id="@+id/ihome"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_weight="1"             android:background="@drawable/img_selector"             android:clickable="true"             android:onclick="gotohome"             android:visibility="visible"             android:src="@drawable/home_black" />          <imageview             android:id="@+id/ilogout"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="left"             android:layout_weight="1"             android:background="@drawable/img_selector"             android:clickable="true"             android:onclick="logoutaction"             android:paddingright="8dp"             android:src="@drawable/logout_black" />     </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparentbottom="true" >          <view             android:layout_width="match_parent"             android:layout_height="3dip"             android:background="#33b5e5" />     </linearlayout>  </relativelayout> 

why not delete current position of image viewer. please me.

implement pagechangelistener , can achive this.   viewpager.setonpagechangelistener(new onpagechangelistener(){              public void onpagescrollstatechanged(int arg) {              }              public void onpagescrolled(int arg0, float arg1, int arg2) {                 // todo auto-generated method stub              }              public void onpageselected(int position) {                imgposition = position;             }          }); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -