self-confidence,the source of all the power

导航

popup menu案例,无说明只代码

效果图:

布局文件, 展示列表的容器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bgcolor">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/White"
        android:dividerHeight="1dp"
        android:layout_marginTop="1dp">
    </ListView>
</LinearLayout>

每一列的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bgcolor"
    >
        <TextView
            android:id="@+id/tvItem"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tvStars"
            android:text="32宀?
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:layout_marginBottom="1dp"
            android:textColor="@color/bg_deep_color"
            android:textSize="17sp"
            android:paddingTop="5dp"
            android:background="@color/White"
        />

</LinearLayout>

java 代码实现popup menu的类

public class PopMenu {
    private ArrayList<String> itemList;
    private Context context;
    private PopupWindow popupWindow ;
    private ListView mLv;
     
    @SuppressWarnings("deprecation")
    public PopMenu(Context context) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.listview_popup_menu, null);
        itemList = new ArrayList<String>();
        //璁剧疆 listview
        mLv = (ListView)view.findViewById(R.id.lv);
         
        mLv.setAdapter(new PopAdapter());
        popupWindow = new PopupWindow(view, 200, LayoutParams.WRAP_CONTENT);
        popupWindow = new PopupWindow(view, context.getResources().getDimensionPixelSize(R.dimen.popmenu_width), LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
         
    }
     
    //璁剧疆鑿滃崟椤圭偣鍑荤洃鍚櫒
    public void setOnItemClickListener(android.widget.AdapterView.OnItemClickListener listener) {
        mLv.setOnItemClickListener(listener);
    }
 
    //鎵归噺娣诲姞鑿滃崟椤?
    public void addItems(String[] items) {
        for (String s : items)
            itemList.add(s);
    }
    
    public void addItems(ArrayList<String> ArrList) {
        // TODO Auto-generated method stub
        itemList.addAll(ArrList);
    }
 
    //鍗曚釜娣诲姞鑿滃崟椤?
    public void addItem(String item) {
        itemList.add(item);
    }
     
    public Object getItem(int position) {
        return itemList.get(position);
    }
 
    //涓嬫媺寮?寮瑰嚭 pop鑿滃崟 parent 
    public void showAsDropDown(View parent) {
        //淇濊瘉灏哄鏄牴鎹睆骞曞儚绱犲瘑搴︽潵鐨?
        popupWindow.showAsDropDown(parent,2,0);
        // 浣垮叾鑱氶泦
        popupWindow.setFocusable(true);
        // 璁剧疆鍏佽鍦ㄥ鐐瑰嚮娑堝け
        popupWindow.setOutsideTouchable(true);
        //鍒锋柊鐘舵€?
        popupWindow.update();
    }
     
    //闅愯棌鑿滃崟
    public void dismiss() {
        popupWindow.dismiss();
    }
 
    // 閫傞厤鍣?
    private final class PopAdapter extends BaseAdapter {
 
        public int getCount() {
            return itemList.size();
        }
         
        public Object getItem(int position) {
            return itemList.get(position);
        }
         
        public long getItemId(int position) {
            return position;
        }
         
        public View getView(int position, View convertView, ViewGroup parent) {
             
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_popup_menu, null);
            TextView groupItem = (TextView) convertView.findViewById(R.id.tvItem);
//            TextView imgTV = (TextView) convertView.findViewById(R.id.imgTV);
//            if (position == itemList.size() - 1) {
//                imgTV.setVisibility(View.GONE);
//            }
            groupItem.setText(itemList.get(position));
            return convertView;
        }
    }

    

让popup menu在适当地方展示出来的方法

mHospitalPopMenu = new PopMenu(mContext);
        mHospitalPopMenu.addItems(mHospitalArrList);
        mHospitalPopMenu.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               TextView tvItem = (TextView)view.findViewById(R.id.tvItem);
               mTvHospital.setText(tvItem.getText());
               mHospitalPopMenu.dismiss();
               }
        });
mHospitalArrList = new ArrayList<String>();
String s[] = new String[]{"鍗庤タ鍖婚櫌","鎴愰兘鍐涘尯鍖婚櫌","鍥涘窛楠ㄧ鍖婚櫌"};
        for (String s1: s){
            mHospitalArrList.add(s1);
        }
mCityPopMenu.showAsDropDown(mTvCity); //在mTvCity控件下show 弹出菜单

 

posted on 2015-05-06 00:13  漩涡鸣人  阅读(290)  评论(0编辑  收藏  举报