[英]ViewPager Image Gallery not showing inside Fragment - Android
I have an image capturing Fragment and I try to show as a ViewPager Gallery in the same fragment all the pictures taken. 我有一个图像捕获片段,我尝试将所有拍摄的图片显示为同一片段中的ViewPager Gallery。
The flow is: user clicks on a Take Image button, an intent opens the camera, he takes a photo and onActivityResult calls my preview images method which should make the ViewPager visible with all the taken photos. 流程是:用户单击“拍摄图像”按钮,意图打开相机,他拍摄照片,并且onActivityResult调用我的预览图像方法,该方法应使ViewPager在所有拍摄的照片中可见。
I keep the photos in a global ArrayList named bitmaps. 我将照片保存在名为bitmaps的全局ArrayList中。
global:
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
ImagePagerAdapter adapter = new ImagePagerAdapter();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
.....
viewPager = (ViewPager) view.findViewById(R.id.view_pager);}
My previewCapturedImage method: 我的PreviewCapturedImage方法:
private void previewCapturedImage() {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
bitmaps.add(bitmap);
viewPager.setVisibility(View.VISIBLE);
viewPager.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
My PagerAdapter: 我的PagerAdapter:
private class ImagePagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return bitmaps.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = 15;
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageBitmap(bitmaps.get(position));
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
my xml: 我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
....
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="@+id/titlu2"
android:layout_marginBottom="60dp"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/butoane_foto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical">
.....
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<!-- To display picture taken -->
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--android:visibility="gone"-->
</LinearLayout>
.....
</LinearLayout>
</ScrollView>
...........
</RelativeLayout>
在previewCapturedImage
的末尾,添加以下行以通知适配器数据已更改:
adapter.notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.