如何自动切换viewpager?

时间:2019-06-14 02:39:41

标签: android android-viewpager

如何使其在几秒钟内自动查看寻呼机? 我尝试了几种方法,但是当viewpager更改时仍然失败

错误消息是“试图在空对象引用上调用接口方法'int java.util.List.size()'”

    private Handler handler;
    private int delay = 5000; //milliseconds
    private int page = 0;
    private HomeSliderPagerAdapter myAdapter;
    Runnable runnable = new Runnable() {
        public void run() {
            if (myAdapter.getCount() == page) {
                page = 0;
            } else {
                page++;
            }
            viewPager.setCurrentItem(page, true);
            handler.postDelayed(this, delay);
        }
    };

关于ActivityCreated

        handler = new Handler();
        viewPager = (ViewPager) getActivity().findViewById(R.id.vPager);
        sliderImg = new ArrayList<>();

        volleyObjectResult = new VolleyObjectResult() {
            @Override
            public void resSuccess(String requestType, JSONObject response) {
                try {
                    //String images = response.getString("images");
                    JSONArray jsonArray = response.getJSONArray("banner");
                    for (int i=0; i<jsonArray.length(); i++) {
                        SliderUtil sliderUtil = new SliderUtil();
                        try {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            sliderUtil.setSliderImgUrl(jsonObject.getString("IMAGES"));
                            Log.e(TAG, "Test -> " + jsonObject.getString("IMAGES"));
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                        sliderImg.add(sliderUtil);
                    }
                    HomeSliderPagerAdapter hspa = new HomeSliderPagerAdapter(sliderImg, getActivity());
                    hspa.notifyDataSetChanged();
                    viewPager.setAdapter(hspa);
                    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                            page = position;
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                    });
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void resError(String requestType, VolleyError error) {
                Toast.makeText(getActivity(), "Network connection problem", Toast.LENGTH_LONG).show();
            }
        };
        volleyObjectService = new VolleyObjectService(volleyObjectResult, getActivity());
        volleyObjectService.postJsonObject("GETCALL", requestUrl, data);
    }

结果: 当图像更改时,它将强制关闭并显示错误消息“尝试在空对象引用上调用接口方法'int java.util.List.size()'”

2 个答案:

答案 0 :(得分:0)

  

使用View Pager自定义适配器

public class SliderPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
Activity activity;
List<ImageSliderModel> imageSliderModels;


public SliderPagerAdapter(Activity activity, List<ImageSliderModel> imageSliderModels) {
    this.activity = activity;
    this.imageSliderModels = imageSliderModels;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = layoutInflater.inflate(R.layout.layout_slider, container, false);
    ImageView im_slider = (ImageView) view.findViewById(R.id.im_slider);
    String url = imageSliderModels.get(position).getImage();




    Glide.with(activity).load(imageSliderModels.get(position).getImage()).into(im_slider);

    container.addView(view);

    return view;
}

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


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


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

在“活动调用”视图中,寻呼机和适配器

  mvViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            addBottomDots(i, ll_dots);
            page = i;
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });
 h.postDelayed(new Runnable() {
        @Override
        public void run() {

            try {
                if (sliderPagerAdapter.getCount() == page) {
                    page = 0;
                } else {
                    page++;
                }
            }catch (Exception ignored)
            {

            }

            mvViewPager.setCurrentItem(page);
            h.postDelayed(this, 4000);
        }
    }, 1000);
  

像这样将列表传递给自定义适配器。

  sliderPagerAdapter = new SliderPagerAdapter((Activity) HomeScreen.this, imageSliderModels);
                mvViewPager.setAdapter(sliderPagerAdapter);
                sliderPagerAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

private void setTimer()
{
   CountDownTimer countDownTimer = new CountDownTimer(5000,2000) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            if(viewPager.getCurrentItem() < arrayList.size() - 1)
            {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            }
            else {
                viewPager.setCurrentItem(0);
            }
           countDownTimer.start();
        }
    };
    countDownTimer.start();
}