android下拉菜單 – 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 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(); 
        } 
    } 

 

作者“hilary3113”

發佈留言