重置CirclePageIndicator库

时间:2013-05-13 08:05:42

标签: android viewpagerindicator

我想在我的ListView中加载图像延迟。我将图片放在ViewPager中,然后为ViewPager设置CirclePageIndicator

public void fetchDrawableOnThread(final ViewPager viewPager, final CirclePageIndicator indicator) {     

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                ImagePagerAdapter adapter = new ImagePagerAdapter((Drawable[]) message.obj);
                viewPager.setAdapter(adapter); 
                indicator.setViewPager(viewPager);
            }
        };

        Thread thread = new Thread() {
            @Override
            public void run() { 

                Drawable[] drawables = fetchDrawables();

                Message message = handler.obtainMessage(1, drawables);
                handler.sendMessage(message);
            }
        };
        thread.start();
    }

这是主Activity中的自定义适配器类:

private class MultiAdapter extends ArrayAdapter<String> {


    public MultiAdapter(Context context, ArrayList<String> mStrings) {
        super(context, R.layout.list_main, mStrings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row;

        if (convertView == null)  {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.list_main, parent, false);
        } else
            row = convertView;

        ViewPager viewPager = (ViewPager) row.findViewById(R.id.pager);
        CirclePageIndicator indicator = (CirclePageIndicator)row.findViewById(R.id.indicator);

        if(scroll_is_idle)
            drawableMng.fetchDrawableOnThread(viewPager, indicator); 
        else {
            viewPager.setAdapter(null);
        }

        return row;
    }
}

在我的getView方法中,我想set viewPager adapter to null, when the scroll is not in idle mode,这就是PageIndicator库中的问题(nullPointerException)。你知道如何重置PageIndicator以避免这个问题吗?

1 个答案:

答案 0 :(得分:0)

我知道这种逻辑可能不那么好:

if(scroll_is_idle)
      drawableMng.fetchDrawableOnThread(viewPager, indicator); 
else 
      viewPager.setAdapter(null);

但顺便说一下,它在ViewPageIndicator库中的onDraw方法中出现问题,并带有nullPoniterException。

我通过编辑onDraw解决了我的问题并将mViewPager.getAdapter() == null添加到条件:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mViewPager == null || mViewPager.getAdapter() == null) {
            return;
        }