1

我想在我的 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 以避免此问题吗?

4

1 回答 1

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;
        }
于 2013-05-13T10:50:21.900 回答