How do I implement view recycling mechanism for PagerAdapter?(如何实现 PagerAdapter 的视图回收机制?)
问题描述
我有一个寻呼机适配器,它可以扩展表示日历的复杂视图.
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.
推荐答案
所以我想通了.
- overwrite
destroyItem(ViewGroup container, int position, Object view)ans save you cached 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.
这里是代码.. 我使用 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 的视图回收机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何实现 PagerAdapter 的视图回收机制?
基础教程推荐
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
