popupWindow的用法(1)

可以参考下这个http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0702/1627.html讲的挺好

我这里主要实现的是那种类似两个listView并在一起的那种

case R.id.tv_school:
            doschool();
			break;     //驾校
case R.id.tv_region:
            doregion();
                        break;    //地区
private void doregion() {
		tv_region.setTextColor(getResources().getColor(R.color.discount_tab));
		tv_school.setTextColor(getResources().getColor(R.color.black));
		iv_cursor1.setVisibility(View.INVISIBLE);
		iv_cursor2.setVisibility(View.VISIBLE);                            //这个是上面tab的变化
		
	if (pop!= null && pop.isShowing()) {  
            pop.dismiss();                                                         //当popupWindow存在时,在按他上面的会先取消popupWindow
        } else {  
        	schoolSel = -1;
	        areaSel = -1;                                                      //分别是左右两个listView选择的position
			                                                           
            /***    弹出自定义的菜单        ***/  
	    View layout = getLayoutInflater().inflate(R.layout.drivingschool_school,null);     //这个布局下面有
            lv_school=(ListView) layout.findViewById(R.id.lv_region);
            lv_area=(ListView) layout.findViewById(R.id.lv_school);
            
            DrivingSchoolNameAdapter schoolAdapter=new DrivingSchoolNameAdapter(this, getAdapterFromNameArea(cateAreaist));//为右边的listView设置adapter
            lv_area.setAdapter(schoolAdapter);
            lv_area.setOnItemClickListener(itemAreaSchoolClickListener);                                                   //右边项的点击事件
            pop = new PopupWindow(layout,WorkApplication.getInstance().getScreenWidth(),WorkApplication.getInstance().getScreenHeight());  宽高设置
            
            
            ColorDrawable cd = new ColorDrawable(getResources().getColor(R.color.main_white));  
            pop.setBackgroundDrawable(cd);  //颜色设置,应该只包含左边没有被点击的地方
            pop.update();  
            pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);  
            pop.setTouchable(true);           // 设置popupwindow可点击  
            pop.setOutsideTouchable(true);    // 设置popupwindow外部可点击  
            pop.setFocusable(true);           //获取焦点  
            
            pop.showAsDropDown(tv_school,5,5);    //相对某控件的位置
            pop.setTouchInterceptor(new View.OnTouchListener() {  
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {  
                        pop.dismiss();                                             //当点击pop以外的位置,pop不为空的话会先使pop为空
                        return true;   
                    }  
                    return false;  
                }  
            });  
            
            if(cateAreaist == null){
            	HashMap<String , Object> map = new HashMap<String , Object>();
            	map.put("type", 1);
            	Task task = new Task(TaskType.TS_GET_AREA_SCHOOL_LIST, map);       //这段是连接服务器的,不用管
            	MainService.newTask(task);
            	ProgressDialogUtil.showMsgDialog(this);
            }
        }
	}
	
	private List<String> getAdapterFromNameArea(List<DSNameAreaInfo> obj){
		if(obj == null){
			return null;
		}
		
		List<String> arr = new ArrayList<String>();
		for (DSNameAreaInfo item : obj) {
			arr.add(item.getName());
		}
		
		return arr;
	}
这里这列举其中一种,另外一种是一样的。
private  OnItemClickListener itemAreaSchoolClickListener=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {			
			
			switch (parent.getId()) {
			case R.id.lv_school:
				schoolSel = -1;
				areaSel = position;       //点击左边项
				
				DrivingSchoolNameAdapter dsschoolNameAdapter=new DrivingSchoolNameAdapter(DrivingschoolSchoolAreaActivity.this, cateAreaist.get(position).getAreas());
	            lv_school.setAdapter(dsschoolNameAdapter);
	            lv_school.setOnItemClickListener(itemAreaSchoolClickListener);
				break;
			case R.id.lv_region:               //点击右边项
				schoollistPage = 1;
				schoollist.clear();
				dsschoolAdapter.notifyDataSetChanged();
				schoolSel = position;
				pop.dismiss();
				loadschool(true, false);
				break;
			default:
				break;
			}
		}
	};<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    >
    <ListView 
        android:id="@+id/lv_school"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:listSelector="@color/gainsboro" 
        ></ListView>
     <ListView 
        android:id="@+id/lv_region"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:listSelector="@android:color/white" 
        android:background="@color/gainsboro"
        ></ListView>
</LinearLayout>


drivingschool_school布局


讲的不是很详细。。。


posted @ 2015-09-24 21:23  LeeJuly  阅读(103)  评论(0编辑  收藏  举报