Android drop-down menu

Source: Internet
Author: User

Implement a drop-down box under the specified control, just as you can select your QQ number in the drop-down box when logging on to QQ.
Implementation idea: to implement the pull-down effect under the specified control, first, determine the starting coordinate to be displayed, first obtain the control coordinate, the height of the widget is the position to be displayed in the drop-down box .. A PopupWindow is displayed at the position of the cursor, and the layout file is a ListView. The width is the width of the control above, so you don't need to worry about the height. The code is for reference only:
 
Java code
/**
* @ Author: hilary
* @ Date: 2011-9-14
**/
Public class PullDownList {

Private PopupWindow selectPopupWindow = null;
Private View parentView = null;
Private Activity activity = null;
Private ListView listView = null;
Private PullDownListAdapter pullListAdapter = null;
Private DeleteData deleteData = null;
ArrayList <HashMap <String, Object> data = null;

Private int [] location = new int [2];
Private int height;
Private int width;

/**
*
* @ Param _ activity the Activity that calls this method
* @ Param _ parentView the View of the PopupWindow to be displayed
* @ Param _ ShowlocationView Display Based on that control
* @ Param _ data Source
*/
Public PullDownList (Activity _ activity, View _ parentView, View _ ShowlocationView, ArrayList <HashMap <String, Object> _ data ){
Activity = _ activity;
ParentView = _ parentView;
_ ShowlocationView. getLocationOnScreen (location );
Width = _ ShowlocationView. getWidth ();
Height = _ ShowlocationView. getHeight ();
Data = _ data;
InitPopuWindow ();
}

/**
* Initialize PopupWindow
*/
Private void initPopuWindow (){
View loginwindow = (View) activity. getLayoutInflater (). inflate (R. layout. popupwindow_pull_down, null );
ListView = (ListView) loginwindow. findViewById (R. id. popupwindow_pulldown_list );
PullListAdapter = new PullDownListAdapter (activity, data );
ListView. setAdapter (pullListAdapter );
SelectPopupWindow = new PopupWindow (loginwindow, width, LayoutParams. WRAP_CONTENT, true );
SelectPopupWindow. setOutsideTouchable (true );
SelectPopupWindow. setBackgroundDrawable (new BitmapDrawable ());
// PopupWindow. setAnimationStyle (R. style. MenuAnimation );
ListView. setOnItemClickListener (new OnItemClickListener (){
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3 ){
SetSelected (String) data. get (arg2). get ("textView "));
SelectPopupWindow. dismiss ();
}
});
}

/**
* Display the PopupWindow
*
* @ Param popupwindow
*/
Public void popupWindwShowing (){

SelectPopupWindow. showAtLocation (parentView, Gravity. NO_GRAVITY, location [0], location [1] + height );
}

/**
* The PopupWindow disappears.
*/
Public void dismiss (){
SelectPopupWindow. dismiss ();
}

Public void setOnDeleteData (DeleteData _ deleteData ){
DeleteData = _ deleteData;
}

Public void setOnSelected (DeleteData _ deleteData ){
DeleteData = _ deleteData;
}

Private void setSelected (String selectedItem ){
If (deleteData! = Null ){
DeleteData. selected (selectedItem );
}
}

Private void setDelete (int position ){
If (deleteData! = Null ){
DeleteData. deletePosition (position );
}
}

Public interface DeleteData {
Public void deletePosition (int position );
Public void selected (String position );
}

Class PullDownListAdapter extends BaseAdapter {
Private ArrayList <HashMap <String, Object> list = new ArrayList <HashMap <String, Object> ();
Private Activity activity = null;
 
Public PullDownListAdapter (Activity _ activity,
ArrayList <HashMap <String, Object> _ list ){
Activity = _ activity;
List = _ list;
}
 
Public int getCount (){
Return list. size ();
}
 
Public Object getItem (int position ){
Return list. get (position );
}
 
Public long getItemId (int position ){
Return position;
}
 
Public View getView (final int position, View convertView, ViewGroup parent ){
ViewHolder holder = null;
If (convertView = null ){
Holder = new ViewHolder ();
ConvertView = LayoutInflater. from (activity). inflate (
R. layout. pull_down_list_item, null );
Holder. textView = (TextView) convertView
. FindViewById (R. id. pull_list_item_text );
Holder. imageView = (ImageView) convertView
. FindViewById (R. id. pull_list_item_image );
ConvertView. setTag (holder );
} Else {
Holder = (ViewHolder) convertView. getTag ();
}
Holder. textView. setText (CharSequence) list. get (position). get ("textView "));
// Holder. imageView. setImageDrawable (Drawable) list. get (position). get ("imageView "));
Holder. imageView. setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
SetDelete (position );
RemoveItem (position );

}
});
Return convertView;
}
 
Class ViewHolder {
TextView textView;
ImageView imageView;
}

Private void removeItem (int position ){
List. remove (position );
NotifyDataSetChanged ();
}
}
}
 

Author "hilary3113"

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.