尝试从FIrebase在ViewPager中显示图像时发生异常

时间:2018-09-30 01:15:20

标签: android firebase-realtime-database android-viewpager fragment

嘿,人们陷入了这种异常

同时从FirebaseDatabase获取数据并将其作为横幅显示在Viewpager适配器中

我使用UltraViewPager库来显示“查看”寻呼机

但是每次我尝试显示数据时,都会出现此异常

[1]  2  3  4

ive把ViewPager放在一个名为 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.team.plustegara, PID: 26162 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 400 Pager id: 1 Pager class: class com.tmall.ultraviewpager.UltraViewPagerView Problematic adapter: class com.tmall.ultraviewpager.UltraViewPagerAdapter at android.support.v4.view.ViewPager.populate(ViewPager.java:1137) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617) at com.tmall.ultraviewpager.UltraViewPagerView.setCurrentItemFake(UltraViewPagerView.java:245) at com.tmall.ultraviewpager.UltraViewPager.scrollNextPage(UltraViewPager.java:400) at com.tmall.ultraviewpager.UltraViewPager$2.callBack(UltraViewPager.java:302) at com.tmall.ultraviewpager.TimerHandler.handleMessage(TimerHandler.java:60) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 的片段

这是我的适配器

  

SliderBaseAdapter.java

News.java

这是从FirebaseDatabase解析列表中数据的方法

  

News.java

public class SliderBaseAdapter extends PagerAdapter {


    @BindView(R.id.text)
    TextView textView;
    @BindView(R.id.imgSlides)
    ImageView imgSlides;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    private List<SliderModels> imageModelArrayList;
    private Context context;
    private LayoutInflater inflater;

    public SliderBaseAdapter(List<SliderModels> imageModelArrayList, Context context) {
        this.imageModelArrayList = imageModelArrayList;
        this.context = context;
        inflater = LayoutInflater.from(context);

    }




    @SuppressLint("CheckResult")
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup view, int position) {
        View convertView   = inflater.inflate(R.layout.text_item, view, false);

        //Butter View Bind
        if (convertView != null) {
            ButterKnife.bind(this, view);
        }

        FadingCircle fadingCircle = new FadingCircle();
        progressBar.setIndeterminateDrawable(fadingCircle);

        SliderModels listy = imageModelArrayList.get(position);

        assert convertView != null;

        textView.setText(listy.getTitle());

        RequestOptions requestOptions = new RequestOptions();
        requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);

        GlideApp.with(context)
                .load(listy.getImage())
                .transition(withCrossFade())
                .placeholder(R.drawable.gradients_card)
                .apply(requestOptions)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }
                })
                .into(imgSlides);

        assert view != null;
        view.addView(convertView,0);


        return convertView;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public int getCount() {
        return imageModelArrayList.size();
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public synchronized void refreshData(@NonNull List<SliderModels> list){

        imageModelArrayList = new ArrayList<>(list);
        notifyDataSetChanged();
    }
}
  

ButterKnife注入的View Pager

@BindView(R.id.ultra_viewpager)UltraPagerView ultra_viewpager;

0 个答案:

没有答案