android - Adding an imagebutton to a gridView -
i have gridview , want have imagebutton in gridview user can press create new view in gridview. i'd prefer not make gridview items have same onclick method (because adding drag-and-drop functionality use long click , because when this, if make gridview "clickable=true", still makes gridview items non-clickable) i'm wondering how can this.
here xml gridview:
<gridview android:id="@+id/gridview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="30dip" android:numcolumns="auto_fit" android:columnwidth="60dp" android:horizontalspacing="10dp" android:verticalspacing="10dp" android:gravity="center" android:stretchmode="columnwidth" android:clickable="true" > </gridview>
and here custom adapter:
package com.example.awesomefilebuilderwidget; imports public class gridviewadapter extends baseadapter { private context context; // keep images in array list public arraylist<integer> drawables = new arraylist<integer>(); checkbox mcheckbox=null; // constructor public gridviewadapter(context c){ context = c; log.d("gridviewadapter", "constructor set"); drawables.add(r.drawable.pattern1); log.d("gridviewadapter", "pattern1 added"); drawables.add(r.drawable.pattern2); log.d("gridviewadapter", "pattern2 added"); drawables.add(r.drawable.trashcan); log.d("gridviewadapter", "trashcan added"); drawables.add(r.drawable.ic_launcher); log.d("gridviewadapter", "ic_launcher added"); } public void setcheckbox(checkbox checkbox){ mcheckbox=checkbox; } @override // how many items in data set represented adapter public int getcount() { return drawables.size(); } @override // data item associated specified position in // data set public object getitem(int position) { return drawables.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { // try reuse views imageview view = (imageview) convertview; boolean checked = (mcheckbox==null)?false:(((checkbox) mcheckbox).ischecked()); // if convert view null create new instance else reuse if (view == null) { view = new imageview(context); log.d("gridviewadapter", "new imageview added"); view.setid(r.id.iconimageview_id); } if(checked == true){ issdreadable(); log.i("gridviewadapter", "checkbox checked"); } else { log.i("gridview", "icons not use/checkbox not checked"); } /* view.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub imageview folder = new imageview(context); folder.setimageresource(r.drawable.ic_launcher); } });*/ view.setimageresource(drawables.get(position)); view.setscaletype(imageview.scaletype.center_crop); view.setlayoutparams(new android.widget.gridview.layoutparams(70, 70)); view.settag(string.valueof(position)); return view; } }
is there way can put imagebutton in actual gridview , set onclick method same this:
view.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub imageview folder = new imageview(context); folder.setimageresource(r.drawable.ic_launcher); } });*/
? i've tried looking around haven't found helps.
also, i've tried adding in "add_button" (the button want user push make new view image ic_launcher) constructor if that, how can make first gridview item clickable , not affect other imageviews?
i struggling same problem , got solution :
1)create interface in adapter.
2)declare method in interface.
3)in onclicklistener() of image button call method .
4)in mainactivity write functionality want implement in method declared in interface.
or
1)create image view instead of image button .
2)for gridview write onitemclicklistener , inside onitemclicked write intent.
3) call intent in activity want go getintent().
4) in intent object putextra() method , pass whatever want pass current activity next activity.
hope helps!!!
Comments
Post a Comment