如何实现查看回收机制,寻呼机适配器寻呼机、适配器、如何实现、机制

2023-09-05 10:09:52 作者:感情奉献

我有一个寻呼机适配器,假设膨胀复杂的观点重新presenting日历。 它需要大约〜350毫秒每年的日历膨胀。

要提高性能,我想实现在循环意见( convertView paramtere在 getView列表视图阵列适配器相同的机理探讨达exsist( )法)

下面是从适配器我目前getView方法。

  @覆盖
    受保护的视图getView(VerticalViewPager寻呼机,最终DateTileGrid currentDataItem,INT位置)
    {
        mInflater = LayoutInflater.from(pager.getContext());

            //这是我想了解天气应使用回收视图或创建一个新的。
        查看datesGridView = mInflater.inflate(R.layout.fragment_dates_grid_page,寻呼机,假);


        DateTileGridView datesGrid =(DateTileGridView)datesGridView.findViewById(R.id.datesGridMainGrid);
        TextView的yearTitle =(TextView中)datesGridView.findViewById(R.id.datesGridYearTextView);
        yearTitle.setText(currentDataItem.getCurrentYear()+);
        DateTileView []瓷砖= datesGrid.getTiles();

        的for(int i = 0; I< 12;我++)
        {
            串pictureCount = currentDataItem.getTile(ⅰ).getPictureCount()的toString()。
            瓷砖[I] .setCenterLabel(pictureCount);
            最终诠释finalI =我;
            瓷砖[I] .setOnCheckedChangeListener(新DateTileView.OnCheckedChangeListener(){
                @覆盖
                公共无效onCheckedChanged(DateTileView tileChecked,布尔器isChecked)
                {
                    DateTile瓦= currentDataItem.getTile(finalI);
                    tile.isSelected(器isChecked);
                }
            });
        }

        返回datesGridView;
    }
 
出售回收吉时利2306电源

有关实施这种行为的任何指针或方向? 特别是我怎么能知道在适配器的 DateTileGridViews 正在刷卡的屏幕,所以我可以将它保存在内存中重用它下一次...... 解决方案

所以我想通了。

覆盖 destroyItem(ViewGroup中的容器,INT位置,对象视图)答节省您的缓存的视图 创建一个单独的方法,看看是否有任何机会使用回收视图还是应该夸大一个新的。 记得删除从缓存中回收的观点,一旦它被用来避免附加同样的观点寻呼机相同的观点。

这里是code ..我用来看堆栈缓存从我的传呼机全部去掉意见

 私人查看inflateOrRecycleView(上下文的背景下)
{

    查看viewToReturn;
    mInflater = LayoutInflater.from(上下文);
    如果(mRecycledViewsList.isEmpty())
    {
        viewToReturn = mInflater.inflate(R.layout.fragment_dates_grid_page,空,假);
    }
    其他
    {
        viewToReturn = mRecycledViewsList.pop();
        Log.i(TAG,恢复循环认为从缓存中+ viewToReturn.hash code());
    }


    返回viewToReturn;
}

@覆盖
公共无效destroyItem(ViewGroup中的容器,INT位置,对象视图)
{
    VerticalViewPager寻呼机=(VerticalViewPager)容器中;
    查看recycledView =(视图);
    pager.removeView(recycledView);
    mRecycledViewsList.push(recycledView);
    Log.i(TAG,在高速缓存中存储视图+ recycledView.hash code());
}
 

不要忘了实例化堆栈在适配器构造。

I have a pager adapter that suppose to inflate a complex view representing a calendar. It takes around ~350 ms to inflate each year of the calendar.

To improve performance i would like to implement the same mechanisim tat exsist in the list view array adapter of recycling views (convertView paramtere in getView() method)

here is my current getView method 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;
    }

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...

解决方案

So I have figured it out.

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());
}

do not forget to instantiate the stack in the adapter constructor.