public class PopShowManyBigImage extends BasePopupWindow {
private TextView tvNUm;
private MyViewPager viewPager;
private List<PhotoView> mViews = new ArrayList<>();
public PopShowManyBigImage(Context context, List<String> imgUrl, int showIndex) {
super(context);
if (showIndex < 0 || showIndex >= imgUrl.size()) {
throw new ArrayIndexOutOfBoundsException();
}
for (String url : imgUrl) {
PhotoView photoView = new PhotoView(context);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
photoView.setLayoutParams(layoutParams);
mViews.add(photoView);
}
viewPager.setAdapter(new ImagePageAdapter(context, imgUrl, mViews));
//设置初始显示Index(需要在setAdapter之后)
viewPager.setCurrentItem(showIndex);
tvNUm.setText(showIndex + 1 + "/" + imgUrl.size());
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.e("viewPager", "onPageSelected:position: " + position);
tvNUm.setText(position + 1 + "/" + imgUrl.size());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public View onCreateContentView() {
View view = createPopupById(R.layout.pop_show_many_img);
tvNUm = view.findViewById(R.id.tv_num);
viewPager = view.findViewById(R.id.view_page);
return view;
}
}
pop布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">
<com.qdzr.westcoast.view.MyViewPager
android:id="@+id/view_page"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:text=""
android:textColor="@color/white"
android:textSize="16sp" />
</RelativeLayout>
pagerAdapter
public class ImagePageAdapter extends PagerAdapter {
private List<String> mUrlList;
private List<PhotoView> mViews;
private Context context;
public ImagePageAdapter(Context context, List<String> mUrlList, List<PhotoView> mViews) {
this.context = context;
this.mUrlList = mUrlList;
this.mViews = mViews;
}
@Override
public int getCount() {
return mUrlList == null ? 0 : mUrlList.size();
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
PhotoView photoView = mViews.get(position);
GlideUtils.showImage(context, mUrlList.get(position), photoView);
container.addView(photoView);
return mViews.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(mViews.get(position));
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
相关依赖:
BasePopupWindow
implementation ‘com.github.razerdp:BasePopup:2.2.20’
implementation ‘com.github.razerdp:BasePopup-compat-androidx:2.2.1’
PhotoView
implementation ‘com.github.chrisbanes:PhotoView:2.3.0’
glide
implementation ‘com.github.bumptech.glide:glide:4.12.0’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.12.0’