Android custom Popupwindow and parameter passing and returning

Source: Internet
Author: User

Before this blog, also wrote an article about Popupwindow, that is mainly about the Popupwindow pop-up location settings. and select the Popupwindow layout after monitoring. For details see the Android Popupwindow sample program one. The next chapter is mainly about custom Popupwindow and parameter passing and returning, and I wrote a ListView in it to sample. Next look at the code, there are comments.

Download the project resources in this article:

First, mainactivity

<span style= "Background-color:rgb (240, 240, 240); >package Com.example.popupwimdow;</span>import Java.util.arraylist;import Com.example.popupwimdow.testpopwindow2.ongetdata;import Android.os.bundle;import;import Android.view.gravity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.popupwindow.ondismisslistener;import android.widget.Toast;/** * * @ Author Mmsx * Blogger Blog URL: */public class Mainactivity extends Activity implements Onclicklis Tener,ondismisslistener {private arraylist<itemtest> marraylist = new arraylist<itemtest> ();p rivate TestPopwindow2 mTestPopwindow2 = null;private int mnseclectitem = 0; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitUI ();} private void Initui () {if (marraylist! = null) {marraylist.clear (); for (int i = 0; i < 3; i++) {IteMtest itemTest1 = new Itemtest (R.DRAWABLE.BMP1, "picture One"); Marraylist.add (itemTest1); Itemtest itemTest2 = new Itemtest ( R.DRAWABLE.BMP2, "picture Two"); Marraylist.add (ITEMTEST2);}} Instantiate testpopwindow2mtestpopwindow2 = new TestPopwindow2 (this);// Set Click other Location mTestPopwindow2 disappear Mtestpopwindow2.setondismisslistener (this); Button ButtonTest2 = (button) Findviewbyid (R.ID.BUTTONTEST2); Buttontest2.setonclicklistener (this);} private void OnPopwindowTest2 () {if (MTestPopwindow2 = = null) return;//back to accept Mtestpopwindow2.setondata (new Ongetdata () {/ /Record the last selected Item@overridepublic int Onseclectitem () {return mnseclectitem;} The callback accepts the function @overridepublic void ondatacallback (int nsectlect,arraylist<itemtest> marraylist) {Toast.maketext ( Getapplicationcontext (), "ListView Click" + string.valueof (nsectlect), Toast.length_short). Show (); Mnseclectitem = Nsectlect;} Pass data source past @overridepublic arraylist<itemtest> onarraylist () {return marraylist;}); /Location Get the position of the control int[] site = new INT[2]; View v = Findviewbyid (;(v! = null) v.getlocationonscreen (location); The control Mtestpopwindow2.setanimationstyle ( in the position of the screen;//MTestPopwindow2 pops up in a control (button) The following mtestpopwindow2.showatlocation (V, | Gravity.left,location[0]-v.getwidth (), location[1] + v.getheight ());} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case (); Break;default: Break;}} Click somewhere else to disappear @overridepublic void Ondismiss () {}}
Second, TestPopwindow2

