2

Usually on a RecyclerView I show an empty view when there are no items on the RecyclerView and since I control all updates to the RecyclerView via the notify methods then that is pretty simple but with PagedListAdapter updates just seem to happen on the background, so how can I hide or show my empty view?

For example, if I call deleteItem() on my Room DB, the PagedListAdapter will be updated on its own without me calling notifyItemDeleted but if it was the last item on the list, how does my code know to show the empty view? I could query the DB each time an action happens for the count but that seems wasteful. Is there a better way?

casolorz
  • 8,486
  • 19
  • 93
  • 200
  • Create a separate adapter for empty views.. easiest approach... Others are complicated for `pagedList` – Santanu Sur May 30 '19 at 17:34
  • `PagedListAdapter` does not update on its own. As far as I know it accept new `PagedList` through `.submitList()` method. Therefore you can check emptyness in the same `LiveData` observer, or you can choose to override `.submitList()` method of your `PagedListAdapter` and check list emptyness there. – Sanlok Lee May 30 '19 at 18:31
  • @SanlokLee you appear to be correct, if you want make an answer for it, I will gladly mark it as correct. – casolorz May 30 '19 at 19:15
  • You can refer from this answer: https://stackoverflow.com/a/61845719/7438254 – Sikander Bakht May 17 '20 at 01:14

2 Answers2

5

As mentioned in the comment, you can test emptiness of the list in the same LiveData observer you use for .submitList().

Java Example:

I am assuming you are following something similar to this snippet found in the PagedListAdapter document. I am simply adding emptiness check to that sample code.

class MyActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
        RecyclerView recyclerView = findViewById(R.id.user_list);
        UserAdapter<User> adapter = new UserAdapter();
        viewModel.usersList.observe(this, pagedList -> {
            // Check if the list is empty
            updateView(pagedList.size());
            adapter.submitList(pagedList));

            pagedList.addWeakCallback(null, new PagedList.Callback() {

                @Override
                public void onChanged(int position, int count) {
                    updateView(pagedList.size())
                    // updateView(adapter.getItemCount())
                }

                ...
            }
        }
        recyclerView.setAdapter(adapter);
    }

    private void updateView(int itemCount) {
        if (itemCount > 0) {
            // The list is not empty. Show the recycler view.
            recyclerView.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        } else {
            // The list is empty. Show the empty list view
            recyclerView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }

    }
}

Kotlin Example:

The above Java example is actually just a Java translation of Kotlin example I found in this Android Paging codelab.

Sanlok Lee
  • 3,404
  • 2
  • 15
  • 25
0

It's not the best solution, but you can give it a try

    myViewModel.getMyPagedList().observe(MainActivity.this, items -> {
        myPagedListAdapter.submitList(items);
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (myPagedListAdapter.getItemCount() == 0) {
                    runOnUiThread(() -> {
                        emptyTextView.setText("Empty");
                        emptyTextView.setVisibility(View.VISIBLE);
                    });
                    // The Runnable will be re-executed (repeated) as long as if condition is true
                    handler.postDelayed(this, 100);
                } else
                    emptyTextView.setVisibility(View.GONE);
            }
        };
        // trigger first time
        handler.postDelayed(runnable, 1000);
    });
Hasan El-Hefnawy
  • 1,249
  • 1
  • 14
  • 20