java - Image click of RecyclerView -


i know there questions on this,but format follow code different unable find answer question. when click on recyclerview item includes textview , imageview, want show image in detailactivity.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/framelayout"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="pritish.sawant.com.photogallery.mainactivity"> </framelayout> 

fragment_photo_gallery.xml

<relativelayout     android:orientation="vertical"     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"     tools:context="pritish.sawant.com.photogallery.photogalleryfragment">  <android.support.v7.widget.recyclerview     android:layout_width="match_parent"     android:id="@+id/recyclerview"     android:layout_height="match_parent"></android.support.v7.widget.recyclerview>  <progressbar     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/loading_indicator"     style="@style/widget.appcompat.progressbar"     android:layout_centerinparent="true"/>  </relativelayout> 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal">  <textview     android:id="@+id/textview"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_weight="1"     tools:text="hi" />  <imageview     android:id="@+id/imageview"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_weight="1"     tools:src="@drawable/image1">  </imageview>  </linearlayout> 

activity_detail.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="pritish.sawant.com.photogallery.detailactivity">           <imageview             android:layout_width="match_parent"             android:layout_height="match_parent"             android:id="@+id/detailimageview"             tools:src="@drawable/image1"/>  </linearlayout> 

mainactivity.java

public class mainactivity extends appcompatactivity {  private fragmentmanager fragmentmanager; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      settitle("photo gallery");     fragmentmanager=getsupportfragmentmanager();     fragment fragment=fragmentmanager.findfragmentbyid(r.id.framelayout);     if(fragment==null){         fragment=new photogalleryfragment();         fragmentmanager.begintransaction().add(r.id.framelayout,fragment).commit();     } } } 

photo.java

package pritish.sawant.com.photogallery;  public class photo {  private string title; private string author; private string authorid; private string link; private string image; private string tag;  public string gettitle() {     return title; }  public void settitle(string title) {     this.title = title; }  public string getauthor() {     return author; }  public void setauthor(string author) {     this.author = author; }  public string getauthorid() {     return authorid; }  public void setauthorid(string authorid) {     this.authorid = authorid; }  public string getlink() {     return link; }  public void setlink(string link) {     this.link = link; }  public string getimage() {     return image; }  public void setimage(string image) {     this.image = image; }  public string gettag() {     return tag; }  public void settag(string tag) {     this.tag = tag; }  @override public string tostring() {     return "photo{" +             "title='" + title + '\'' +             ", author='" + author + '\'' +             ", authorid='" + authorid + '\'' +             ", link='" + link + '\'' +             ", image='" + image + '\'' +             ", tag='" + tag + '\'' +             '}'; } 

}

flickrfetchr.java

package pritish.sawant.com.photogallery;  import android.net.uri;  import org.json.jsonarray; import org.json.jsonobject;  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; import java.util.list;   public class flickrfetchr {  private static final string tag = "flickrfetchr"; private list<photo> photolist = null; private string baseurl="https://api.flickr.com/services/feeds/photos_public.gne";  public  string getdata(string url){     bufferedreader bufferedreader=null;     try {         url url1=new url(url);         httpurlconnection httpurlconnection=(httpurlconnection)url1.openconnection();         stringbuilder stringbuilder=new stringbuilder();         bufferedreader=new bufferedreader(new inputstreamreader(httpurlconnection.getinputstream()));         string line;         while ((line=bufferedreader.readline())!=null){             stringbuilder.append(line+"\n");         }         return stringbuilder.tostring();      } catch (exception e) {         e.printstacktrace();         return null;     }     {         if(bufferedreader!=null){             try {                 bufferedreader.close();             } catch (ioexception e) {                 e.printstacktrace();                 return null;             }         }     } }  private string buildurl(string searchphoto, string lang, boolean match){     return uri.parse(baseurl).buildupon().appendqueryparameter("tags", searchphoto)             .appendqueryparameter("tagmode", match ? "all" : "any")             .appendqueryparameter("format", "json")             .appendqueryparameter("lang", lang)             .appendqueryparameter("nojsoncallback", "1").build().tostring(); }  public list<photo> fetchphotos(string searchphoto,string language,boolean matchall){     string url=buildurl(searchphoto,language,matchall);     return downloadgalleyitem(url); }   //to receive json format public list<photo> downloadgalleyitem(string url){     photolist=new arraylist<>();     photo photo=new photo();     string jsonstring=getdata(url);     try {         jsonobject jsonobject=new jsonobject(jsonstring);         jsonarray jsonarray=jsonobject.getjsonarray("items");          for(int i=0;i<jsonarray.length();i++){             jsonobject jsonobject1=jsonarray.getjsonobject(i);             photo.settitle(jsonobject1.getstring("title"));             photo.setauthor(jsonobject1.getstring("author"));             photo.setauthorid(jsonobject1.getstring("author_id"));             photo.settag(jsonobject1.getstring("tags"));              jsonobject jsonmedia =jsonobject1.getjsonobject("media");             string imageurl=jsonmedia.getstring("m");             photo.setimage(jsonmedia.getstring("m"));              //we changing _m _b when image tapped biigger image             photo.setlink(imageurl.replaceall("_m.","_b."));             photolist.add(photo);         }     } catch (exception e) {         e.printstacktrace();     }     return photolist; } 

}

recycleritemclicklistener.java

public class recycleritemclicklistener extends recyclerview.simpleonitemtouchlistener {  private static final string tag = "recycleritemclicklisten";  interface onrecyclerclicklistener{     void onitemclick(view view,int position);     void onitemlongclick(view view,int position); }  private final onrecyclerclicklistener listener; private final gesturedetectorcompat gesturedetectorcompat;  public recycleritemclicklistener(context context, final recyclerview recyclerview, final onrecyclerclicklistener listener) {     this.listener = listener;     gesturedetectorcompat=new gesturedetectorcompat(context,new gesturedetector.simpleongesturelistener(){         @override         public boolean onsingletapup(motionevent e) {             view childview=recyclerview.findchildviewunder(e.getx(),e.gety());             if(childview!=null && listener!=null){                 listener.onitemclick(childview,recyclerview.getchildadapterposition(childview));             }             return true;         }          @override         public void onlongpress(motionevent e) {             view childview=recyclerview.findchildviewunder(e.getx(),e.gety());             if(childview!=null && listener!=null){                 listener.onitemlongclick(childview,recyclerview.getchildadapterposition(childview));             }          }     }); }  public boolean onintercepttouchevent(recyclerview recyclerview, motionevent motionevent){     if (gesturedetectorcompat!=null){         boolean result=gesturedetectorcompat.ontouchevent(motionevent);         return result;     }else{         return false;     } } 

}

photogalleryfragment.java

public class photogalleryfragment extends fragment implements recycleritemclicklistener.onrecyclerclicklistener{  private recyclerview recyclerview; public progressbar progressbar; public list<photo> photos = new arraylist<>();   public photogalleryfragment() {     // required empty public constructor }  @override public void oncreate(@nullable bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setretaininstance(true);     sethasoptionsmenu(true);  }  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment      view view = inflater.inflate(r.layout.fragment_photo_gallery, container, false);     progressbar = (progressbar) view.findviewbyid(r.id.loading_indicator);     recyclerview = (recyclerview) view.findviewbyid(r.id.recyclerview);      recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity()));     recyclerview.addonitemtouchlistener(new recycleritemclicklistener(getactivity(),recyclerview,this));      progressbar.setvisibility(view.gone);     setupadapter();     new fetchtask().execute();     return view;  }  private void setupadapter() {     //isadded checks whether fragment has been added activity     if (isadded()) {         recyclerview.setadapter(new photoadapter(photos));     } }  @override public void onitemclick(view view, int position) {     toast.maketext(getactivity(),"item clicked",toast.length_long).show();     intent intent=new intent(getactivity(),detailactivity.class);     //what should pass in putextra of intent     //intent.putextra("image",photo.getlink());     startactivity(intent); }  @override public void onitemlongclick(view view, int position) {     toast.maketext(getactivity(),"item long clicked",toast.length_long).show(); }  private class photoholder extends recyclerview.viewholder {      private imageview imageview;     private textview textview;      public photoholder(view itemview) {         super(itemview);         imageview = (imageview) itemview.findviewbyid(r.id.imageview);         textview = (textview) itemview.findviewbyid(r.id.textview);     }      public void binditems(photo photos1) {          picasso.with(getactivity()).load(photos1.getimage()).into(imageview);          textview.settext(photos1.gettitle());           //  glide.with(getactivity()).load(photos1.getimage()).into(imageview);     } }   private class photoadapter extends recyclerview.adapter<photoholder> {      private list<photo> galleryitems;      public photoadapter(list<photo> galleryitems) {         this.galleryitems = galleryitems;     }      @override     public photoholder oncreateviewholder(viewgroup parent, int viewtype) {         layoutinflater layoutinflater = layoutinflater.from(getactivity());         view view = layoutinflater.inflate(r.layout.list_item, parent, false);          return new photoholder(view);     }      @override     public void onbindviewholder(photoholder holder, int position) {         photo photos2 = galleryitems.get(position);         holder.binditems(photos2);        }      @override     public int getitemcount() {         return galleryitems.size();     } }  private class fetchtask extends asynctask<void, void, list<photo>> {       @override     protected void onpreexecute() {         super.onpreexecute();         if (photos.size() == 0) {             progressbar.setvisibility(view.visible);         }     }      @override     protected list<photo> doinbackground(void... params) {         return new flickrfetchr().fetchphotos("android", "en-us", false);       }      @override     protected void onpostexecute(list<photo> photos1) {         super.onpostexecute(photos);         progressbar.setvisibility(view.gone);         photos = photos1;         setupadapter();     } }  } 

detailactivity.java

public class detailactivity extends appcompatactivity {  private imageview imageview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_detail);     imageview=(imageview)findviewbyid(r.id.detailimageview);     int img_id;      if (savedinstancestate != null ){         img_id = getintent().getintextra("image",0);         imageview.setimageresource(img_id);      } } } 

in onitemclick can pass this

toast.maketext(getactivity(),"item clicked",toast.length_long).show();     intent intent=new intent(getactivity(),detailactivity.class);     //what should pass in putextra of intent     intent.putextra("image",photos.get(position).getlink());     startactivity(intent); 

in detailsactivity can retrieve by

 if (getintent().getstringextra("image") != null) {             imageurl = getintent().getstringextra("image");         } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -