通知数据更改到viewPagerAdapter

时间:2018-06-29 06:21:42

标签: android android-viewpager android-volley

我通过无限滚动从recyvlerview中的服务器加载图像。我在recyclerview Itee上设置onClick,它在viewpager中打开。它工作正常,但是当我从服务器无限加载时,它给出了错误-

 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 9, found: 29 .

齐射回复

     @Override
                public void onResponse(JSONObject response) {
                    progressBarBottom.setVisibility(View.GONE);

                    imagelist = new ArrayList<>();
                    JSONArray heroArray = null;


                    try {
                        heroArray = response.getJSONArray("images");
                        for (int i = 0; i < heroArray.length(); i++) {
                            JSONObject jsonObject = 
                        heroArray.getJSONObject(i);

                            Photos photos = new Photos();
                            photos.setUrl(jsonObject.getString("image"));

                            imagelist.add(photos);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    adapter.addImages(imagelist);
                    myViewPagerAdapter.addImages(imagelist);
                    adapter.notifyDataSetChanged();
                    myViewPagerAdapter.notifyDataSetChanged();
                }

我创建了一个模式类,该模式类从服务器获取数据并设置数据。 照片课程

@Parcel
public class Photos {
public String url;
public String title;

public Photos() {

}

 public Photos(String url, String weight, String title, String thumb, String 
 date, String hit) {
    this.url = url;
    this.thumb = thumb;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}



public String getThumb() {
    return thumb;
}

public void setThumb(String thumb) {
    this.thumb = thumb;
}

}

MyViewPagerAdapter

public class MyViewPagerAdapter extends PagerAdapter {
private Context context;

ProgressBar fullImagePB;
private ArrayList<Photos> imagesList;
private LayoutInflater layoutInflater;

public MyViewPagerAdapter(Context context, ArrayList<Photos> imagesList) {
    this.context = context;
    this.imagesList = imagesList;
    layoutInflater = (LayoutInflater) 
 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public Object instantiateItem(final ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) 
  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Photos image = imagesList.get(position);
    final View view = 
  layoutInflater.inflate(R.layout.image_fullscreen_preview, container, 
  false);
    fullImagePB = view.findViewById(R.id.fullImagePB);
    final ImageView imageViewPreview = (ImageView) 
  view.findViewById(R.id.image_preview);
    imageViewPreview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    });
    imageViewPreview.setOnTouchListener(new 
   ImageMatrixTouchHandler(view.getContext()));


    Glide.with(imageViewPreview.getContext())
            .load(image.getUrl())
            .asBitmap()
            .listener(new RequestListener<String, Bitmap>() {
                @Override
                public boolean onException(Exception e, String model, 
  Target<Bitmap> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, String model, 
  Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) 
   {

                    fullImagePB.setVisibility(View.GONE);
                    return false;
                }
            })
            .fitCenter().into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(final Bitmap resource, GlideAnimation<? 
   super Bitmap> glideAnimation) {
            imageViewPreview.setImageBitmap(resource);

        }

    });
    imageViewPreview.setTag(position);

    container.addView(view);
    return view;

}

@Override
public int getCount() {
    return imagesList.size();
}
public int getItemPosition(Object object) {
    return POSITION_NONE;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
    return view.equals(obj);
}


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

public void addImages(ArrayList<Photos> img){
    for(Photos im : img){
        imagesList.add(im);
        notifyDataSetChanged();
    }

}
}

错误

 06-29 12:12:27.225 19845-19845/satlaa.mysql_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: satlaa.mysql_test, PID: 19845
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 10, found: 30 Pager id: satlaa.mysql_test:id/viewpager Pager class: class satlaa.mysql_test.adapter.ClickableViewPager Problematic adapter: class satlaa.mysql_test.adapter.MyViewPagerAdapter
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1137)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616)
    at android.view.View.measure(View.java:19857)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:19857)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:19857)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
    at android.view.View.measure(View.java:19857)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
    at android.view.View.measure(View.java:19857)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
    at android.view.Choreographer.doCallbacks(Choreographer.java:686)
    at android.view.Choreographer.doFrame(Choreographer.java:621)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我在onBindViewHolder中的一个片段中打开图像。

((MyViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("url", imagesList);
                bundle.putInt("position", position);
                FragmentTransaction ft = ((Activity) mContext).getFragmentManager().beginTransaction();
                Full_Image newFragment = Full_Image.newInstance();
                newFragment.setArguments(bundle);
                newFragment.show(ft, "slideshow");

            }

FullImagefragment

  images = (ArrayList<Photos>) getArguments().getSerializable("url");=
    selectedPosition = getArguments().getInt("position");

    final int number = viewPager.getCurrentItem();
    final Photos image = images.get(number);
    final String img_title = image.getTitle();
    final String img_weight = image.getWeight();
   myViewPagerAdapter = new MyViewPagerAdapter(getActivity(), images);
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
    setCurrentItem(selectedPosition);

2 个答案:

答案 0 :(得分:1)

尝试从您的帐户中删除notifyDataSetChanged();

public void addImages(ArrayList<Photos> img){
    for(Photos im : img){
       imagesList.add(im);
       notifyDataSetChanged();
    }
}

在排球响应中添加项目后的操作。

还要交换一下通知 recycleradapter viewPagerAdapter 因此,而不是

 adapter.addImages(imagelist);
 myViewPagerAdapter.addImages(imagelist);
 adapter.notifyDataSetChanged();
 myViewPagerAdapter.notifyDataSetChanged();

尝试

 myViewPagerAdapter.addImages(imagelist);
 myViewPagerAdapter.notifyDataSetChanged();
 adapter.addImages(imagelist);
 adapter.notifyDataSetChanged();

答案 1 :(得分:0)

1)您不需要:

public void addImages(ArrayList<Photos> img){
    for(Photos im : img){
       imagesList.add(im);
       notifyDataSetChanged();
    }
}

这样做的原因是因为您已经按如下所述在适配器的构造函数中传递了列表。每当您调用notifydatasetchanged()时,构造函数都是UPDATED。

public MyViewPagerAdapter(Context context, ArrayList<Photos> imagesList){..}

2)只需在下面的行下面调用notifydatasetchanged():

 imagelist.add(photos);
 adapter.notifyDatasetChanged();
 myViewPagerAdapter.notifyDatasetChanged();

3)确保从viewpager适配器和其他适配器上都删除方法addImages。