想必Tabs+ViewPager+ListView 结合使用的场景在你的Android手机中的各大应用里并不少见,比如最为典型的网易新闻。
众所周知,用RecyclerView
可以非常简单的替代掉ListView
。可仅仅就为了将ListView换成RecyclerView,这换汤不换药的做法显然不足以让人心动。
如果我说,再用上RecycledViewPool
,可以使你的布局渲染速度、ViewPager滑动流畅度上升一个档次 ,你会心动并行动吗?
RecycledViewPool是什么? 关于RecycledViewPool,官方文档 是这样说的:
Recycled view pools allow multiple RecyclerViews to share a common pool of scrap views. This can be useful if you have multiple RecyclerViews with adapters that use the same view types, for example if you have several data sets with the same kinds of item views displayed by a ViewPager.
RecyclerView automatically creates a pool for itself if you don’t provide one.
简言之就是,你可以给RecyclerView设置一个ViewHolder的对象池,这个池称为RecycledViewPool
,这个对象池可以节省你创建ViewHolder的开销,更能避免GC
。即便你不给它设置,它也会自己创建一个。
如此说来,如果多个RecylerView间共用一个RecycledViewPool是不是能让你的UI更加的“顺滑”?
使用RecycledViewPool RecycledViewPool使用起来也是非常的简单:先从某个RecyclerView对象中获得它创建的RecycledViewPool对象,或者是自己实现一个RecycledViewPool对象,然后设置个接下来创建的每一个RecyclerView即可。
需要注意的是,如果你使用的LayoutManager是LinearLayoutManager 或其子类(如GridLayoutManager),需要手动开启这个特性:layout.setRecycleChildrenOnDetach(true)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 RecyclerView view1 = new RecyclerView(context); LinearLayoutManager layout = new LinearLayoutManager(context); layout.setRecycleChildrenOnDetach(true ); view1.setLayoutManager(layout); RecycledViewPool pool = view1.getRecycledViewPool(); RecyclerView view2 = new RecyclerView(context); view2.setRecycledViewPool(pool); RecyclerView view3 = new RecyclerView(context); view3.setRecycledViewPool(pool);
ViewPager中使用原理同上,只是你得通过ViewPagerAdapter传递个下一个RecyclerView。
代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 public class PagerActivity extends AppCompatActivity { @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_pager); ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new PageAdapter(getSupportFragmentManager())); } static class PageAdapter extends FragmentPagerAdapter { RecyclerView.RecycledViewPool mPool = new RecyclerView.RecycledViewPool(); public PageAdapter (FragmentManager fm) { super (fm); } @Override public Fragment getItem (int i) { RecyclerViewFragment f = new RecyclerViewFragment(); f.mPool = mPool; return f; } } public static class RecyclerViewFragment extends Fragment { RecyclerView.RecycledViewPool mPool; @Nullable @Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RecyclerView view = new RecyclerView(inflater.getContext()); LinearLayoutManager layout = new LinearLayoutManager(inflater.getContext()); layout.setRecycleChildrenOnDetach(true ); view.setLayoutManager(layout); if (mPool != null ) { view.setRecycledViewPool(mPool); } view.setAdapter(...); return view; } } }
布局xml:
activity_pager.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.PagerTitleStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.v4.view.ViewPager> </LinearLayout>
详见 https://gist.github.com/yrom/728237025575005a8fd3
More Tips RecycledViewPool是依据ItemViewType
来索引ViewHolder的,所以你必须确保共享的RecyclerView的Adapter是同一个,或view type 是不会冲突的。
RecycledViewPool可以自主控制需要缓存的ViewHolder数量:mPool.setMaxRecycledViews(itemViewType, number);
毕竟池子里的水并不是越深越好。
RecyclerView可以设置自己所需要的ViewHolder数量,只有超过这个数量的detached ViewHolder
才会丢进ViewPool中与别的RecyclerView共享。recyclerView.setItemViewCacheSize(10);
在合适的时机,RecycledViewPool会自我清除掉所持有的ViewHolder对象引用,不用担心池子会“侧漏”。当然你也可以在你认为合适的时机手动调用clear()