Anu*_*are 7 android android-room android-architecture-components
ViewModel类,我们在其中使用数据源加载pagedlist.
public class RecipeListViewModel extends ViewModel {
public LiveData<PagedList<RecipeListPojo>> mutableLiveData;
public void init(RecipeFrom recipeFrom, RecipeDao recipeDao) {
mutableLiveData = new
LivePagedListBuilder(recipeDao.getRecipeList(),10).build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的dao,我们以数据源工厂的形式获取数据.
@Dao
public interface RecipeDao {
@Query("select * from recipe")
public DataSource.Factory<Integer, RecipeListPojo> getRecipeList();
}
Run Code Online (Sandbox Code Playgroud)
在我的RecipeListPojo里面,我创建了DiffCallBack.
public static DiffUtil.ItemCallback<RecipeListPojo> diffCallback=new
DiffUtil.ItemCallback<RecipeListPojo>() {
@Override
public boolean areItemsTheSame(RecipeListPojo oldItem, RecipeListPojo
newItem) {
return oldItem.getId()==newItem.getId();
}
@Override
public boolean areContentsTheSame(RecipeListPojo oldItem, RecipeListPojo
newItem) {
return oldItem.equals(newItem);
}
};
Run Code Online (Sandbox Code Playgroud)
在我的活动中,我通过观察者和设置我的适配器来回收页面列表.
arrayListObserver=new Observer<PagedList<RecipeListPojo>>() {
@Override
public void onChanged(@Nullable PagedList<RecipeListPojo> recipePojos) {
if(recipePojos!=null)
{
recipeAdapter.submitList(recipePojos);
recyclerView.setAdapter(recipeAdapter);
progressBar.setVisibility(GONE);
}
}
};
recipeFrom=new RecipeFrom.RecipeFromBuilder(fromActivity).build();
recipeDao=GlobalApplication.recipeRoomDatabase.getRecipeDao();
recipeListViewModel.init(recipeFrom,recipeDao);
recipeListViewModel.mutableLiveData.observe(this,arrayListObserver);
Run Code Online (Sandbox Code Playgroud)
这就是我的适配器的样子.
public class RecipeListAdapter extends
PagedListAdapter<RecipeListPojo,RecipeListAdapter.RecipeListHolder> {
private LayoutInflater inflater;
private Context context;
public RecipeListAdapter()
{
super(RecipeListPojo.diffCallback);
}
@Override
public RecipeListHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
context=parent.getContext();
inflater=LayoutInflater.from(context);
View rootView = inflater.inflate(R.layout.one_item_recipe_list, null,
false);
return new RecipeListHolder(rootView);
}
@Override
public int getItemCount() {
return super.getItemCount();
}
}
Run Code Online (Sandbox Code Playgroud)
图书馆使用 -
// Paging
implementation "android.arch.paging:runtime:1.0.0-rc1"
Run Code Online (Sandbox Code Playgroud)
根据我的研究,我终于发现它返回一个大小=总数的列表。的项目,但只有 pagedList size * 3 将被初始化,其余项目将为空,并且它们将在使用 PagedListAdapter 滚动 recyclerView 时更新。
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |