用全屏DialogFragment代替Activity,结合ViewPager展示图片

最近在知乎上看到一篇很不错的分享,关于Fragment的一些用法和开发经验,其中提倡用全屏的DialogFragment代替Activity处理一些特殊的场景,比如用户点击item触发打开一些内容详情页,相册点击小图片时会将图片全屏放大展示…等等的这些场景都可以考虑用全屏的DialogFragment来代替Activity来代替进行视图的展示。

由于具体的代码逻辑非常简单,我就直接上代码了,注释也写得很清楚,你肯定能看懂:

public class FullScreenDlgFragment extends DialogFragment implements View.OnClickListener{
    private int mClickItem;//对应显示ViewPager子项的位置
    private List<String> mListImgUrls;
    private ViewPager mViewPager;
    private Integer[] mImgIds;//本地图片资源ID
    private Dialog mDialog;
    public static final String TAG_NAME=AlertDlgFragment.class.getName();
    private Context mContext;

    //即学即用的工厂方法
    public static FullScreenDlgFragment newInstance(Context context, Integer[] imgIds, int clickItem) {
        Bundle args = new Bundle();
        FullScreenDlgFragment fragment = new FullScreenDlgFragment();
        fragment.setArguments(args);
        fragment.mContext = context;
        fragment.mImgIds = imgIds;
        fragment.mClickItem = clickItem;
        return fragment;
    }

      //由ViewPager来响应点击
    @Override
    public void onClick(View v) {
        this.dismiss();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //设置Dialog样式
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog_fill);
        mDialog=new Dialog(mContext);
        //去标题栏
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        initView();
        return mDialog;
    }

    private void initView() {
        //将Dialog设置全屏!!!
        setDlgParams();

        mViewPager = new ViewPager(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mViewPager.setLayoutParams(params);
        mViewPager.setBackgroundColor(0xFF000000);
        initViewPager();
        mDialog.setContentView(mViewPager);
    }

    private void setDlgParams() {
        ViewGroup.LayoutParams lay = mDialog.getWindow().getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        mDialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        Rect rect = new Rect();
        View view = mDialog.getWindow().getDecorView();
        view.getWindowVisibleDisplayFrame(rect);
        lay.height = dm.heightPixels - rect.top;
        lay.width = dm.widthPixels;
    }

    private void initViewPager() {
        if (mImgIds != null && mImgIds.length > 0) {
            List<View> listImgs = new ArrayList<>();
            for (int i = 0; i < mImgIds.length; i++) {
                ImageView iv = new ImageView(mContext);
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                iv.setLayoutParams(params);
                listImgs.add(iv);
                iv.setOnClickListener(this);
                iv.setImageResource(mImgIds[i]);
                // 加载网络图片
                // BitmapHelper.getInstance(mContext).display(iv,
                // mListImgUrls.get(i));
            }
            if (listImgs.size() > 0) {
                MyPagerAdapter pageAdapter = new MyPagerAdapter(listImgs);
                mViewPager.setAdapter(pageAdapter);
                mViewPager.setCurrentItem(mClickItem);
            }
        }
    }
}

styles.xml

<style name="CustomDialog_fill" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowBackground">@android:color/white</item>
</style>

具体使用只需:

FullScreenDlgFragment.newInstance(this,new Integer[]{R.mipmap.p1,R.mipmap.p2,R.mipmap.p3,R.mipmap.p4},2).show(getFragmentManager(),AlertDlgFragment.TAG_NAME);

特别鸣谢,以下两篇文章:
【干货】Dialog的高冷用法
面条在知乎上“关于 Android,用多个 activity,还是单 activity 配合 fragment?”的回答

你可能感兴趣的:(android,viewpager,dialog,Fragment,全屏Dialog)