Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

java - Android ListAdapter.getItemCount() returns always 0

I try to show a TextView if the RecyclerView is empty. But getItemCount() of ListAdapter always returns 0, even if the RecyclerView shows items.

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());
        }
    }
}

Any help would be greatly appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Data will only get set after adapter::submitList call i.e after Live data observer call. So you need to make changes after it. There are a few ways how you can do that. One way is below for which you need to create adapter as global or just get it from 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);
    }
}

Set onLoaded as observer lambda.

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

You can also use submit list variant submitList(@Nullable List<String> list, @Nullable Runnable commitCallback) which will provide you a callback and u can do same stuff inside the Runnable. which is almost same thing in this case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...