ListView in Android PopupWindow -
is there simple example or tutorial, how put listview (getting value baseadapter through xml parser) in popupwindow in android?
you might want check listpopupwindow
this how did it:
popupwrapper class wraps popupwindow inside more friendly class:
package com.blablabla.android.helpers.gui.dialog; import com.blablabla.android.helpers.r; import android.app.activity; import android.content.context; import android.graphics.rect; import android.graphics.drawable.bitmapdrawable; import android.graphics.drawable.drawable; import android.view.gravity; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.viewgroup.layoutparams; import android.view.windowmanager; import android.widget.popupwindow; /** * class of work of wrapping {@link popupwindow} it's * simpler use. */ public class popupwrapper implements ontouchlistener { /** context show popup */ protected context context = null; /** popup show */ protected popupwindow window = null; /** show in popup */ protected view root = null; /** parent/anchor view */ protected view anchor = null; /** optional background */ protected drawable background = null; /** window manager */ private windowmanager windowmanager; public popupwrapper(activity activity) { anchor = null; context = activity; init(); } public popupwrapper(view anchor) { this.anchor = anchor; context = anchor.getcontext(); init(); } private void init() { window = new popupwindow(context); window.setwidth(windowmanager.layoutparams.wrap_content); window.setheight(windowmanager.layoutparams.wrap_content); window.settouchable(true); //window.setfocusable(true); window.setoutsidetouchable(true); // when touch happens outside of window // make window go away window.settouchinterceptor(this); windowmanager = (windowmanager) context .getsystemservice(context.window_service); oncreate(); } /** * want have happen when created. should create * view , setup event listeners on child views. */ protected void oncreate() { } /** * in case there stuff right before displaying. */ protected void onshow() { } private void preshow() { if (root == null) { throw new illegalstateexception( "setcontentview not called view display."); } onshow(); if (background == null) { window.setbackgrounddrawable(new bitmapdrawable()); } else { window.setbackgrounddrawable(background); } // if using popupwindow#setbackgrounddrawable values of // width , hight make work // otherwise need set background of root viewgroup // , set popupwindow background empty bitmapdrawable window.setwidth(windowmanager.layoutparams.wrap_content); window.setheight(windowmanager.layoutparams.wrap_content); window.settouchable(true); window.setfocusable(true); window.setoutsidetouchable(true); window.setcontentview(root); } @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_outside) { window.dismiss(); return true; } return false; } public void setbackgrounddrawable(drawable background) { this.background = background; } /** * sets content view. should called {@link oncreate} * * @param root * view popup display */ public void setcontentview(view root) { this.root = root; window.setcontentview(root); } /** * inflate , set view resource id * * @param layoutresid */ public void setcontentview(int layoutresid) { layoutinflater inflator = (layoutinflater) context .getsystemservice(context.layout_inflater_service); setcontentview(inflator.inflate(layoutresid, null)); } /** * if want when {@link dismiss} called * * @param listener */ public void setondismisslistener(popupwindow.ondismisslistener listener) { window.setondismisslistener(listener); } /** * displays popdown menu anchor view */ public void showlikepopdownmenu() { showlikepopdownmenu(0, 0); } /** * displays popdown menu anchor view. * * @param xoffset * offset in x direction * @param yoffset * offset in y direction */ public void showlikepopdownmenu(int xoffset, int yoffset) { preshow(); window.setanimationstyle(r.style.animations_popdownmenu); window.showasdropdown(anchor, xoffset, yoffset); } /** * displays quickaction anchor view. */ public void showlikequickaction() { showlikequickaction(0, 0); } /** * displays quickaction anchor view. * * @param xoffset * offset in x direction * @param yoffset * offset in y direction */ public void showlikequickaction(int xoffset, int yoffset) { preshow(); window.setanimationstyle(r.style.animations_growfrombottom); int[] location = new int[2]; anchor.getlocationonscreen(location); rect anchorrect = new rect(location[0], location[1], location[0] + anchor.getwidth(), location[1] + anchor.getheight()); root.measure(layoutparams.wrap_content, layoutparams.wrap_content); int rootwidth = root.getmeasuredwidth(); int rootheight = root.getmeasuredheight(); int screenwidth = windowmanager.getdefaultdisplay().getwidth(); //int screenheight = windowmanager.getdefaultdisplay().getheight(); int xpos = ((screenwidth - rootwidth) / 2) + xoffset; int ypos = anchorrect.top - rootheight + yoffset; // display on bottom if (rootheight > anchorrect.top) { ypos = anchorrect.bottom + yoffset; window.setanimationstyle(r.style.animations_growfromtop); } window.showatlocation(anchor, gravity.no_gravity, xpos, ypos); } public void dismiss() { window.dismiss(); } public void showatlocation(int gravity, int x, int y) { window.showatlocation(anchor, gravity, x, y); } } fileexplorer class example of using popupwrapper:
package com.blablabla.android.helpers.gui.dialog.fexplorer; import java.io.file; import java.io.ioexception; import java.util.arraylist; import java.util.arrays; import java.util.list; import com.blablabla.android.helpers.r; import com.blablabla.android.helpers.gui.viewhelper; import com.blablabla.android.helpers.gui.dialog.popupwrapper; import com.blablabla.android.helpers.util.filehelper; import com.blablabla.android.helpers.util.log; import android.app.activity; import android.os.environment; import android.view.gravity; import android.view.view; import android.view.view.onclicklistener; import android.view.windowmanager; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.button; import android.widget.imagebutton; import android.widget.listview; /** * simple file explorer popup window. allows selecting 1 normal file (no * directory). * * @author mymail@blablabla.eu * */ public class fileexplorer extends popupwrapper implements runnable, onclicklistener, onitemclicklistener { private static final string tag = fileexplorer.class.getname(); /** listview file listing */ private listview filelist = null; /** top path (cannot go backward this) */ private file rootpath = new file(environment.getexternalstoragedirectory() .getpath()); /** current path */ private file path = new file(environment.getexternalstoragedirectory() .getpath()); /** selected item */ private file selected = null; /** notify when file selecting finished */ private onfileselectedlistener listener = null; /** show hidden files or not */ private boolean showhidden = false; public fileexplorer(activity activity, boolean showhidden) { this(activity); this.showhidden = showhidden; } public fileexplorer(activity activity) { super(activity); // view show on popup root = viewhelper.inflateviewbyid(activity, r.layout.explorer); // parent view anchor = viewhelper.getrootview(activity); // listeners initializeviews(); } /** initializes view listeners */ private void initializeviews() { // set popup window window.setfocusable(true); window.setcontentview(root); window.setwidth(windowmanager.layoutparams.wrap_content); window.setheight(windowmanager.layoutparams.wrap_content); window.setclippingenabled(true); // set listview filelist = (listview) root.findviewbyid(r.id.explorer_list); filelist.setchoicemode(listview.choice_mode_single); filelist.setselector(android.r.drawable.screen_background_light_transparent); filelist.setonitemclicklistener(this); // set button listeners imagebutton dirup = (imagebutton) root.findviewbyid(r.id.explorer_up); dirup.setonclicklistener(this); button = (button) root.findviewbyid(r.id.explorer_ok); but.setonclicklistener(this); = (button) root.findviewbyid(r.id.explorer_cancel); but.setonclicklistener(this); } /** first time load files in path */ private void loadlist() { file[] finallist; try { finallist = filehelper.getfilelist(path, showhidden); } catch (ioexception e) { log.e(tag, e.getmessage(), true); return; } fileexploreradapter list = (fileexploreradapter) filelist.getadapter(); // no adapter set yet if (list == null) { // java's way of life... stupid thing ever list<file> aux = new arraylist<file>(); aux.addall(arrays.aslist(finallist)); fileexploreradapter adapter = new fileexploreradapter(context, aux); filelist.setadapter(adapter); } else { // modify adapter list.clear(); list.addall(finallist); list.notifydatasetchanged(); } } public void show() { loadlist(); showatlocation(gravity.center, 0, 0); } @override public void run() { show(); } /** set path show */ public void setpath(string path) { this.path = new file(path); } /** show or not hidden files */ public void showhidden(boolean show) { showhidden = show; } /** refresh list current set path */ public void refresh() { loadlist(); } @override public void onclick(view v) { // cannot use switch/case... int id = v.getid(); if (id == r.id.explorer_up) { dirup(); } else if (id == r.id.explorer_ok) { ok(); } else if (id == r.id.explorer_cancel) { //cancel(); } } /** called when "explorer_up" button clicked */ public void dirup() { file newpath = path.getparentfile(); if (newpath != null) { path = newpath; loadlist(); } } /** called when "explorer_ok" button clicked */ public void ok() { if (selected == null) { log.d(tag, "nothing selected"); } else { log.d(tag, selected.getpath()); } } @override public void onitemclick(adapterview<?> adapter, view v, int pos, long id) { log.d(tag, "onitemclick"); selected = (file) filelist.getitematposition(pos); } } and layout popup window:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp" android:orientation="vertical" > <textview android:id="@+id/explorer_title" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical|center_horizontal" android:layout_margin="8dp" android:ellipsize="marquee" android:text="@string/explorer_title" android:textcolor="#ffffffff" android:textsize="16dp" /> <listview android:id="@+id/explorer_list" android:layout_width="fill_parent" android:layout_height="400dp" android:layout_margin="8dp" > </listview> <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="8dp" android:orientation="horizontal" > <imagebutton android:id="@+id/explorer_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:src="@drawable/dirup_icon" android:onclick="dirup" /> <button android:id="@+id/explorer_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:text="@string/ok" android:textsize="16dp" /> <button android:id="@+id/explorer_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/cancel" android:textsize="16dp" /> </linearlayout> </linearlayout> the problem implementation this
Comments
Post a Comment