问题描述
我有一个寻呼机适配器,它想使代表日历的复杂视图膨胀.
I have a pager adapter that suppose to inflate a complex view representing a calendar.
日历的每年充气大约需要350毫秒.
It takes around ~350 ms to inflate each year of the calendar.
为了提高性能,我想实现与ListView
回收视图的数组适配器(getView()
中的convertView
参数)相同的机制.
To improve performance I would like to implement the same mechanism that exists in the ListView
array adapter of recycling views (convertView
parameter in getView()
).
这是适配器当前的getView()
.
@Override
protected View getView(VerticalViewPager pager, final DateTileGrid currentDataItem, int position)
{
mInflater = LayoutInflater.from(pager.getContext());
// This is were i would like to understand weather is should use a recycled view or create a new one.
View datesGridView = mInflater.inflate(R.layout.fragment_dates_grid_page, pager, false);
DateTileGridView datesGrid = (DateTileGridView) datesGridView.findViewById(R.id.datesGridMainGrid);
TextView yearTitle = (TextView) datesGridView.findViewById(R.id.datesGridYearTextView);
yearTitle.setText(currentDataItem.getCurrentYear() + "");
DateTileView[] tiles = datesGrid.getTiles();
for (int i = 0; i < 12; i++)
{
String pictureCount = currentDataItem.getTile(i).getPictureCount().toString();
tiles[i].setCenterLabel(pictureCount);
final int finalI = i;
tiles[i].setOnCheckedChangeListener(new DateTileView.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(DateTileView tileChecked, boolean isChecked)
{
DateTile tile = currentDataItem.getTile(finalI);
tile.isSelected(isChecked);
}
});
}
return datesGridView;
}
是否有实现此类行为的指针或方向?特别是,我怎么能在适配器中得知DateTileGridViews
之一正在被刷出屏幕,所以我可以将其保存在内存中以便下次再次使用.
Any pointers or direction for implementing such a behavior?In particular how can I know in the adapter that one of the DateTileGridViews
is being swiped of the screen so I could save it in memory to reuse it next time.
推荐答案
所以我已经弄清楚了.
- 覆盖
destroyItem(ViewGroup container, int position, Object view)
并保存您的缓存视图 - 创建一个单独的方法,以查看是否有机会使用再生视图,或者是否应该为新视图充气.
- 请记住,一旦使用了回收的视图,就从缓存中删除该视图,以避免相同的视图将相同的视图附加到寻呼机上.
- overwrite
destroyItem(ViewGroup container, int position, Object view)
ans save you cached view - create a separate method to see if there is any chance to use a recycled view or should you inflate a new one.
- remember to remove the recycled view from cache once its been used to avoid having same view attaching same view to the pager.
这是代码..我使用了视图堆栈来缓存从寻呼机中删除的所有视图
here is the code.. I used a Stack of view to cache all removed views from my pager
private View inflateOrRecycleView(Context context)
{
View viewToReturn;
mInflater = LayoutInflater.from(context);
if (mRecycledViewsList.isEmpty())
{
viewToReturn = mInflater.inflate(R.layout.fragment_dates_grid_page, null, false);
}
else
{
viewToReturn = mRecycledViewsList.pop();
Log.i(TAG,"Restored recycled view from cache "+ viewToReturn.hashCode());
}
return viewToReturn;
}
@Override
public void destroyItem(ViewGroup container, int position, Object view)
{
VerticalViewPager pager = (VerticalViewPager) container;
View recycledView = (View) view;
pager.removeView(recycledView);
mRecycledViewsList.push(recycledView);
Log.i(TAG,"Stored view in cache "+ recycledView.hashCode());
}
不要忘记在适配器构造函数中实例化堆栈.
这篇关于如何实现PagerAdapter的视图回收机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!