ViewPager中的滚动页面运行速度非常慢

时间:2015-02-03 13:04:17

标签: android android-viewpager android-pageradapter

在HTC Desire X上的ViewPager中滚动页面非常慢,在三星Galaxy S4上还可以。 在我的应用程序中,我有一个查看寻呼机,每页显示九个项目。 每个项目都有:名称,小图像(25kb-50kb,500x350)和简短描述。 每个图像都是通过Universal Image Loader从url下载的 当我设置适配器超过20项时,viewpager工作缓慢,我有警告:

Skipped XX frames!  The application may be doing too much work on its main thread.

这是我的PagerAadapter中的instantiateItem()代码:

public Object instantiateItem(ViewGroup container, final int position) {
    LayoutInflater inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemsView = inflater.inflate(R.layout.item_pager, container,
            false);
    LinearLayout[] line = new LinearLayout[3];
    line[0] = (LinearLayout)itemsView.findViewById(R.id.item_pager_first_line);
    line[1] = (LinearLayout)itemsView.findViewById(R.id.item_pager_second_line);
    line[2] = (LinearLayout)itemsView.findViewById(R.id.item_pager_third_line);

    int i = 0;
    for(LinearLayout layout : line){
        TextView[] itemsName = new TextView[3];
        LinearLayout[] itemsLayout = new LinearLayout[3];
        itemsLayout[0] = (LinearLayout)layout.findViewById(R.id.item1_layout);
        itemsLayout[1] = (LinearLayout)layout.findViewById(R.id.item2_layout);
        itemsLayout[2] = (LinearLayout)layout.findViewById(R.id.item3_layout);
        final ImageView[] itemsImage = new ImageView[3];
        TextView[] desc = new TextView[3];

        final int objectId1 = (position*9)+(i*3);
        final int objectId2 = (position*9)+(i*3)+1;
        final int objectId3 = (position*9)+(i*3)+2;

        if(mItemsList.size()>objectId1){
            // Set appropriate name of item.
            itemsName[0] = (TextView)layout.findViewById(R.id.item1_name);
            itemsName[0].setText(mItemsList.get(objectId1).getName());
            //Set appropriate image of item
            itemsImage[0] = (ImageView)layout.findViewById(R.id.item1_image);
            mImageLoader.displayImage(Image.getRegularUrl(mItemsList.get(objectId1)
                    .getImages()[0].getFilename()),itemsImage[0],mOptions);
            desc[0] = (TextView)layout.findViewById(R.id.item1_desc_text_view);
            desc[0].setText(mItemsList.get(objectId1).getDesc()));
            //Set action after click on item.
            itemsLayout[0].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onMyClikItem(mItemsList.get(objectId1).getItemId());
                }
            });
            //Set action after long click on item.
            itemsLayout[0].setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onMyLongClikItem(objectId1);
                    return true;
                }
            });
            // Check next item.
            if(mItemsList.size()>objectId2){
                itemsName[1] = (TextView)layout.findViewById(R.id.item2_name);
                itemsName[1].setText(mItemsList.get(objectId2).getName());
                itemsImage[1] = (ImageView)layout.findViewById(R.id.item2_image);

                mImageLoader.displayImage(Image.getRegularUrl(mItemsList.get(objectId2)
                        .getImages()[0].getFilename()),itemsImage[1],mOptions);
                desc[1] = (TextView)layout.findViewById(R.id.item2_desc_text_view);
                desc[1].setText(mItemsList.get(objectId1).getDesc()));
                itemsLayout[1].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onMyClikItem(mItemsList.get(objectId2).getItemId());
                    }
                });
                itemsLayout[1].setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        onMyLongClikItem(objectId2);
                        return true;
                    }
                });
                if(mItemsList.size()>objectId3){
                    itemsName[2] = (TextView)layout.findViewById(R.id.item3_name);
                    itemsName[2].setText(mItemsList.get(objectId3).getName());
                    itemsImage[2] = (ImageView)layout.findViewById(R.id.item3_image);

                    mImageLoader.displayImage(Image.getRegularUrl(mItemsList.get(objectId3)
                            .getImages()[0].getFilename()),itemsImage[2],mOptions);
                    desc[2] = (TextView)layout.findViewById(R.id.item3_desc_text_view);
                    desc[2].setText(mItemsList.get(objectId1).getDesc()));
                    itemsLayout[2].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onMyClikItem(mItemsList.get(objectId3).getItemId());
                        }
                    });
                    itemsLayout[2].setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            onMyLongClikItem(objectId3);
                            return true;
                        }
                    });
                } else {
                    itemsLayout[2].setVisibility(View.INVISIBLE);
                }
            } else {
                itemsLayout[1].setVisibility(View.INVISIBLE);
                itemsLayout[2].setVisibility(View.INVISIBLE);
            }
        }else {
            layout.setVisibility(View.INVISIBLE);
        }
        i++;
    }

    ((ViewPager) container).addView(itemView);
    return itemsView;
}

我应该更改什么才能加快我的ViewPager。

1 个答案:

答案 0 :(得分:0)

您可以采取许多措施来改善Android的性能。尝试将所有加载等放入AsycTask(http://developer.android.com/reference/android/os/AsyncTask.html)。

此外,还有关于如何优化性能的文档:http://developer.android.com/training/articles/perf-tips.html。当我阅读你的代码时,我看到很多难以阅读的代码和大量的重复代码。试着重新思考一下你处理这种情况的方式。