xamarin的android中RecyclerView的使用
先看下面的图
简单来说:
1.我们需要把数据源放到Adapter里
// Prepare the data source:
mPhotoAlbum = new PhotoAlbum();
// Instantiate the adapter and pass in its data source:
mAdapter = new PhotoAlbumAdapter (mPhotoAlbum);
2.RecyclerView需要指定Adapter
// Set our view from the "main" layout resource:
SetContentView (Resource.Layout.Main);
// Get our RecyclerView layout:
mRecyclerView = FindViewById<RecyclerView> (Resource.Id.recyclerView);
// Plug the adapter into the RecyclerView:
mRecyclerView.SetAdapter (mAdapter);
3.RecyclerView需要指定LayoutManager
mLayoutManager = new LinearLayoutManager (this);
mRecyclerView.SetLayoutManager (mLayoutManager);
另外:
ViewHolder就是把界面上需要的UI元素缓存下来, 建立引用关系。也就是RecyclerView的每个Item的UI元素
ViewHolder和Adapter的关系
Adapter内部有3个功能。
1.实例化RecyclerView的每个Item的layout前端文件.
2.创建ViewHolder, 把layout定义的UI元素关联到ViewHolder里。
3.把ViewHolder和DataSource关联起来。