Прокрутка страницы в ViewPager на HTC Desire X очень медленная, на Samsung Galaxy S4 в порядке. В моем приложении у меня есть пейджер, который отображает девять элементов на каждой странице. Каждый элемент имеет: имя, маленькое изображение (25kb-50kb, 500x350) и краткие описания. Каждого изображения загружается из URL с помощью Универсальной Image Loader Когда я установлен на переходник более 20 наименований ViewPager работает медленно, и я имел предупреждение:Прокрутка страницы в ViewPager работает очень медленно
Skipped XX frames! The application may be doing too much work on its main thread.
Вот мой instantiateItem (код) от моего PagerAadapter:
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, чтобы определить, где находится ваша проблема. – CommonsWare
Когда я пытаюсь быстро прокручивать 3 или более страниц, просмотр пейджера работает неточно, и я должен подождать несколько секунд, чтобы прокрутить телефон до следующей страницы. – piotrekso91