【发布时间】:2015-02-03 21:22:25
【问题描述】:
在 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。
【问题讨论】:
-
“工作非常缓慢”是什么意思?您的意思是它的延迟?您是否尝试过启用硬件加速?
-
使用 Traceview 确定您的问题所在。
-
当我尝试快速滚动 3 页或更多页时,view pager 无法正常工作,我必须等待几秒钟才能将手机滚动到下一页。
标签: android android-viewpager android-pageradapter