主页面代码
int [] img = {R.mipmap.bg_float1,R.mipmap.bg_float2,R.mipmap.bg_float3};
ImgDialog imgDialog = ImgDialog.newInstance(img);
imgDialog.show(getSupportFragmentManager(),"MainActivity",true);
dialog布局xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Dialog代码
public class ImgDialog extends BaseDialog {
private static final String DATA ="DATA";
private int[] mData;
private FrameLayout footview;
private int mCurrent = 0;
//传参数
public static ImgDialog newInstance(int[] imgs){
ImgDialog imgDialog = new ImgDialog();
imgDialog.setCancelable(false);
Bundle bundle = new Bundle();
bundle.putIntArray(DATA,imgs);
imgDialog.setArguments(bundle);
return imgDialog;
}
@Override
protected int getLayoutRes() {
return R.layout.dialog_img;
}
@Override
protected void initView(View view, Bundle savedInstanceState) {
footview = view.findViewById(R.id.footview);
//接收参数
if (getArguments()!=null){
mData = getArguments().getIntArray(DATA);
if (mData==null)return;
//根据数组长度动态创建imageview
for (int data :mData){
ImageView imageView = new ImageView(getContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(data);
imageView.setVisibility(View.INVISIBLE);//首先全部隐藏imageview
footview.addView(imageView);//把imageview加入到父布局FrameLayout
}
footview.getChildAt(0).setVisibility(View.VISIBLE);//使最上面的imageview显示(默认显示一张)
}
//点击imageview开始切换图片
footview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mData==null)return;
//mCurrent默认是0,
if (mCurrent<mData.length-1){
mCurrent++;
switchView(mCurrent);//第一次经过mCurrent++后,switchview(1)
}else {
dismiss();
}
}
});
}
//第一次position是1
private void switchView(int position) {
所以第一次除了i==1显示,其他的全部隐藏(三张图片换着显示隐藏,并没有删除,dialog关闭后全部不显示)
for (int i=0;i<footview.getChildCount();i++){
if (position==i){
footview.getChildAt(i).setVisibility(View.VISIBLE);
}else {
footview.getChildAt(i).setVisibility(View.INVISIBLE);
}
}
}
}