如何替换popupwindow全屏显示中的fragment

zillachan 的BLOG
用户名:zillachan
文章数:64
评论数:32
访问量:139649
注册日期:
阅读量:5863
阅读量:12276
阅读量:367265
阅读量:1062102
[匿名]莫名其妙:
[匿名]lhi007:
51CTO推荐博文
ViewPager + Fragment 替换 TabActivity
Fragment+ViewPager 替换 TabActivity
之前首页的框架采用TabActivity+Activity的形式实现,首页页面切换时,性能消耗较大,本次修改可以大大节约页面切换性能。
下面是这次框架调整的一些具体内容
使用ViewPager作为首页的容器,替换TabActivity;
使用Fragment作为具体页面的容器,替换Activity;
将Fragment添加到ViewPager中,以实现页面切换。
ViewPager实现
ViewPager可以通过layout
&xmlns:android=&/apk/res/android&&&&&&android:layout_width=&match_parent&&&&&&android:layout_height=&fill_parent&&&&&&android:background=&#ffffff&&&&&&android:flipInterval=&30&&&&&&android:persistentDrawingCache=&animation&&&&&&android:layout_centerInParent=&true&&&&&&&&&
因为ViewPager中存放的是一系列的Fragment,所以需要一个Fragment的适配器,该适配器中保存了对首页各个Fragment的列表的引用。
public&class&MainFragmentPagerAdapter&extends&FragmentPagerAdapter&{&private&ArrayList&Fragment&&&public&MainFragmentPagerAdapter(FragmentManager&fm)&{&super(fm);&&}&public&MainFragmentPagerAdapter(FragmentManager&fm,ArrayList&Fragment&&fragments){&super(fm);&this.fragments&=&&}&&&&@Override&public&Fragment&getItem(int&arg0)&{&return&fragments.get(arg0);&}&&&&@Override&public&int&getCount()&{&return&fragments.size();&}&@Override&public&int&getItemPosition(Object&object)&{&&return&super.getItemPosition(object);&}&}&
这里主要处理两个事件,1.OnPageChange事件;2.OnTouch事件
这两个事件的任务是:
1.OnPageChange事件,当页面发生切换时,通知底部工具栏改变焦点,以实现底部工具栏和页面之间同步。
2.OnTouch事件,该事件用于分发touch事件,解决与&主页&中的Gallery横屏时事件冲突的问题。
viewPager.setOnPageChangeListener(pageChangeListener);&viewPager.setOnTouchListener(touchListener);&pageChangeListener定义如下:&private&OnPageChangeListener&pageChangeListener&=&new&OnPageChangeListener()&{&@Override&public&void&onPageSelected(int&arg0)&{&setIconSelected(arg0);&}&@Override&public&void&onPageScrolled(int&arg0,&float&arg1,&int&arg2)&{&}&@Override&public&void&onPageScrollStateChanged(int&arg0)&{&}&};&
touchListener定义如下:
private&OnTouchListener&touchListener&=&new&OnTouchListener(){&@Override&public&boolean&onTouch(View&v,&MotionEvent&event)&{&if&(currentIndex&!=&0)&{&return&false;&}&int[]&location&=&new&int[2];&homeFragment.gallery.getLocationOnScreen(location);&if&(location[0]&!=&0)&{&return&false;&}&if&(event.getRawY()&&&location[1]&&&&event.getRawY()&-&location[1]&&&homeFragment.gallery&.getHeight())&{&return&homeFragment.gallery.dispatchTouchEvent(event);&}&return&false;&}&};&
Fragment实现
Fragment的实现方式和Activity的实现方式基本相同,所需要注意的是要重写onCreateView方法。主要的内容是将Activity的onCreate方法中的内容写到Fragment的onCrateView方法中。例如SettingActivity中的onCrate方法如下:
protected&void&onCreate(Bundle&savedInstanceState)&{&super.onCreate(savedInstanceState);&setContentView(R.layout.setting);&getView();&setListener();&}&
对应的Fragment中的onCrateView方法为:
public&View&onCreateView(LayoutInflater&inflater,&ViewGroup&container,&Bundle&savedInstanceState)&{&Utils.log(&onCreateView&);&View&v&=&inflater.inflate(R.layout.setting,&container,&false);&getViews(v);&setListener();&return&v;&}&
ViewPager与底部工具栏同步
Viewpager改变通知底部工具栏索引改变:
viewPager.setOnPageChangeListener(pageChangeListener);然后再onPageSelected方法中处理
底部工具栏索引发生改变通知ViewPager切换页面
viewPager.setCurrentItem(i);
与Gallery冲突解决
为ViewPager注册Touch事件
private&OnTouchListener&touchListener&=&new&OnTouchListener(){&@Override&public&boolean&onTouch(View&v,&MotionEvent&event)&{&if&(currentIndex&!=&0)&{&return&false;&}&int[]&location&=&new&int[2];&homeFragment.gallery.getLocationOnScreen(location);&if&(location[0]&!=&0)&{&return&false;&}&if&(event.getRawY()&&&location[1]&&&&event.getRawY()&-&location[1]&&&homeFragment.gallery&.getHeight())&{&return&homeFragment.gallery.dispatchTouchEvent(event);&}&return&false;&}&};&
默认情况下ViewPager内的Gallery拖动时没有效果,可以参考android的事件传递模型,这里是在touch的时候指定某一区域的事件传递到Gallery中去,算是一个补丁吧。
&本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)
16:20:39 08:51:22 10:21:11 13:22:59 09:08:41 08:46:50 09:20:43 15:09:13 15:33:39 16:15:33 14:15:32 14:19:00 14:21:45 08:46:39 16:12:56 &&1&
&&页数 ( 1/2 ) &匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。Android popwindow跟fragment结合 左侧弹出下拉菜单 切换界面 - Android当前位置:& &&&Android popwindow跟fragment结合 左侧弹出下拉菜单Android popwindow跟fragment结合 左侧弹出下拉菜单 切换界面&&网友分享于:&&浏览:0次Android popwindow和fragment结合 左侧弹出下拉菜单 切换界面& & & 延续上一篇文章Android
实现对话框圆角功能&,在项目推进的过程当中,之前是已经用popwindow实现了点击按钮,在按钮下方弹出下拉菜单,实现了类似微信右上角加好友的功能,有兴趣的朋友,可以下载这个资源。回归主题,之前popwindow的使用,是固定在了登陆刚进去的界面,假设现在点击了左侧菜单的其他按钮,这就要求标题下方的内容必须更新所要显示的内容,一开始想都没想,就用了如下代码进行跳转:
Intent intent = new Intent(Intent.ACTION_EDIT, null);
startActivity(intent);& & & &这样做的确是能跳转到另一个显示界面,但所有的xml文件、下拉popwindow菜单,都得重新在activity重复使用,关键是跳转到这个界面,如果点击下拉菜单的其他按钮,这个时候又得重新写
Intent intent = new Intent(Intent.ACTION_EDIT, null);
startActivity(intent);& & 陷入了一个死循环,假设有10个菜单项,我就得写相同的代码10次,而且intent跳来跳去的,代码太乱,无法进行管理,非常难以忍受。所以就想着android应该有提供这样的类可以保持左侧不变,或者其他部分可以动态更新,很高兴找到了actvitygroup这个类,下载的demo运行之后,的确是能解决,但已经不推荐使用这个类,所以就决定使用fragment来进行界面的动态切换。下面是工程代码:
& & &1.主界面文件,里面存放着一个FrameLayout,可以用你想呈现的界面进行更换,也就是fragment,类似于单独的activity。
&LinearLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical&
tools:context=&.MainActivity& &
&RelativeLayout
android:layout_width=&fill_parent&
android:layout_height=&50dp&
android:background=&@color/menu_background&
android:orientation=&horizontal& &
android:id=&@+id/popBtn&
android:layout_width=&30dp&
android:layout_height=&30dp&
android:layout_marginTop=&8dp&
android:background=&@drawable/image2&
android:textColor=&@color/white& /&
android:id=&@+id/textView1&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_centerInParent=&true&
android:gravity=&center&
android:text=&测试&
android:textColor=&@color/white&
android:textSize=&30dp& /&
android:id=&@+id/menu_person&
android:layout_width=&30dp&
android:layout_height=&30dp&
android:layout_alignParentRight=&true&
android:layout_marginTop=&8dp&
android:background=&@drawable/image1& /&
&/RelativeLayout&
&span style=&font-size:18color:#ff0000;background-color: rgb(255, 255, 102);&&&strong&//这是关键点,用来动态替换frament,更换界面&/strong&&/span&
&FrameLayout
android:id=&@+id/fragment_container&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layout_weight=&1&
android:background=&@color/background&
&/LinearLayout&
& & & & & &2.定义好了主界面,就得编写你要替换的界面xml文件,就是不同的布局文件如下:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:background=&@color/background&
android:orientation=&vertical& &
&!-- 用戶狀態欄 --&
&LinearLayout
android:layout_width=&fill_parent&
android:layout_height=&25dp&
android:orientation=&horizontal& &
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:text=&测试&
android:textColor=&@color/white&
android:textSize=&12dp& /&
android:id=&@+id/price_trademargin&
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:textColor=&@color/white&
android:textSize=&12dp& /&
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:text=&测试&
android:textColor=&@color/white&
android:textSize=&12dp& /&
android:id=&@+id/price_floatpl&
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:textColor=&@color/white&
android:textSize=&12dp& /&
&/LinearLayout&
&/LinearLayout&& & & &第二个fragment2的布局文件:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:background=&@color/background&
android:orientation=&vertical&&
&!-- 用戶狀態欄 --&
&LinearLayout
android:layout_width=&fill_parent&
android:layout_height=&25dp&
android:orientation=&horizontal& &
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:text=&@string/trademargin&
android:textColor=&@color/white&
android:textSize=&12dp& /&
android:id=&@+id/price_trademargin&
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:textColor=&@color/white&
android:textSize=&12dp& /&
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:text=&@string/floatpl&
android:textColor=&@color/white&
android:textSize=&12dp& /&
android:id=&@+id/price_floatpl&
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:layout_weight=&1&
android:gravity=&center&
android:textColor=&@color/white&
android:textSize=&12dp& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal& &
android:layout_width=&@dimen/activity_price_inst_width&
android:layout_height=&@dimen/price_table_comm_height&
android:background=&@drawable/tableheader&
android:gravity=&center&
android:text=&@string/price_table_ccy&
android:textColor=&@color/price_tableheader_forcolor&
android:textSize=&@dimen/price_table_header_font_size& /&
android:layout_width=&@dimen/activity_price_chart_width&
android:layout_height=&@dimen/price_table_comm_height&
android:background=&@drawable/tableheader&
android:gravity=&center&
android:text=&&
android:textColor=&@color/white& /&
&com.android.fragmentnormal.AlloneHorizontalScrollView
android:id=&@+id/HorizontalScrollView_1&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:scrollbars=&none& &
&/com.android.fragmentnormal.AlloneHorizontalScrollView&
&/LinearLayout&
&ScrollView
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:background=&@color/background& &
&RelativeLayout
android:layout_width=&fill_parent&
android:layout_height=&match_parent&
android:background=&@color/background& &
&TableLayout
android:id=&@+id/left_table&
android:layout_width=&@dimen/price_left_table_width&
android:layout_height=&fill_parent&
android:background=&@color/background&
android:orientation=&vertical& &
&/TableLayout&
&com.android.fragmentnormal.AlloneHorizontalScrollView
android:id=&@+id/HorizontalScrollView_2&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_toRightOf=&@+id/left_table&
android:background=&@color/background& &
&TableLayout
android:id=&@+id/data_table&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:background=&@color/background&
android:orientation=&vertical& &
&/TableLayout&
&/com.android.fragmentnormal.AlloneHorizontalScrollView&
&/RelativeLayout&
&/ScrollView&
&/LinearLayout&
& & & 3.编写主类,对你的两个fragment进行管理,决定一开始显示哪个,点击按钮之后切换显示哪个fragment。
package com.android.
import android.os.B
import android.support.v4.app.F
import android.support.v4.app.FragmentA
import android.support.v4.app.FragmentM
import android.support.v4.app.FragmentT
import android.util.DisplayM
import android.view.MotionE
import android.view.V
import android.view.View.OnClickL
import android.view.ViewG
import android.widget.B
import android.widget.PopupW
public class MainActivity extends FragmentActivity
Fragment f1,f2,f3 ;
private PopupWindow popupW
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager= getSupportFragmentManager() ;
FragmentTransaction transaction = manager.beginTransaction() ;
f1 = new Fragment1();
f2 = new Fragment2();
f3 = new Fragment3();
pop = (Button) findViewById(R.id.popBtn);
pop.setOnClickListener(popClick);
transaction.add(R.id.fragment_container, f2);
// 点击弹出左侧菜单的显示方式
OnClickListener popClick = new OnClickListener() {
public void onClick(View v) {
/*Toast toast = Toast.makeText(MainActivity.this, &这是一个代图片的Toast!&, Toast.LENGTH_LONG);
toast.show();*/
getPopupWindow();
// 这里是位置显示方式,在按钮的左下角
popupWindow.showAsDropDown(v);
* 创建PopupWindow
protected void initPopuptWindow() {
// 获取自定义布局文件pop.xml的视图
View popupWindow_view = getLayoutInflater().inflate(R.layout.pop, null,
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// 创建PopupWindow实例,200,150分别是宽度和高度
popupWindow = new PopupWindow(popupWindow_view, 350,
ViewGroup.LayoutParams.MATCH_PARENT, true);
// popupWindow.setWidth(350);
// popupWindow.setHeight(dm.heightPixels * 20 / 2);
// 设置动画效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
// 点击其他地方消失
popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow =
// pop.xml视图里面的控件
initOpenMenuItem(popupWindow_view);
initOpenMenuOther(popupWindow_view);
initOpenPosition(popupWindow_view);
日17:35:24,
author:qiulinhe
添加对于开仓单的监听和界面增加
private void initOpenPosition(View popupWindow_view) {
DrawableCenterTextView menu_open = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_open);
// pop.xml视图里面的控件触发的事件
menu_open.setOnClickListener(new OnClickListener() {
&span style=&font-size:18color:#ff0000;&&&strong&FragmentTran//对fragment进行跳转控制。&/strong&&/span&
public void onClick(View v) {
&strong&&span style=&font-size:18color:#ff0000;&&transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_container, f1);//把f1的界面替换container
popupWindow.dismiss();&/span&&/strong&
&strong&&span style=&font-size:18color:#33cc00;&&//初始化左侧下拉菜单的按钮&/span&&/strong&
private void initOpenMenuOther(View popupWindow_view) {
DrawableCenterTextView menu_open = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_open);
DrawableCenterTextView menu_order = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_order);
DrawableCenterTextView menu_orderhis = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_orderhis);
DrawableCenterTextView menu_closehis = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_closehis);
DrawableCenterTextView menu_summary = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_summary);
DrawableCenterTextView menu_pricewarning = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_pricewarning);
DrawableCenterTextView menu_news = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_news);
DrawableCenterTextView menu_margin = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_margin);
DrawableCenterTextView menu_message = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_message);
DrawableCenterTextView menu_syssett = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_syssett);
DrawableCenterTextView menu_about = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_about);
private void initOpenMenuItem(View popupWindow_view) {
DrawableCenterTextView menu_price = (DrawableCenterTextView) popupWindow_view
.findViewById(R.id.menu_price);
menu_price.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
* 获取PopupWindow实例
private void getPopupWindow() {
if (null != popupWindow) {
popupWindow.dismiss();
initPopuptWindow();
& & & & 4.frament1的代码如下:
package com.android.
import android.os.B
import android.support.v4.app.F
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.ArrayA
import android.widget.ListA
import android.widget.T
public class Fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
&span style=&color:#ff0000;&&&strong&View vi = inflater.inflate(R.layout.activity_openposition, container,false);&/strong&&/span&
//实现下方长按弹出listview,日10:01:19
ActionSlideExpandableListView list = (ActionSlideExpandableListView)vi.findViewById(R.id.list);
list.setAdapter(buildDummyData());
list.setItemActionListener(
new ActionSlideExpandableListView.OnActionClickListener() {
public void onClick(View listView, View buttonview,
int position) {
String actionName = &&;
if (buttonview.getId() == R.id.duichong) {
actionName = &duichong&;
Toast.makeText(
getActivity(),
&你点击了对冲按钮&,
Toast.LENGTH_SHORT).show();
}, R.id.duichong);
* qiulinhe
* 日10:02:03
* 实现开仓单下方长按,弹出两个按钮功能
public ListAdapter buildDummyData() {
final int SIZE = 20;
String[] values = new String[SIZE];
for (int i = 0; i & SIZE; i++) {
values[i] = &單號&;
return new ArrayAdapter&String&(getActivity(), R.layout.expandable_list_item,
R.id.text, values);
& &5.fragment2的代码如下:
package com.android.
import android.os.B
import android.support.v4.app.F
import android.view.LayoutI
import android.view.V
import android.view.ViewG
public class Fragment2 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.frament2, container,false);
& & &代码基本完毕,如果想要demo可以到这个链接:csdn资源可以下载点击打开链接,其实原理很简单,就是通过fragment来管理切换界面。
运行界面如下:
版权声明:本文为博主原创文章,未经博主允许不得转载。
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有自定义一个简单的PopupWindow - 博客频道 - CSDN.NET
mr_zdd的专栏
本人也是菜鸟,正在学习摸索中,有好的就分享
分类:android PopupWindow
最近闲的没事做就写了一下PopupWindow希望对有些人有点帮助
照常先看一下完成后的结果(界面比较难看就不要吐槽了)
点击地理位置然后弹出的PopupWindow,数据我写死了但是可以根据你们的需求自己改,或者通过网络获取数据。我是通过listView进行展示的你们也可以改成表格布局,具体的实现代码如下:
PopupWindow的弹出框的整体布局(listView)fragment_popup:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&
android:id=&@+id/pop_path&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&&
&/ListView&
&/LinearLayout&
listview要加载的item:pop_list_adapter.xml
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&
android:id=&@+id/item_content&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:textSize=&18dp&/&
&/LinearLayout&
listview的适配器:PopAdapter
public class PopAdapter extends BaseAdapter {
private List&String& list;
private Context context;
public PopAdapter(List&String& list, Context context) {
this.list = list;
this.context = context;
public int getCount() {
return list.size();
public Object getItem(int position) {
return position;
public long getItemId(int position) {
return position;
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.pop_list_adapter, null);
viewHolder.item_content = (TextView) convertView.findViewById(R.id.item_content);
convertView.setTag(viewHolder);
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.item_content.setText(list.get(position));
return convertView;
private class ViewHolder {
private TextView item_content;
写一个MyPopupWindow类继承PopupWindow:
public class MyPopuWindow extends PopupWindow {
private View contentView;
private ListView lv_pop;
private List&String& paths;
private Context context;
public MyPopuWindow(final Activity context) {
this.context = context;
//获得 LayoutInflater 的实例
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.fragment_popup, null);
//获取屏幕的宽高
int h = context.getWindowManager().getDefaultDisplay().getHeight();
int w = context.getWindowManager().getDefaultDisplay().getWidth();
this.setContentView(contentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.MATCH_PARENT);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
this.setOutsideTouchable(true);
// 刷新状态
this.update();
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable();
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(dw);
// 设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimationPreview);
initData();
private void initData() {
paths = new ArrayList&&();
paths.add(&北京&);
paths.add(&上海&);
paths.add(&广州&);
paths.add(&天津&);
paths.add(&大连&);
paths.add(&长春&);
paths.add(&济南&);
paths.add(&青岛&);
paths.add(&无锡&);
paths.add(&郑州&);
paths.add(&宁波&);
paths.add(&厦门&);
lv_pop = (ListView) contentView.findViewById(R.id.pop_path);
lv_pop.setAdapter(new PopAdapter(paths, context));
lv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView&?& parent, View view, int position, long id) {
Toast.makeText(context, paths.get(position), Toast.LENGTH_SHORT).show();
showPopupWindow(view);
* 显示popupWindow
* @param parent
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
this.dismiss();
接下来就是调用PopupWindow显示了。actionPath:是你的组件也就是我的地理位置
myPopuWindow= new MyPopuWindow(getActivity());
myPopuWindow.showPopupWindow(actionPath);
好了大概的一个代码就是这样了希望对你们有用。
排名:千里之外
(0)(1)(1)(2)(1)(1)(0)(1)(1)(1)(1)(1)(1)(1)(1)(1)(1)(0)(1)(1)(1)(1)(1)(1)(0)}

我要回帖

更多关于 popupwindow 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信