gpt4 book ai didi

android - 使用 DiffUtil ListAdapter 时 Recyclerview 没有动画

转载 作者:太空狗 更新时间:2023-10-29 13:04:42 28 4
gpt4 key购买 nike

我正在使用支持库中的 ListAdapter 和 LiveData 来观察我的 Room 数据库(我的实现类似于所示的 here )。出于某种原因,当数据更新时,我的回收站 View 没有动画。新项目被添加到适配器中,但它们仍然在回收站 View 之外,因此我必须手动向上滚动才能查看它们。为什么会这样?

ItemAdapter.java

public class ItemRecyclerAdapter extends ListAdapter<Item, PostViewHolder> {
public static final DiffUtil.ItemCallback<Item> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Item>() {
@Override
public boolean areItemsTheSame(
@NonNull Item oldItem, @NonNull Item newItem) {
return Objects.equals(oldItem.id, newItem.id);
}

@Override
public boolean areContentsTheSame(
@NonNull Item oldItem, @NonNull Item newItem) {
return Objects.equals(oldItem.content, newItem.content);
}
};

public ItemRecyclerAdapter() {
super(DIFF_CALLBACK);
}

@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new PostViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_item, parent, false));
}

@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
holder.onBind(getItem(position));
}
}

在我的 fragment 中 -

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new ItemRecyclerAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));

viewModel = ViewModelProviders.of...
viewModel.setData(...);
viewModel.getPosts().observe(this, listResource -> {
if (listResource != null && listResource.data != null) {
adapter.submitList(listResource.data);
}
});
}

最佳答案

喜欢使用下面的代码用户 pojo 类

    @Entity
public class User {
public static DiffCallback<User> DIFF_CALLBACK = new DiffCallback<User>() {
@Override
public boolean areItemsTheSame(@NonNull User oldItem, @NonNull User newItem) {
return oldItem.userId == newItem.userId;
}

@Override
public boolean areContentsTheSame(@NonNull User oldItem, @NonNull User newItem) {
return oldItem.equals(newItem);
}
};

@SerializedName("id")
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "user_id")
public long userId;

@SerializedName("login")
@ColumnInfo(name = "first_name")
public String firstName;
public String address;

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;

User user = (User) obj;

return user.userId == this.userId && user.firstName == this.firstName;
}
}

然后在适配器之后像下面的代码..

public class UserAdapter extends ListAdapter<User, UserAdapter.UserItemViewHolder> {


public UserAdapter() {
super(User.DIFF_CALLBACK);
}

@Override
public UserItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.item_user_list, parent, false);
return new UserItemViewHolder(view);
}

@Override
public void onBindViewHolder(UserItemViewHolder holder, int position) {
User user= getItem(position);
if(user!=null) {
holder.bindTo(user);
}
}

static class UserItemViewHolder extends RecyclerView.ViewHolder {
TextView userName, userId;

public UserItemViewHolder(View itemView) {
super(itemView);
userId = itemView.findViewById(R.id.userId);
userName = itemView.findViewById(R.id.userName);
}

public void bindTo(User user) {
userName.setText(user.firstName);
userId.setText(String.valueOf(user.userId));
}
}

然后像下面这样查看模型类..

public class UserViewModel extends ViewModel {

public LiveData<PagedList<User>> userList;

public UserViewModel() {

}

public void init(UserDao userDao) {
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder()).setEnablePlaceholders(true)
.setPrefetchDistance(10)
.setPageSize(20).build();

userList = (new LivePagedListBuilder(userDao.usersByFirstName(), pagedListConfig))
.build();
}

回收器 View 绑定(bind)数据,如下代码..

    private void initView() {
RecyclerView recyclerView = findViewById(R.id.plRvData);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
userDao=demoDatabase.getUserDao();
UserViewModel viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
viewModel.init(userDao);
final UserAdapter userUserAdapter = new UserAdapter();

viewModel.userList.observe(this, pagedList -> {
userUserAdapter.setList(pagedList);
});
Log.d("Vikas:::","Vikas PAndey");
recyclerView.setAdapter(userUserAdapter);

}

关于android - 使用 DiffUtil ListAdapter 时 Recyclerview 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49770416/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com