android – 如何在FragmentStatePagerAdapter中动态添加页面

前端之家收集整理的这篇文章主要介绍了android – 如何在FragmentStatePagerAdapter中动态添加页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道这个问题需要很多次,但经过多次研究后我都没有得到答复.
在我的主要活动中,我有一个视图寻呼机,我为它设置了一个适配器:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
ViewPager mPager;
    PagerAdapter mPagerAdapter;

    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
    String tag = "onActivityResult";
    String msg = "omad";
    String timeStamp;
    Bitmap reducedImage;
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();//content://media/external/images/media/40596

            selectedImagePath = getPath(selectedImageUri);// /storage/emulated/0/DCIM/Camera/IMG_20131022_192859.jpg
            //reducedImage = reduceImageSize(selectedImagePath);
            Bitmap img = BitmapFactory.decodeFile(selectedImagePath);

            File Images = new File(MainActivity.root + File.separator + "Images");
            String[] Image = Images.list();
            int ImageCount = Image.length;
            try {
                FileOutputStream fos = new FileOutputStream(new File(root + File.separator +
                        "/Images" + File.separator + String.valueOf(ImageCount) + ".jpg"));
                img.compress(Bitmap.CompressFormat.JPEG,90,fos);
                fos.flush();
                fos.close();
            } catch (IOException e) {
                Log.e("onActivityResult",e.getMessage());
            }
        }
    }

    NUM_PAGES += 1;
        ScreenSlidePagerAdapter test = new  ScreenSlidePagerAdapter(getSupportFragmentManager());
         test.notifyDataSetChanged();
}

我的主要活动中有适配器类,如私有类:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
@Override
    public Fragment getItem(int position) {
        Log.v("adapter","getitem" + String.valueOf(position));
        return ScreenSlidePageFragment.create(position);
    }
@Override
    public int getCount() {
        Log.v("adapter","getcount" + String.valueOf(NUM_PAGES));
        return num_pages;
    }

最后我有一个片段类,它为视图中的每个页面扩展我的布局pager.it有图像视图和文本和…:

public class ScreenSlidePageFragment extends Fragment {

public static final String ARG_PAGE = "page";
private int mPageNumber;
private Context mContext;
private int Cimage;
private ArrayList<Uri> imageUri;

public static ScreenSlidePageFragment create(int pageNumber){
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE,pageNumber);
    fragment.setArguments(args);
    return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);

    imageUri = new ArrayList<Uri>();
             ...
    // image uri get uri of image that saved in directory of app
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.
    //mContext = getActivity();
    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_screen_slide_page,container,false);

    // Set the title view to show the page number.
    //View v;
    ImageView imageView;
    //if(convertView == null){
    /*LayoutInflater li = LayoutInflater.from(mContext) ;
    v = li.inflate(R.layout.imagetext,null);*/
    rootView.setId(mPageNumber);
    Button button = (Button) rootView.findViewById(R.id.ImageSetting);
    button.setId(mPageNumber);
    String title = MainActivity.imageSetting.getString(String.valueOf(mPageNumber),"default");
    TextView tv = (TextView) rootView.findViewById(R.id.ImageText);
    tv.setText(title);
    imageView = (ImageView) rootView.findViewById(R.id.image);  //imageView = new ImageView(mContext);
    SetLayoutSize(mPageNumber);
    imageView.setLayoutParams(new RelativeLayout.LayoutParams(layoutWidth,layoutHeight));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setPadding(30,50,70,30);
    //imageView.setImageResource(mThumbIds[position]);
        //imageView.setImageURI(imageUri.get(position));
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 4; 
    Bitmap bmp = BitmapFactory.decodeFile(imageUri.get(mPageNumber).getPath(),option);
    imageView.setImageBitmap(bmp);

    /*((TextView) rootView.findViewById(android.R.id.text1)).setText(
            getString(R.string.title_template_step,mPageNumber + 1));*/

    return rootView;
}

// this method is not very important

private void SetLayoutSize(int position){
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imageUri.get(position).getPath(),option);
    float scale = (float) ((float) ((double)option.outHeight) / ((double)option.outWidth));
    //Log.v("reduceimage",String.valueOf(imagetemp.getWidth()) + " * " + String.valueOf(imagetemp.getHeight()));
    //Log.v("scale",String.valueOf(scale));
    layoutWidth = MainActivity.display.getWidth();
    layoutHeight = MainActivity.display.getHeight();
    layoutHeight = (int) ( layoutWidth * scale);
    //Log.v("size",String.valueOf(width) + " * " + String.valueOf(height));
    //Bitmap imagebmp = BitmapFactory.decodeFile(path);
 }

这是每个片段具有的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
>

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

<ImageView 
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ImageView>

<TextView 
    android:id="@+id/ImageText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" />

<Button
    android:id="@+id/ImageSetting"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="SettingButton"
    android:background="@drawable/ic_menu_moreoverflow_normal_holo_light" />

 </RelativeLayout>
 </ScrollView>

在我的主要活动中,我有一个按钮,从图库中获取图像并保存在目录中.我应该在onActivityResult中更新我的适配器,但是我收到此错误

02-03 19:30:21.567:E / AndroidRuntime(3407):java.lang.IllegalStateException:应用程序的PagerAdapter在不调用PagerAdapter#notifyDataSetChanged的情况下更改了适配器的内容!预期的适配器项目数:1,找到:2 Pager id:com.sober.secret:id / pager Pager class:class android.support.v4.view.ViewPager有问题的适配器:class com.example.test.MainActivity $ScreenSlidePagerAdapter

但在应用程序崩溃后,我的图像显示正确.
我尝试notifyDataSetChanged()但没有成功,我尝试在stackoverflow中的任何解决方案.

解决方法

在NUM_PAGES = 1之后调用mPagerAdapter.notifyDataSetChanged();

猜你在找的Android相关文章