android - implement onClickListener for Horizontal ScrollView -
i'm populating horizontal scrollview using picasso
, problem can't implement onclicklistener
because item views of list doesn't have index.
xml:
<horizontalscrollview android:layout_width="match_parent" android:layout_height="120dp" android:id="@+id/mygallery"> <linearlayout android:id="@+id/channelsscrollview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> </horizontalscrollview>
and code:
private void populateimages() { final list<string> urls = new arraylist<>(); //getting list of urls (int = 0; < activityloading.datachannelsarraylist.get(0).getchannelarraylist().size(); i++) { urls.add(getstring(r.string.get_channels_img_host) + activityloading.datachannelsarraylist.get(0).getchannelarraylist().get(i).getid()); } //add views horizontal scrollview amount of urls mygallery = (linearlayout) findviewbyid(r.id.channelsscrollview); (int = 0; < urls.size(); i++) { mygallery.addview(insertphoto(urls.get(i), i)); } } public view insertphoto(string path, int position) { linearlayout layout = new linearlayout(getapplicationcontext()); layout.setgravity(gravity.center); final squaredimageview squaredimageview = new squaredimageview(getapplicationcontext(), position); squaredimageview.setscaletype(imageview.scaletype.center_crop); picasso.with(this).load(path).placeholder(r.drawable.loading_small).into(squaredimageview); //i need receive somehow position of view pressed squaredimageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //this crashes here toast.maketext(getapplicationcontext(), squaredimageview.id, toast.length_short).show(); } }); layout.addview(squaredimageview); return layout; }
how bind index of urls
list respective squaredimageview
item in horizontal scrollview? or maybe can suggest better approach onclick
option 1: change position parameter final: public view insertphoto(string path, final int position) {
then inside onclicklistener: have access position parameter. toast.maketext(getapplicationcontext(), "position: " + position, toast.length_short).show();
option 2: set tag of squaredimageview: squaredimageview.settag(position);
then in onclicklistener tag calling: int location = (int)v.gettag();
Comments
Post a Comment