浮层弹框点击切换

主页面代码

 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);
            }
        }

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值