How's RecyclerView get the text that it's empty?



  • RecyclerView is filled with a list via LinearLayoutManager, the question is, when the list is empty, like RecyclerView, to get the text that it's empty?



  • So, what I invented is that I created a caste-based adapter:

    private class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.list_empty_my, parent, false);
            return new RecyclerView.ViewHolder(view) {};
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        }
    
        @Override
        public int getItemCount() {
            return 1;
        }
    }
    

    My liot:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    &lt;TextView
        android:id="@+id/list_item_my_empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/empty_list"
        android:padding="4dp"
        android:gravity="center"/&gt;
    

    </RelativeLayout>

    And I put it in the code.

    List<MyModel> models = MyModelLab.get().getList();
    if(models.size() == 0){
    mMyRecyclerView.setAdapter(new EmptyAdapter());
    return;
    }

    The solution was created before the topic, it just didn't work, as the whole problem was that the way getItemCount () was returning 0, which left nothing on the screen, replaced it with one and the block on the screen.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2