目测在一个popwindow 里的某一个控件的点击事件里就不能再继续的show了
所以我在一个布局文件里写了两个,也就是两个popwindow 贴上代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" >
下边是布局里的第一个部分 <RelativeLayout android:id="@+id/rl_shop" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shops"> <ImageView android:id="@+id/img_closem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/closem" android:layout_marginRight="15dp" android:layout_marginTop="30dp" android:layout_alignParentRight="true"/> <RelativeLayout android:layout_marginLeft="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_centerVertical="true" > <ImageView android:id="@+id/img_recharge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:background="@drawable/rechargem" /> <ImageView android:id="@+id/seeds" android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/img_recharge" android:background="@drawable/seedm" /> </RelativeLayout> </RelativeLayout>
下边是布局里的第二个部分 <RelativeLayout android:id="@+id/rl_seed" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/seed_bg"> <ImageView android:id="@+id/img_seedclose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/closem" android:layout_marginRight="15dp" android:layout_marginTop="30dp" android:layout_alignParentRight="true"/> </RelativeLayout> </RelativeLayout>
解析布局 这里就不多说了(只需要解析这一个就可以)
contentView = LayoutInflater.from(this).inflate( R.layout.activity_shop_dialog, null);
java 代码 public class MelonsActivity extends AppCompatActivity { private TextView mtv_melons; private ImageView mimg_backg; private ImageView mimg_shop; private PopupWindow popupWindow; private RelativeLayout rl_shop; private RelativeLayout rl_seed; private View contentView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_melons); findView(); ButterKnife.bind(this); UiUtils.changStatus(this); } @Override protected void onStart() { super.onStart(); 这里调用下方方法 你们用不到 我就没有调用 } public void findView(){ mimg_shop=(ImageView)findViewById(R.id.img_shop); } public void onClick(){ mtv_melons.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { UiUtils.popUpPayDailog(MelonsActivity.this, 0.6, 1.0); } }); mimg_shop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showPopupWindow(view);
这里点击会有两个 其中一个可见 另一个不可见 rl_shop.setVisibility(View.VISIBLE); rl_seed.setVisibility(View.INVISIBLE); } }); } // TODO: 2017/1/20 商店弹出的pop private void showPopupWindow(View view) { // 一个自定义的布局,作为显示的内容 contentView = LayoutInflater.from(this).inflate( R.layout.activity_shop_dialog, null); // 设置按钮的点击事件(第一个pop) rl_shop=(RelativeLayout)contentView.findViewById(R.id.rl_shop); ImageView img_seeds=(ImageView) contentView.findViewById(R.id.seeds); //种子点击事件(第二个pop) rl_seed=(RelativeLayout)contentView.findViewById(R.id.rl_seed); // TODO: 2017/1/20 popwindow点击关闭-shop img_close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(popupWindow!=null){ popupWindow.dismiss(); } } }); // TODO: 2017/1/20 种子-shop img_seeds.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {这是在第一个popwindow 里的点击事件,点击的时候第一隐藏 第二个显示 就是这样 rl_shop.setVisibility(View.INVISIBLE); rl_seed.setVisibility(View.VISIBLE); } }); popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i("mengdd", "onTouch : "); return false; } }); int[] location = new int[2]; // 设置好参数之后再show //popupWindow.showAsDropDown(view); popupWindow.showAtLocation(view, Gravity.CENTER, location[0]-popupWindow.getWidth()/2, location[1]); } 总结:就是一个布局文件,写好两个pop布局,解析的时候全部解析,点击事件处理,一个可见一个不可见,就这么简单,解决一个pop里show另一pop 的问题