当前位置:操作系统 > 安卓/Android >>

android下拉菜单

在指定的控件底下实现下拉框,就像登录QQ时可以在下拉框中选择自己QQ号。
实现思路:要想在指定的控件的底下实现下拉效果,首先,我们得确定要显示的起始坐标,首先得到控件的坐标,然后再由控件的坐标向下移动它本身的高度就是下拉框要显示的位置,得到显示位置就好办了。。在坐标的位置显示一个PopupWindow,布局文件为一个ListView就可以了,宽度为上面控件的宽度,高度就不用管了。代码只供参考:
 
Java代码 
/**
 * @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         调用此方法的Activity
     * @param _parentView       用来显示的PopupWindow的View
     * @param _ShowlocationView 基于那个控件显示
     * @param _data             数据源
     */ 
    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(); 
    } 
     
    /**
     * 初始化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(); 
            } 
        }); 
    } 
     
    /**
     * 显示PopupWindow窗口
     * 
     * @param popupwindow
     */ 
    public void popupWindwShowing() { 
         
        selectPopupWindow.showAtLocation(parentView, Gravity.NO_GRAVITY, location[0], location[1] + height); 
    } 
     
    /**
     * PopupWindow消失
     */ 
    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 inte易做图ce 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; 
        

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,