Android programming of the ICS Drop-down menu Popupwindow Implementation approach (with source download) _android

Source: Internet
Author: User
Tags stub

The example of this article describes the implementation of the ICS Pull-down menu Popupwindow for Android programming. Share to everyone for your reference, specific as follows:

The screenshot of the running effect is as follows:

Right this is the drop down menu, see some place called his ICS-style pull-down menu, ouch, yes!

Let's talk about the implementation principle:

This kind of menu is actually a pop-up menu, so we think of the Android Popupwindow class, and give him a view in the bounce is not OK.

Popupwindow's use is simple, too.

Main methods:

Step 1.new An instance out, we use this construct method, we can

Copy Code code as follows:
Public Popupwindow (View contentview, int width, int height)

3 parameters you want to display view, display width, display height
Use instance:

View view = Layoutinflater.from (context). Inflate (R.layout.popmenu, null);
Popupwindow = new Popupwindow (view, layoutparams.wrap_content);

Very simple, needless to say, if you do not understand these 2 sentences to see the basis of the first.

Step 2: Show it

Copy Code code as follows:
Popupwindow.showasdropdown (parent,10, 10);

Parameter: Which view to attach to (should be based on this parent to determine the popup position), relative to Parent's x-axis offset, relative to Parent's y-axis offset

These 2 steps are all right. He bounced out and noticed a few important details below.

Note that the following 3 is done to make the area outside of the click Popuwindow able to close it.

Make it clustered
popupwindow.setfocusable (true);
Set to allow popupwindow.setoutsidetouchable to disappear
(true);
Refresh status (must refresh otherwise invalid)
popupwindow.update ();

The following is to enable the Popuwindow to be turned off by pressing the return button

This is to click "Back" to make it disappear, and it will not affect your background (very magical) 
popupwindow.setbackgrounddrawable (New bitmapdrawable ());
I don't know why I found it on the Internet.

OK above the introduction of the principle of use, know the principle that everyone can do their own pop-up menu, right.

Here to share my encapsulated Popmenu class

Package com.tszy.wight;
Import java.util.ArrayList;
Import Android.content.Context;
Import android.graphics.drawable.BitmapDrawable;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.ViewGroup.LayoutParams;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.BaseAdapter;
Import Android.widget.ListView;
Import Android.widget.PopupWindow;
Import Android.widget.TextView;
Import COM.TSZY.WATERGATE.R;
 public class Popmenu {private arraylist<string> itemList;
 private context;
 Private Popupwindow Popupwindow;
 Private ListView ListView;
 Private Onitemclicklistener listener;
  Public Popmenu {//TODO auto-generated constructor stub this.context = context;
  ItemList = new arraylist<string> (5);
  View view = Layoutinflater.from (context). Inflate (R.layout.popmenu, NULL);
  Set ListView ListView = (ListView) View.findviewbyid (R.id.listview); Listview.setadapter (new Popadapter ());
  Popupwindow = new Popupwindow (view, layoutparams.wrap_content); Popupwindow = new Popupwindow (view, Context.getresources (). Getdimensionpixelsize (r.dimen.popmenu_width), LayoutPa Rams.
  Wrap_content);
 This is to click "Back" to make it disappear, and it will not affect your background (very magical) popupwindow.setbackgrounddrawable (New bitmapdrawable ());
  //Set menu item click Listener public void Setonitemclicklistener (Onitemclicklistener listener) {//this.listener = listener;
 Listview.setonitemclicklistener (listener);
 }//Bulk add menu item public void Additems (string[] items) {for (String S:items) Itemlist.add (s);
 ///Single Add menu item public void AddItem (String item) {Itemlist.add (item); //dropdown Pop menu parent right lower corner public void Showasdropdown (View parent) {Popupwindow.showasdropdown (parent, 10,//guarantee Ruler
  Inch is based on the pixel density of the screen context.getresources (). Getdimensionpixelsize (R.dimen.popmenu_yoff));
  Make it clustered popupwindow.setfocusable (true);
  Set to allow Popupwindow.setoutsidetouchable to disappear (true); Refresh Status POPUPWIndow.update ();
 ///Hide menu public void Dismiss () {Popupwindow.dismiss (); }//Adapter Private Final class Popadapter extends Baseadapter {@Override public int getcount () {//TODO Auto-gene
  Rated method Stub return Itemlist.size (); @Override public Object getitem (int position) {//TODO auto-generated Method Stub return Itemlist.get (Positio
  n);
  @Override public long getitemid (int position) {//TODO auto-generated method stub return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {//TODO auto-generated method s
   Tub Viewholder Holder;
    if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate (R.layout.pomenu_item, NULL);
    Holder = new Viewholder ();
    Convertview.settag (holder);
   Holder.groupitem = (TextView) Convertview.findviewbyid (R.id.textview);
   else {holder = (Viewholder) convertview.gettag (); } holder.groupItem.setText (Itemlist.get (positION));
  return convertview;
  Private Final class Viewholder {TextView groupitem;

 }
 }
}

This class refers to a lot of resource files, layout files, which can be achieved by themselves, I will not post.

Specific code This site download the address is as follows:

Project Source

Analog server-side source code

I hope this article will help you with the Android program.

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: info-contact@alibabacloud.com 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.