Package Com.example.popupwimdow;import Java.util.arraylist;import;import;import Android.content.context;import;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.listview;import android.widget.popupwindow;import android.widget.textview;/** * Custom TestPopwindow2 * @ Author Mmsx * Blogger Blog URL: */public class TestPopwindow2 extends Popupwindow implements Onite mclicklistener{//root View Private view mrootview;private layoutinflater minflater;//ArrayList array, listview raw Data private arraylist<itemtest> marraylist;//Data Interface Ongetdata mongetdata;//ListView Adapter Selftypeadapt mselftypeadapt;private int mnseclectitem = 0;public TestpopwindoW2 (Activity context) {super (context); InitData (context); Initui ();} Data interface settings, data source interface incoming public void Setondata (Ongetdata sd) {mongetdata = Sd;marraylist = new Arraylist<itemtest> (); if (MO Ngetdata = null) {marraylist = Mongetdata.onarraylist (); mnseclectitem = Mongetdata.onseclectitem ();}} Data interface abstract method public interface Ongetdata {abstract arraylist<itemtest> onarraylist (); abstract int onseclectitem (); abstract void Ondatacallback (int nsectlect,arraylist<itemtest> marraylist);} private void InitData (context context) {Minflater = (layoutinflater) context.getsystemservice (context.layout_inflater _service); Mrootview = Minflater.inflate (r.layout.test_popwindow_2, null); Setcontentview (Mrootview); This.setWidth ( layoutparams.fill_parent); This.setheight (layoutparams.wrap_content);//Set Popupwindow Popup related properties settouchable (true); Setoutsidetouchable (True); Setfocusable (true); Setbackgrounddrawable (New Bitmapdrawable (Context.getresources ())); Update (); Getcontentview (). Setfocusableintouchmode (true); GetconTentview (). Setfocusable (True); Setanimationstyle (;} private void Initui () {ListView ListView = (ListView) Mrootview.findviewbyid (; mselftypeadapt = new Selftypeadapt (Mrootview.getcontext ()); Listview.setadapter (MSELFTYPEADAPT); Listview.setonitemclicklistener (this );} public class Selftypeadapt extends Baseadapter {private Context mcontext;private layoutinflater mlayoutinflater;public SELFTYPEADAPT (Context context) {Mcontext = Context;mlayoutinflater = (layoutinflater) mcontext.getsystemservice ( Context.layout_inflater_service);} @Overridepublic int GetCount () {return marraylist = = null? 0:marraylist.size ();} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {return 0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder Viewholder = null;if (convertvi EW = = NULL) {Viewholder = new Viewholder (); Convertview = (linearlayout) mlayoutinflater.inflate (R.layout.listview_item, null); Viewholder.textview = (TextView) Convertview.findviewbyid (; Viewholder.imageview = ( ImageView) Convertview.findviewbyid (; Convertview.settag (Viewholder);} else {Viewholder = (Viewholder) Convertview.gettag ();} Itemtest itemtest = new Itemtest () Itemtest = Marraylist.get (position); ViewHolder.textView.setText ( Itemtest.getimageviewid ()); ViewHolder.imageView.setImageResource (Itemtest.getimageviewid ()); return Convertview;}} Inner class implementation, lifting the ListView efficiency class Viewholder {public ImageView imageview;public TextView TextView;} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {    Mongetdata.ondatacallback (position, marraylist); Dismiss ();}}
third, the ListView inside the entity class Itemtest
Package com.example.popupwimdow;/** * Entity class Itemtest * @author mmsx * Blogger Blog URL: */public clas s itemtest {private int mimageviewid;private string Mstrname;public itemtest () {}public itemtest (int imageviewid, string s Trname) {This.mimageviewid = Imageviewid;this.mstrname = StrName;} public int Getimageviewid () {return mimageviewid;} Public String Getstrname () {return mstrname;}}
Iv. Layout Files

1, Activity_main

<linearlayout xmlns:android= ""    xmlns:tools= "http// "    android:id=" @+id/linearlayoutmain "    android:layout_width=" Match_parent    " android:layout_height= "Match_parent"    android:orientation= "vertical" >    <textview        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "@string/hello_world"/ >    <linearlayout        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        android:gravity= "Right" >            <button        android:id= "@+id/buttontest2"        android:layout_width = "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Test 2"/>    </linearlayout ></LinearLayout>
2, Test_popwindow_2
<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= " Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:background = "#00ff" >    <listview        android:id= "@+id/listview1"        android:layout_width= "Match_parent        " android:layout_height= "Wrap_content"        android:layout_weight= "1" >    </listview></linearlayout >
3, Listview_item
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Android "    android:layout_width=" match_parent "    android:layout_height=" 50DP "    android: Descendantfocusability= "blocksdescendants"    android:orientation= "Horizontal" >    <imageview        Android:id= "@+id/imageview1"        android:layout_width= "50DP"        android:layout_height= "50DP"        android:src= "@drawable/bmp1"/>    <textview        android:id= "@+id/textviewtest"        android:layout_width= "0DP"        android:layout_height= "50DP"        android:layout_weight= "1"        android:gravity= "center"        android: text= "TextView"        android:textcolor= "#FF236A9C"/></linearlayout>
The main display here is to click on the button, the button below the pop-up Popupwindow. The content of Popupwindow is a ListView layout. The data source for the ListView is passed in using an abstract interface and returns the item information of the selected ListView. This is a flexible use of one of the Popupwindow, large can be based on actual needs, to make different effects.

Five, the realization effect

Project Resources Download:
Reprint please indicate the source of the blog site:
If there is a reprint does not indicate the source of the blog URL, or did not get the author's consent. The author holds copyright ownership.

Android custom Popupwindow and parameter passing and returning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.