当我向下滚动时,查看寻呼机销毁项目

时间:2018-05-08 04:40:06

标签: java android slider

我在回收商查看项目中使用图像滑块,每个项目都有视图寻呼机和自己的图像滑块,在回收站视图中加载所有项目后。第一项显示图像滑块在其视图寻呼机内,然后我向下滚动到其他项目可能是项目10或11然后我向上滚动到第一项目然后查看寻呼机销毁其中的所有项目或片段,现在没有显示图像。请给我解决方案,我的适配器代码。用户FragmentStatePagerAdapter无法访问Fragment Manager

    private class ViewPagerAdapter extends PagerAdapter {


   ArrayList<ImageInfo> allImages=new ArrayList<>();




    private ViewPagerAdapter(ArrayList<ImageInfo> allImages){
        this.allImages = allImages;
    }

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.fragment_big_row_image, container, false);

        ImageView imageListing = (ImageView) itemView.findViewById(R.id.imageListing);


        //MH: Loading Images in slider
        if(allImages.get(position).csImageFull.length()>0)
        {
            CommonMethods.ShowImage(myContext, imageListing, allImages.get(position).csImageFull);

        }
        else
        {
            CommonMethods.ShowImage(myContext, imageListing, allImages.get(position).csImageMedium);

        }

        container.addView(itemView);

        return itemView;
    }

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

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

   /*****************************************************************************************************/

2 个答案:

答案 0 :(得分:0)

在viewpager中设置屏幕页面。屏幕外页面限制定义了您希望在视图寻呼机中保留在内存中的页数。将此数字设置为您想要的页数。

mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);

此处查看寻呼机将最后访问的2页保留在内存中,并且不会销毁它们。

答案 1 :(得分:0)

尝试以下代码:

public class SliderPagerAdapter extends PagerAdapter {

    private Activity activity;
    private ArrayList<String> images;

    public SliderPagerAdapter(Activity activity) {
        this.activity = activity;

    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.item_slider, container, false);
        ImageView allImages = null;

      /MH: Loading Images in slider
        if(images.get(position).csImageFull.length()>0)
        {
            CommonMethods.ShowImage(myContext, imageListing, allImages.get(position).csImageFull);

        }
        else
        {
            CommonMethods.ShowImage(myContext, allImages, images.get(position).csImageMedium);

        }

        container.addView(itemView);



        return view;
    }

    public void setData(ArrayList<String> images) {
        this.images = images;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return images == null ? 0 : images.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object obj) {
        return view == obj;
    }

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

从activity调用方法setData并设置图像数组。

Adapter.setData(yourData);