【发布时间】:2019-05-05 23:14:45
【问题描述】:
我一直在复制 Sunflower android 架构最佳实践应用程序,到目前为止速度很慢但很成功,虽然我的代码可以运行,但它显示一条未经检查的消息,所以我运行了它询问的命令,它是这样说的:
[unchecked] 未选中调用 submitList(List) 作为 原始类型 ListAdapter,其中 T 是类型变量:T extends Object 在 ListAdapter 类中声明
这可能并不重要,但它让我很烦,我用作参考的那个在 ListAdapter 周围从来没有任何 或任何东西,也没有任何警告,所以我不确定我的原因。
我的片段:
public class NewsFeedFragment extends Fragment {
private PinViewModel viewModel;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FragmentNewsFeedBinding binding = FragmentNewsFeedBinding.inflate(inflater, container, false);
PinViewModelFactory factory = InjectorUtils.providePinListViewModelFactory(getContext());
ListAdapter adapter = new PinListAdapter();
binding.pinList.setAdapter(adapter);
this.viewModel = ViewModelProviders.of(this, factory).get(PinViewModel.class);
subscribeUi(adapter);
return binding.getRoot();
}
private void subscribeUi(ListAdapter adapter) {
this.viewModel.pins.observe(getViewLifecycleOwner(), pins -> {
if (pins != null) {
adapter.submitList(pins);
}
});
}
}
我的视图模型:
public class PinViewModel extends ViewModel {
private PinRepository pinRepository;
public LiveData<List<Pin>> pins;
PinViewModel(@NonNull PinRepository pinRepository) {
this.pinRepository = pinRepository;
this.pins = this.pinRepository.getPins();
}
}
我的适配器:
public class PinListAdapter extends ListAdapter<Pin, PinListAdapter.ViewHolder>{
public PinListAdapter() {
super(new PinDiffCallback());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(ListItemPinBinding.inflate(
LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Pin pin = getItem(position);
holder.bind(pin);
holder.itemView.setTag(pin);
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ListItemPinBinding binding;
ViewHolder(@NonNull ListItemPinBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
void bind(Pin item) {
binding.setPin(item);
binding.executePendingBindings();
}
}
static class PinDiffCallback extends DiffUtil.ItemCallback<Pin> {
@Override
public boolean areItemsTheSame(@NonNull Pin oldItem, @NonNull Pin newItem) {
return oldItem.getId() == (newItem.getId());
}
@Override
public boolean areContentsTheSame(@NonNull Pin oldItem, @NonNull Pin newItem) {
return oldItem == newItem;
}
}
}
【问题讨论】:
标签: java android android-jetpack