Equivalent of Listview.Setemptyview in Recyclerview

Equivalent of ListView.setEmptyView in RecyclerView

With the new data binding feature you can also achieve this in your layout directly:

<TextView
android:text="No data to display."
android:visibility="@{dataset.size() > 0 ? View.GONE : View.VISIBLE}" />

In that case you just need to add a variable and an import to the data section of your XML:

<data>
<import type="android.view.View"/>
<variable
name="dataset"
type="java.util.List<java.lang.String>"
/>
</data>

How to show an empty view with a RecyclerView?

On the same layout where is defined the RecyclerView, add the TextView:

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />

<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="@string/no_data_available" />

At the onCreate or the appropriate callback you check if the dataset that feeds your RecyclerView is empty.
If the dataset is empty, the RecyclerView is empty too. In that case, the message appears on the screen.
If not, change its visibility:

private RecyclerView recyclerView;
private TextView emptyView;

// ...

recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
emptyView = (TextView) rootView.findViewById(R.id.empty_view);

// ...

if (dataset.isEmpty()) {
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}

recyclerview setEmptyView implementation - android

It looks like you haven't added your emptyView to the hierarchy anywhere, when you inflate it you set the parent as null and while you toggle its visibility when it is in the listView you never actually add it as a child of any view. As a result, it exists but it isn't displayed on the screen. To get it show up you can inflate it with a parent view or add it to the parent view after inflation:

getLayoutInflater().inflate(R.layout.empty_layout, contentView);

or

contentView.addView(emptyView);

(The contentView is the view that is holding your recyclerView)

Data-bound visibility for RecyclerView's empty view has no effect

So I ended up fixing this after reading here that SwipeRefreshLayout must have one child, so I wrapped the RecyclerView and RelativeLayout in a FrameLayout. I also used this solution to inflate the Fragment's layout with the DataBindingUtil inflate() method and setting the binding in the Fragment's onCreateView() method.

setEmptyView on ListView not showing its view in a android app?

the problem is, i have to do a addContentView:

View empty = getLayoutInflater().inflate(R.layout.empty_list_item, null, false);
addContentView(empty, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
view.setEmptyView(empty);

now its fine

How to stop setEmptyView displaying a TextView while populating a ListView with Android?

If your adapter has this method, you can set empty view in this method,

adapter.registerDataSetObserver(new DataSetObserver() {

@Override
public void onChanged() {
super.onChanged();

TextView emptyView = (TextView) findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);

}
});

Empty View on a RecyclerView

I updated your code:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:name="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment"
android:layout_width="match_parent"
tools:context="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/grocery_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/grocery_item" />

<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="NO DATA AVAILABLE" />
</FrameLayout>

Your Fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_grocerylist, container, false);

rv = view.findViewById(R.id.grocery_list);
TextView emptyView = (TextView)view.findViewById(R.id.empty_view);

dh = new DatabaseHandler(view.getContext());

dataset = dh.getGroceries();

Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) rv;
recyclerView.setLayoutManager(new LinearLayoutManager(context));

adapter = new GroceryListRecyclerViewAdapter(dataset,mListener,this);
recyclerView.setAdapter(adapter);

ItemTouchHelper.Callback callback = new SimpleTouchHelperCallback(adapter);
ith = new ItemTouchHelper(callback);
ith.attachToRecyclerView(recyclerView);

DividerItemDecoration div = new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.VERTICAL);

recyclerView.addItemDecoration(div);

if (dataset.isEmpty()){
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}

return view;
}


Related Topics



Leave a reply



Submit