Reputation: 520
I am trying to place an empty view when my list is empty in recycler view but it't not working. I tried following code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_notification, container, false);
((AppCompatActivity) getActivity()).getSupportActionBar().show();
initVar();
initViews();
loadData();
if (notifications.isEmpty()){
recyclerView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
}
return view;
}
And XML Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notification_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/empty_iv_notification"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:visibility="gone"
android:src="@drawable/ic_empty" />
</LinearLayout>
Though list have data it still showing empty view. I could not help my self with stackoverflow answer since, all answer i tried are same and did not work. can anybody help me.
Upvotes: 0
Views: 142
Reputation: 1431
put below code in loadData() when your data is loaded i mean if your list is empty then below code executed.
if (notifications.isEmpty()){
recyclerView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
}
Upvotes: 3