当前位置: 代码迷 >> Android >> 兑现在android的popupwindow中显示listview
  详细解决方案

兑现在android的popupwindow中显示listview

热度:7   发布时间:2016-05-01 16:46:16.0
实现在android的popupwindow中显示listview
之前我实现了在listview中显示图片和checkbox,具体看这儿:http://gqdy365.iteye.com/blog/992340

接下来我将上述listview添加到popupwindow窗口中。关于这个listview我就再不多说了,主要是实现popupwindow和美化popupwindow。

先看看我做截图:



1、创建一个popupwindow,并设置相应的样式。
private void popAwindow(View parent) {		if (window == null) {			LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);			View v = lay.inflate(R.layout.popupwindow, null);			v.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_view));						//初始化按钮			submit = (Button) v.findViewById(R.id.submit);			submit.setOnClickListener(submitListener);			cancel = (Button) v.findViewById(R.id.cancel);			cancel.setOnClickListener(cancelListener);						//初始化listview,加载数据。			list=(ListView)v.findViewById(R.id.lv);			MyAdapter adapter=new MyAdapter(Main.this);			list.setAdapter(adapter);			list.setItemsCanFocus(false);			list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);			list.setOnItemClickListener(listClickListener);						window = new PopupWindow(v, 500,260);		}				//设置整个popupwindow的样式。		window.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));		//使窗口里面的空间显示其相应的效果,比较点击button时背景颜色改变。		//如果为false点击相关的空间表面上没有反应,但事件是可以监听到的。		//listview的话就没有了作用。		window.setFocusable(true);//如果不设置setFocusable为true,popupwindow里面是获取不到焦点的,那么如果popupwindow里面有输入框等的话就无法输入。		window.update();		window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0);	}	OnItemClickListener listClickListener = new OnItemClickListener() {		@Override		public void onItemClick(AdapterView<?> parent, View view, int position,				long id) {			ViewHolder vHollder = (ViewHolder) view.getTag();			// 在每次获取点击的item时将对于的checkbox状态改变,同时修改map的值。			vHollder.cBox.toggle();			MyAdapter.isSelected.put(position, vHollder.cBox.isChecked());		}	};

注意:popupwindow的showAtLocation()方法的第一个参数是popupwindow显示的父窗口,可以通过:findViewById()获取当前布局。将该view作为参数传递给popupwindow。
给按钮添加监听事件:
OnClickListener submitListener = new OnClickListener() {		@Override		public void onClick(View v) {			//这儿可以写提交数据的代码。			closeWindow();		}	};	OnClickListener cancelListener=new OnClickListener(){		@Override		public void onClick(View v){			closeWindow();		}	};		private void closeWindow(){		//将每个checkbox的标记改为false,以便下次弹出window时是初始的状态。		for (int j = 0; j < MyAdapter.isSelected.size(); j++) {			MyAdapter.isSelected.put(j, false);			ViewHolder vHollder = (ViewHolder) list.getChildAt(j).getTag();			vHollder.cBox.setChecked(false);		}		//提交数据时关闭popupwindow。		if (window != null) {			window.dismiss();		}	}



在layout中新建popupwindow.xml文件,具体内容如下,主要是对window的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView    	android:id="@+id/tip"     	android:layout_width="wrap_content"     	android:layout_height="wrap_content"    	android:layout_gravity="center"    	android:textSize="18dip"    	android:background="@drawable/rounded_corners_list"    	android:text="这是一个popupWindow的例子"/>    	<!-- 如果layout_width的值为fill_parent时,居中要用android:gravity="center"-->	<ListView		android:id="@+id/lv"    	android:layout_width="fill_parent"     	android:layout_height="wrap_content"    	android:background="@drawable/rounded_corners_list"/>    <LinearLayout    	android:orientation="horizontal"    	android:layout_gravity="center"    	android:layout_width="wrap_content"    	android:layout_height="wrap_content"    	android:background="@drawable/rounded_corners_list">    	<Button     		android:id="@+id/submit"     		android:layout_width="100dip"     		android:layout_height="50dip"     		android:text="提交"/>    	<Button 	    	android:id="@+id/cancel"     		android:layout_width="100dip"     		android:layout_height="50dip"     		android:text="取消"/>    </LinearLayout></LinearLayout>


其中listView的布局的布局和实现请参考文章开头提到的另一篇文章。
新建rounded_corners_pop.xml,用于自定义窗口的样式文件,具体内容如下:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">	<solid android:color="#ffffffff" />	<stroke android:width="3dp" color="#ffff8080" />	<corners android:radius="10dp" />	<padding 		android:left="3dp"		android:top="3dp" 		android:right="3dp"		android:bottom="3dp" /></shape>

这个就可以实现圆角的样式,周围的白边是通过在白的样式上面叠加黑色的来实现的。
其他样式文件大家可以参考上面的rounded_corners_pop.xml自己写。

2、在main.xml中添加按钮,一个用于显示window,一个用于隐藏window
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:id="@+id/lmain"    >    <Button     	android:id="@+id/myButton1"     	android:layout_width="100dip"     	android:layout_height="50dip"     	android:text="显示"/>    <Button     	android:id="@+id/myButton2"     	android:layout_width="100dip"     	android:layout_height="50dip"     	android:text="隐藏"/></LinearLayout>

在activity中初始化这两个按钮,并添加监听事件:
	OnClickListener bPop = new OnClickListener() {		@Override		public void onClick(View v) {			popAwindow(v);		}	};	OnClickListener boff = new OnClickListener() {		@Override		public void onClick(View v) {			if(window!=null){				window.dismiss();			}		}	};
1 楼 heji 2011-04-12  
不错,谢谢分享。其实有这么一句话就行了new PopupWindow(v, 500,260); 其他的都很easy~~~支持楼主!共勉
2 楼 gqdy365 2011-04-14  
heji 写道
不错,谢谢分享。其实有这么一句话就行了new PopupWindow(v, 500,260); 其他的都很easy~~~支持楼主!共勉

恩,是的,但我第一次做的时候很多事实想不到的,呵呵,所以我觉得也有和我一样遇到此类问题的,分享给这些人!大侠们见笑了!
3 楼 hoozh 2011-07-01  
ggdy你好,我是android和java的初学者,我继承了PopupWindow,想把类似 listClickListener 这些接口放在继承的子类里面,这样程序主Actibity代码比较少,看起来清爽一点,但是我发现在PopupWindow的子类里没法使用findViewById函数,不知有什么办法没有?
4 楼 gqdy365 2011-07-01  
ericslegend 写道
支持楼主,下来先看看这几部分分别是怎么实现的

你试试这样可以吗?
LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);		View v = lay.inflate(R.layout.dialog_window_layout, null);		final PopupWindow p = new PopupWindow(v,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);		p.setBackgroundDrawable(new ColorDrawable(0));				TextView name=(TextView)v.findViewById(R.id.dialog_name);
  相关解决方案