我有一个寻呼机适配器,它可以扩展表示日历的复杂视图.
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()
.
Here is my current getView()
from the adapter.
@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)
ans save you cached viewdestroyItem(ViewGroup container, int position, Object view)
ans save you cached view这里是代码.. 我使用 Stack of view 来缓存从我的寻呼机中删除的所有视图
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 的视图回收机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!