Mob*_*Dev 7 android pagedlist android-adapter
每次我调用无效数据时,都不会使用我的 DIFF_UTIL。日志没有显示,整个列表都用新数据更新,导致屏幕移动位置等。不确定这里是什么问题。
我有PagedListAdapter一个LiveData<PagedList<Post>> postList,我打电话postList.getValue().getDataSource().invalidate();来刷新我的数据。
我有 DIFF_UTIL
public static DiffUtil.ItemCallback<Post> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Post>() {
@Override
public boolean areItemsTheSame(Post oldItem, Post newItem) {
Log.d(TAG, "areItemsTheSame with result: "
+ (oldItem.getId() == newItem.getId()));
Log.d(TAG, "areItemsTheSame Old Item is: " + oldItem.toString());
Log.d(TAG, "areItemsTheSame New Item is: " + newItem.toString());
return oldItem.getId() == newItem.getId();
}
@Override
public boolean areContentsTheSame(Post oldItem, Post newItem) {
Log.d(TAG, "areContentsTheSame with result: " + oldItem.equals(newItem));
Log.d(TAG, "areContentsTheSame Old Item is: " + oldItem.toString());
Log.d(TAG, "areContentsTheSame New Item is: " + newItem.toString());
return oldItem.equals(newItem);
}
};
Run Code Online (Sandbox Code Playgroud)
我有我的适配器:
import...
public class PostAdapter extends PagedListAdapter<Post, PostAdapter.PostViewHolder> {
interface...
protected PostAdapter() {
super(DIFF_CALLBACK);
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext()).inflate(R.layout.feed_card_view, parent, false);
return new PostViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
Post post = getItem(position);
Log.d(TAG, "onBindViewHolder with position: " + position);
if (post != null) {
setUpPost(holder, post);
Log.d(TAG, "onBindViewHolder with post: " + post.toString());
} else {
holder.clear();
Log.d(TAG, "onBindViewHolder post is null");
}
}
private void setUpPost(@NonNull PostViewHolder holder, @NonNull Post post) {
...do some things to set up post
}
class PostViewHolder extends RecyclerView.ViewHolder {
Buttons etc..
public PostViewHolder(View itemView) {
super(itemView);
button b = findviewbyid...
}
void clear() {
button.invalidate()..
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_feed, container, false);
feedViewModel = ViewModelProviders.of(getActivity()).get(FeedViewModel.class);
RecyclerView recyclerView = v.findViewById(R.id.post_list);
PostAdapter adapter = new PostAdapter(this);
feedViewModel.postList.observe(this, pagedList -> {
try {
//refresh current list
adapter.submitList(pagedList);
} catch (Exception e) {}
});
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
return v;
}
Run Code Online (Sandbox Code Playgroud)
我的视图模型
public class FeedViewModel extends AndroidViewModel {
private LiveData<PagedList<Post>> postList;
private FeedRepository feedRepository;
public FeedViewModel(final Application application) {
super(application);
feedRepository = new FeedRepository();
postList = feedRepository.getPosts();
}
public void refreshPosts() {
feedRepository.refreshPosts();
}
...
public LiveData<PagedList<Post>> getPostList() {
return postList;
}
}
Run Code Online (Sandbox Code Playgroud)
存储库
public class FeedRepository {
private LiveData<PagedList<Post>> postList;
private static final String TAG = FeedRepository.class.getName();
private static final int NUMBER_OF_POSTS_TO_GET = 2;
private static final boolean ENABLE_POST_PAGE_HOLDER = true;
private static final int NUMBER_OF_POSTS_TO_GET_IN_ADVANCE = 50;
private static final int NUMBER_OF_POSTS_ON_FIRST_LOAD = 20;
public FeedRepository() {
//Prefs on PagedList
PagedList.Config myPagingConfig = new PagedList.Config.Builder()
.setPageSize(NUMBER_OF_POSTS_TO_GET)
.setEnablePlaceholders(ENABLE_POST_PAGE_HOLDER)
.setPrefetchDistance(NUMBER_OF_POSTS_TO_GET_IN_ADVANCE)
.setInitialLoadSizeHint(NUMBER_OF_POSTS_ON_FIRST_LOAD)
.build();
PostDataSourceFactory postDataSourceFactory = new PostDataSourceFactory();
postList = new LivePagedListBuilder(
postDataSourceFactory, myPagingConfig).build();
}
public LiveData<PagedList<Post>> getPosts() {
return postList;
}
public void refreshPosts() {
Log.d(TAG, "refreshPosts, invalidateDataSources()");
postList.getValue().getDataSource().invalidate();
}
}
Run Code Online (Sandbox Code Playgroud)
数据源工厂
public class PostDataSourceFactory extends DataSource.Factory<String, Post> {
public PostDataSourceFactory() {
}
@NonNull
@Override
public DataSource<String, Post> create() {
PostDataSource postDataSource = new PostDataSource();
return postDataSource;
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里遵循谷歌示例:https : //developer.android.com/reference/android/arch/paging/PagedListAdapter.html
您可以Ctrl + Click在submitList功能上查看核心内部的功能。
public void submitList(final List<T> newList) {
if (newList == mList) {
// nothing to do if the newList & old List have the same address in memory
return;
}
....
}
Run Code Online (Sandbox Code Playgroud)
因此,尝试提交新的列表数据并重新检查
submitList(new ArrayList<>(list))
欲了解更多信息,您可以在这篇文章中查看完整答案
| 归档时间: |
|
| 查看次数: |
1386 次 |
| 最近记录: |