android - How to properly use Dagger 2 to callback from adapter to activity/fragment? -


i'm talking new architecture presented on last google io.

now have next implementation:

public class thumbnailsadapterviewmodel extends viewmodel {      mutablelivedata<thumbnailselected> thumbnailselectedmutablelivedata = new mutablelivedata<>();      @inject     public thumbnailsadapterviewmodel() {     }      public livedata<thumbnailselected> getselectedthumbnail() {         return thumbnailselectedmutablelivedata;     }      public void setthumbnailselected(thumbnailselected thumbnailselected) {         thumbnailselectedmutablelivedata.setvalue(thumbnailselected);     } } 

myfragment:

@inject thumbnailsadapter thumbnailsadapter;   // oncreateview thumbnailsadapterviewmodel thumbnailsadapterviewmodel = viewmodelproviders.of(this, viewmodelfactory).get(thumbnailsadapterviewmodel.class);         thumbnailsadapterviewmodel.getselectedthumbnail().observe(this, new observer<thumbnailselected>() {             @override             public void onchanged(@nullable thumbnailselected thumbnailselected) {                 if (thumbnailselected != null) {                     snackbar.make(getview(), "thumbnail #" + thumbnailselected.getposition() + " selected", snackbar.length_short).show();                 }             }         });  thumbnailsadapter.setviewmodel(thumbnailsadapterviewmodel); 

thumbnailsadapter:

public class thumbnailsadapter extends recyclerview.adapter<thumbnailsadapter.viewholder> {      list<thumbnail> thumbnails;     private thumbnailsadapterviewmodel viewmodel;      @inject     public thumbnailsadapter(list<thumbnail> thumbnails) {         this.thumbnails = thumbnails;     }      public void setviewmodel(thumbnailsadapterviewmodel viewmodel) {         this.viewmodel = viewmodel;     }      @override     public viewholder oncreateviewholder(viewgroup parent, int viewtype) {         view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.item_thumbnail, parent, false);         return new viewholder(view);     }      @override     public void onbindviewholder(viewholder holder, int position) {         holder.binddata(thumbnails.get(position), position);     }      @override     public int getitemcount() {         return thumbnails.size();     }      class viewholder extends recyclerview.viewholder {          // ... implementation      public void binddata(final thumbnail thumbnail, final int position) {              imagethumbnail.setimageresource(r.drawable.ic_thumbnail);              imagethumbnail.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view view) {                     viewmodel.setthumbnailselected(new thumbnailselected(thumbnail, position));                 }             });         }     } } 

the main disadvantage: i'm passing viewmodel adapter ideal inject it, cannot (at least don't know how properly). guess viewmodel created scope. tried create in next class:

@module public class activitymodulescopemain {      @provides     list<thumbnail> providethumbnails() {         list<thumbnail> thumbnails = new arraylist<>();         // here list adapter created         return thumbnails;     } } 

one more dagger module present whole (i hope) picture:

@module public abstract class activitymodulemain {      @contributesandroidinjector(modules = activitymodulescopemain.class)     abstract mainfragment contributemainfragment(); } 

if it's not enough i'm ready post more code.

it's working solution i'm not satisfied.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -