gpt4 book ai didi

java - Android ListAdapter.getItemCount() 总是返回 0

转载 作者:行者123 更新时间:2023-12-04 23:57:41 25 4
gpt4 key购买 nike

如果 RecyclerView 为空,我会尝试显示 TextView。但是 ListAdaptergetItemCount() 总是返回 0,即使 RecyclerView 显示项目也是如此。

fragment .java

public class ProjectsFragment extends Fragment {

private RepoViewModel repoViewModel;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FloatingActionButton floatingActionButton = ((MainActivity) requireActivity()).getFloatingActionButton();
floatingActionButton.setVisibility(View.VISIBLE);

return inflater.inflate(R.layout.fragment_projects, container, false);
}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

TextView textview_no_project = view.findViewById(R.id.textview_no_project);

RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity().getApplicationContext()));
final RepoListAdapter adapter = new RepoListAdapter(new RepoListAdapter.RepoDiff());

if (adapter.getItemCount() == 0) {
recyclerView.setVisibility(View.INVISIBLE);
textview_no_project.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
textview_no_project.setVisibility(View.INVISIBLE);
}
recyclerView.setAdapter(adapter);
repoViewModel = new ViewModelProvider(requireActivity()).get(RepoViewModel.class);
repoViewModel.getAllRepos().observe(getViewLifecycleOwner(), adapter::submitList);
}
}

RepoListAdapter.java

public class RepoListAdapter extends ListAdapter<Repo, RepoViewHolder> {

public RepoListAdapter(@NonNull DiffUtil.ItemCallback<Repo> diffCallback) {
super(diffCallback);
}

@NonNull
@Override
public RepoViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
return RepoViewHolder.create(viewGroup);
}

@Override
public void onBindViewHolder(RepoViewHolder holder, int position) {
Repo current = getItem(position);
holder.bind(current.getName());
}

public static class RepoDiff extends DiffUtil.ItemCallback<Repo> {

@Override
public boolean areItemsTheSame(@NonNull Repo oldItem, @NonNull Repo newItem) {
return oldItem == newItem;
}

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

如有任何帮助,我们将不胜感激。

最佳答案

数据只会在 adapter::submitList 之后设置调用即在实时数据观察器调用之后。所以你需要在它之后进行更改。有几种方法可以做到这一点。以下是您需要将适配器创建为全局适配器或仅从 RecyclerView 获取它的一种方法。

public void onLoaded(List<Repo> list){
if (list.size() == 0) {
recyclerView.setVisibility(View.INVISIBLE);
textview_no_project.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
textview_no_project.setVisibility(View.INVISIBLE);
adapter.submitList(list);
}
}

设置onLoaded作为观察者 lambda。

repoViewModel.getAllRepos().observe(getViewLifecycleOwner(), this::onLoaded);

您还可以使用提交列表变体 submitList(@Nullable List<String> list, @Nullable Runnable commitCallback)这将为您提供回调,您可以在 Runnable 中做同样的事情.在这种情况下,这几乎是一回事。

关于java - Android ListAdapter.getItemCount() 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65668892/

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