android - List view with button and popup -
am struck issue.. have list view contains 1 button , when click on button should show pop up. have implemented list view pop not showing on clicked button position..
sample code given below
public view getview(int position, view convertview, final viewgroup parent) { view vi=convertview; if(convertview==null) vi = inflater.inflate(r.layout.list_row, null); vi.findviewbyid(r.id.statusimage).setontouchlistener(new view.ontouchlistener(){ @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub x=(int) event.getx(); y=(int) event.gety(); showpopup(x,y);// calls popup return false; public void showpopup(int xk , int ys) { layoutinflater inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); view layout = inflater.inflate(r.layout.popup_layout,null); pwindo = new popupwindow(layout, 300, 250, true); pwindo.showatlocation(layout, gravity.no_gravity, xk,ys); pwindo.setoutsidetouchable(true); pwindo.settouchable(true); pwindo.setbackgrounddrawable(new bitmapdrawable()); layout.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view arg0, motionevent arg1) { pwindo.dismiss(); return true; } }); } public class myadapter extends baseadapter{ private activity activity; private popupwindow pwindo; point p; private arraylist<hashmap<string, string>> data; private static layoutinflater inflater=null; string value="0"; imagebutton imgbn ; int x,y; public myadapter (activity a, arraylist<hashmap<string, string>> d) { activity = a; data=d; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); } public int getcount() { return data.size(); } /* public boolean ontouchevent(motionevent event) { // motionevent object holds x-y values if(event.getaction() == motionevent.action_down) { x=(int) event.getx(); y=(int) event.gety(); } return true; }*/ public object getitem(int position) { hashmap<string, string> song = new hashmap<string, string>(); song = data.get(position); return (song.get(jobslisting.key_id)); } public long getitemid(int position) { return position; } public view getview(int position, view convertview, final viewgroup parent) { view vi=convertview; if(convertview==null) /* vi = inflater.inflate(r.layout.list_row, null); view imgview = vi.findviewbyid(r.id.statusimage); imgview.settag(position); */ vi = inflater.inflate(r.layout.list_row, null); imageview button = (imageview) vi.findviewbyid(r.id.statusimage); // x, y location , store in location[] array // location[0] = x, location[1] = y. button.settag(position); button.setontouchlistener(new view.ontouchlistener(){ @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub int y; if(v.gettag() == null ) { y=100; } else { int position = (integer) v.gettag(); y = (1+position)*v.getheight(); } int x= (int) v.getright(); showpopup(x,y);// calls popup return false; } }); /* vi.findviewbyid(r.id.statusimage).setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub //int offset_x = 30; // int offset_y = 30; showpopup(); //layoutinflater inflater = (layoutinflater) vi.getsystemservice(context.layout_inflater_service); // layoutinflater inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); //layoutinflater inflater = layoutinflater.from(parent.getcontext()); // view layout = inflater.inflate(r.layout.popup_layout,null); //pwindo = new popupwindow(layout, 300, 250, true); //pwindo.showatlocation(layout, gravity.right, p.x + offset_x, p.y + offset_y); //pwindo.setoutsidetouchable(true); // pwindo.settouchable(true); // pwindo.setbackgrounddrawable(new bitmapdrawable()); } });*/ textview artistd = (textview)vi.findviewbyid(r.id.artistd); textview titleb = (textview)vi.findviewbyid(r.id.titleb); textview title = (textview)vi.findviewbyid(r.id.title); // title textview artist = (textview)vi.findviewbyid(r.id.artist); // artist name imageview thumb_image=(imageview)vi.findviewbyid(r.id.list_image); // thumb image string fontpaths = "fonts/helveticaneue-bold.otf"; typeface tfs = typeface.createfromasset(activity.getassets(), fontpaths); title.settypeface(tfs); artist.settypeface(tfs); titleb.settypeface(tfs); artistd.settypeface(tfs); hashmap<string, string> song = new hashmap<string, string>(); song = data.get(position); // setting values in listview title.settext(song.get(jobslisting.key_title)); artist.settext(song.get(jobslisting.key_artist)); artistd.settext(song.get(jobslisting.key_place)); titleb.settext(song.get(jobslisting.key_clientname)); return vi; } public void showpopup(int xk , int ys) { system.out.println("xk>>>"+xk+"xk<<<<"+ys); layoutinflater inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); view layout = inflater.inflate(r.layout.popup_layout,null); pwindo = new popupwindow(layout, 300, 250, true); pwindo.showatlocation(layout, gravity.no_gravity, xk,ys); pwindo.setoutsidetouchable(true); pwindo.settouchable(true); layout.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view arg0, motionevent arg1) { pwindo.dismiss(); return true; } }); }
}
event.gety() return y position relative parents have find position of row clicked , height of each row calculate y position.
this code display popup @ row imageview clicked , littele modification use.
@override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; if(convertview == null) { layoutinflater inflater = (layoutinflater) ctx.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.list_row, null); holder = new viewholder(); convertview.settag(holder); holder.imgview = (imageview) convertview.findviewbyid(r.id.txtviewclientname); } else { holder = (viewholder) convertview.gettag(); } holder.imgview.settag(position); holder.imgview.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { int y; if(v.gettag() == null ) { y=100; } else { int position = (integer) v.gettag(); y = (1+position)*v.getheight(); } int x= (int) v.getright(); showpopup(x,y);// calls popup return false; } private void showpopup(int x, int y) { layoutinflater inflater = (layoutinflater)ctx.getsystemservice(context.layout_inflater_service); view layout = inflater.inflate(r.layout.popup_layout,null); final popupwindow pwindo = new popupwindow(layout, 200, 300, true); pwindo.showatlocation(layout, gravity.no_gravity, x, y); pwindo.setoutsidetouchable(true); pwindo.settouchable(true); layout.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view arg0, motionevent arg1) { pwindo.dismiss(); return true; } }); } }); return convertview; } private static class viewholder { public imageview imgview = null; } // popup_layout <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dp" android:layout_height="200dp" android:background="@android:color/darker_gray" android:orientation="vertical" > <textview android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:text="popup" > </textview> </linearlayout> /// list_row layout <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <imageview android:id="@+id/statusimage" android:layout_width="wrap_content" android:src="@drawable/ic_launcher" android:layout_height="wrap_content" android:text="button" /> </linearlayout>
Comments
Post a Comment