ViewPager图片库未显示Fragment内部 - Android

时间:2014-12-22 15:37:32

标签: android android-viewpager

我有一个捕捉Fragment的图像,我尝试在同一个片段中显示所有拍摄的照片。

流程是:用户点击拍摄图像按钮,意图打开相机,拍摄照片,onActivityResult调用我的预览图像方法,这样可以使ViewPager显示所有拍摄的照片。

我将照片保存在名为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);}

我的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();
    }
}

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

我的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>

1 个答案:

答案 0 :(得分:0)

previewCapturedImage结束时,添加此行以通知适配器数据已更改:

adapter.notifyDataSetChanged();