PYPL PYPL - 6 months ago 155
Android Question

Cant trigger empty view of recyclerview

I have a recyclerview where i want to show an text/image when it's empty but i cant find a way to make it work.

recyclerview xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:name="ViolationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"

app:layoutManager="LinearLayoutManager"
tools:context=".ViolationFragment"
tools:listitem="@layout/fragment_violation" />

<TextView
android:id="@+id/list_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty" />
</android.support.v4.widget.SwipeRefreshLayout>


fragment

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

context = getActivity();

View view = inflater.inflate(R.layout.fragment_violation_list, container, false);
swipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Refresh items
updateList();
}
});

//Filters
filterPaid = ((ViolationBrowser) context).filterPaid;
filterUnpaid = ((ViolationBrowser) context).filterUnpaid;

recyclerView = (RecyclerView) view.findViewById(R.id.list);
TextView emptyView = (TextView) view.findViewById(R.id.list_empty);

adapter = new ViolationAdapter(context, getData());
adapter.SetOnItemClickListener(new ViolationAdapter.OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
showDetailedViolationData(position);
}
});

recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
if (adapter.getItemCount() >0) {
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
return view;
}


adapter

public class ViolationAdapter extends RecyclerView.Adapter<ViolationAdapter.MyViewHolder> {

OnItemClickListener mItemClickListener;
List<ViolationItem> data = Collections.emptyList();
private LayoutInflater inflater;

public ViolationAdapter(Context context, List<ViolationItem> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.fragment_violation, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ViolationItem current = data.get(position);
holder.carName.setText(current.carName);
holder.amount.setText(current.amount);
holder.date.setText(current.date);

if (current.payStatus == 2) holder.payStatus.setImageResource(R.drawable.status_paid);
else if (current.payStatus == 1)
holder.payStatus.setImageResource(R.drawable.status_part_paid);
else holder.payStatus.setImageResource(R.drawable.status_not_paid);
}

@Override
public int getItemCount() {
return data.size();
}

public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}

public ViolationItem getItem(int position) {
return data.get(position);
}

public void addData(List<ViolationItem> data) {
for (ViolationItem itm : data) {
this.data.add(itm);
notifyDataSetChanged();
}
}

public void clearData() {
int size = data.size();
data.clear();
notifyItemRangeRemoved(0, size);
}

public interface OnItemClickListener {
public void onItemClick(View view, int position);
}

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView carName, amount, date;
ImageView payStatus, showAllDetails;

public MyViewHolder(View itemView) {
super(itemView);
carName = (TextView) itemView.findViewById(R.id.itemCarName);
amount = (TextView) itemView.findViewById(R.id.itemAmount);
date = (TextView) itemView.findViewById(R.id.itemDate);
payStatus = (ImageView) itemView.findViewById(R.id.payStatus);
showAllDetails = (ImageView) itemView.findViewById(R.id.showAllDetails);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (mItemClickListener != null) mItemClickListener.onItemClick(v, getLayoutPosition());
}
}
}


What should i do to make it work? i still see an empty list instead of a text

Answer

In your example the below condition should be like this:

if (adapter.getItemCount() <=0) 
{
    recyclerView.setVisibility(View.GONE);
    emptyView.setVisibility(View.VISIBLE);
} 
else
{
    recyclerView.setVisibility(View.VISIBLE);
    emptyView.setVisibility(View.GONE);
}

Also, in the xml layout:

 <TextView
    android:id="@+id/list_empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Empty" />

Place the "list_empty" outside the SwipeRefreshLayout.

See below the complete example: It shows empty view, if the recycler view is empty.

public class MainActivity extends AppCompatActivity {

SwipeRefreshLayout mSwipeRefreshLayout;
RecyclerView recyclerView;
List<String> arrayList = null;
MyAdapter mAdapter = null;
TextView list_empty;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    recyclerView = (RecyclerView)findViewById(R.id.itemsRecyclerView);
    list_empty = (TextView)findViewById(R.id.list_empty);
    arrayList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.mobile)));
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    mAdapter = new MyAdapter((ArrayList<String>)arrayList);

    recyclerView.setAdapter(mAdapter);       

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshContent();
        }
    });

}

private void refreshContent(){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

           arrayList.clear();
            mAdapter.notifyDataSetChanged();

            if(mAdapter.getItemCount()<=0)
            {
                recyclerView.setVisibility(View.GONE);
                list_empty.setVisibility(View.VISIBLE);
            }
            else
            {
                recyclerView.setVisibility(View.VISIBLE);
                list_empty.setVisibility(View.GONE);
            }
            // Stop refresh animation
            mSwipeRefreshLayout.setRefreshing(false);
        };
    },2000);
}

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<String> itemsData;

    public MyAdapter(ArrayList<String> itemsData) {
        this.itemsData = itemsData;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item, null);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

   viewHolder.txtViewTitle.setText(itemsData.get(position));


    }

    // inner class to hold a reference to each item of RecyclerView
    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtViewTitle;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            txtViewTitle = (TextView)
itemLayoutView.findViewById(R.id.item_title);
        }
    }


    // Return the size of your itemsData (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return itemsData.size();
    }
  }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.test.demotest.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        />
</android.support.v4.widget.SwipeRefreshLayout>

    <TextView
        android:id="@+id/list_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Empty"
        android:layout_gravity="center"
        android:visibility="gone"/>

</FrameLayout>
</RelativeLayout>

item.xml:

<!-- title -->
<TextView
    android:id="@+id/item_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/darker_gray"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:textSize="22dp" />

</RelativeLayout>