<tfoot id='DokNN'></tfoot>

<small id='DokNN'></small><noframes id='DokNN'>

    <i id='DokNN'><tr id='DokNN'><dt id='DokNN'><q id='DokNN'><span id='DokNN'><b id='DokNN'><form id='DokNN'><ins id='DokNN'></ins><ul id='DokNN'></ul><sub id='DokNN'></sub></form><legend id='DokNN'></legend><bdo id='DokNN'><pre id='DokNN'><center id='DokNN'></center></pre></bdo></b><th id='DokNN'></th></span></q></dt></tr></i><div id='DokNN'><tfoot id='DokNN'></tfoot><dl id='DokNN'><fieldset id='DokNN'></fieldset></dl></div>

    <legend id='DokNN'><style id='DokNN'><dir id='DokNN'><q id='DokNN'></q></dir></style></legend>
    • <bdo id='DokNN'></bdo><ul id='DokNN'></ul>

      1. 如何实现 PagerAdapter 的视图回收机制?

        时间:2023-10-04
            <tbody id='wYFAC'></tbody>
          1. <tfoot id='wYFAC'></tfoot>

              <bdo id='wYFAC'></bdo><ul id='wYFAC'></ul>
                • <small id='wYFAC'></small><noframes id='wYFAC'>

                  <legend id='wYFAC'><style id='wYFAC'><dir id='wYFAC'><q id='wYFAC'></q></dir></style></legend>
                • <i id='wYFAC'><tr id='wYFAC'><dt id='wYFAC'><q id='wYFAC'><span id='wYFAC'><b id='wYFAC'><form id='wYFAC'><ins id='wYFAC'></ins><ul id='wYFAC'></ul><sub id='wYFAC'></sub></form><legend id='wYFAC'></legend><bdo id='wYFAC'><pre id='wYFAC'><center id='wYFAC'></center></pre></bdo></b><th id='wYFAC'></th></span></q></dt></tr></i><div id='wYFAC'><tfoot id='wYFAC'></tfoot><dl id='wYFAC'><fieldset id='wYFAC'></fieldset></dl></div>
                  本文介绍了如何实现 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.

                  推荐答案

                  所以我想通了.

                  1. overwrite destroyItem(ViewGroup container, int position, Object view) ans save you cached view
                  2. 创建一个单独的方法,看看是否有机会使用回收的视图,或者是否应该扩充一个新的视图.
                  3. 记得在缓存中删除回收的视图,以避免相同的视图将相同的视图附加到寻呼机.
                  1. overwrite destroyItem(ViewGroup container, int position, Object view) ans save you cached view
                  2. create a separate method to see if there is any chance to use a recycled view or should you inflate a new one.
                  3. 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 的视图回收机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <bdo id='ZdWv6'></bdo><ul id='ZdWv6'></ul>
                    1. <i id='ZdWv6'><tr id='ZdWv6'><dt id='ZdWv6'><q id='ZdWv6'><span id='ZdWv6'><b id='ZdWv6'><form id='ZdWv6'><ins id='ZdWv6'></ins><ul id='ZdWv6'></ul><sub id='ZdWv6'></sub></form><legend id='ZdWv6'></legend><bdo id='ZdWv6'><pre id='ZdWv6'><center id='ZdWv6'></center></pre></bdo></b><th id='ZdWv6'></th></span></q></dt></tr></i><div id='ZdWv6'><tfoot id='ZdWv6'></tfoot><dl id='ZdWv6'><fieldset id='ZdWv6'></fieldset></dl></div>

                      • <legend id='ZdWv6'><style id='ZdWv6'><dir id='ZdWv6'><q id='ZdWv6'></q></dir></style></legend>

                      • <small id='ZdWv6'></small><noframes id='ZdWv6'>

                          <tfoot id='ZdWv6'></tfoot>
                            <tbody id='ZdWv6'></tbody>