使用dialogfragment显示多张图片

布局使用了databinding
加载图片使用了glide
别忘了网络权限哦:

<uses-permission android:name="android.permission.INTERNET"/>

效果图:

主代码:

PreviewImageDialog.java

/** * 图片预览 */
public class PreviewImageDialog extends DialogFragment {

    private List<String> previewPics;
    private PreviewBinding binding;
    private PreviewAdapter adapter;
    private static final String PIC_PATH = "pic_path";


    public static PreviewImageDialog newInstance(ArrayList<String> list) {
        PreviewImageDialog previewDialog = new PreviewImageDialog();
        Bundle args = new Bundle();
        args.putStringArrayList(PIC_PATH, list);
        previewDialog.setArguments(args);
        return previewDialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x4c000000));
        binding = DataBindingUtil.inflate(inflater, R.layout.activity_preview_image, container, false);
        initView();
        return binding.getRoot();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        previewPics = new ArrayList<String>();
        previewPics = getArguments().getStringArrayList(PIC_PATH);
        LogUtil.HDLog("预览图片: " + previewPics.size());
    }

    public void initView() {
        adapter = new PreviewAdapter(previewPics, getActivity());
        binding.previewPager.setAdapter(adapter);
        binding.previewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        binding.previewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, WindowManager.LayoutParams.MATCH_PARENT);
    }
}

适配器文件:

在适配器里根据图片的url生成一个Imageview对象,加入到viewpager里,并使用glide加载图片。

PreviewAdapter.java

/** * Created by ${} on 2016/9/8. * <p/> * 图片阅览页面 pagerAdapter */
public class PreviewAdapter extends PagerAdapter {

    private List<String> list;
    private Context context;

    public PreviewAdapter(List<String> list, Context context) {
        this.list = list;
        this.list = new ArrayList<>();
        this.list.addAll(list);
        this.list.remove(0);//去除第一个
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        Glide.with(context).load(list.get(position)).diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="PreviewBinding">

    </data>

    <RelativeLayout  android:layout_width="match_parent" android:layout_height="match_parent">

        <android.support.v4.view.ViewPager  android:id="@+id/preview_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:background="#66000000">

        </android.support.v4.view.ViewPager>

    </RelativeLayout>
</layout>

使用方法:

 /** * 预览大图 */
    private void preBigImage() {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag(Constant.PREVIEW_PICTURE_DIALOG_FRAMENTS);
        if (prev != null) {
            ft.remove(prev);
        }

        ft.addToBackStack(null);

    //构造PreviewImageDialog,传入图片的url集合即可
    //private ArrayList<String> storyImages;

        PreviewImageDialog previewDialog = new PreviewImageDialog().newInstance(storyImages);
//调用show方法显示dialogfragment
        previewDialog.show(ft, Constant.PREVIEW_PICTURE_DIALOG_FRAMENTS);
    }

demo下载:

dialogfragment加载多张图片 - 下载频道 - CSDN.NET
http://download.csdn.net/detail/baidu_31093133/9629184

你可能感兴趣的:(dialog,Fragment)