PullToRefresh-ListView icon indicating copy to clipboard operation
PullToRefresh-ListView copied to clipboard

Empty view cannot be set while there is no items in the list

Open suji099 opened this issue 13 years ago • 7 comments

When there is no items in the list view i have to set an emptyview .In the current code there is no option to set an empty view .

suji099 avatar Aug 20 '12 13:08 suji099

The PullToRefreshListView does not override #setEmptyView of ListView, so you can just use http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View) or set the empty view in XML like you would on any other regular ListView.

erikwt avatar Sep 20 '12 09:09 erikwt

I have to agree that this is a bug. When using setEmptyView, you lose the functionality to pull to refresh when the list is indeed empty. Also it is very gltchy when refreshing (as the list goes empty for a brief second while updating and you see the empty view).

vincentjames501 avatar Sep 24 '12 17:09 vincentjames501

Yes, you are right. I'm re-opening the issue and will look into it ASAP.

erikwt avatar Sep 25 '12 08:09 erikwt

I thought I'd just go ahead and put my solution:

In PullToRefreshListView add the following:

private TextView emptyView; private String noItemsText;

public void removeEmptyHeaderView() {
    if(getHeaderViewsCount()!=1) {
        removeHeaderView(emptyView);
    }
}

//modify public void onRefreshComplete() { state = State.PULL_TO_REFRESH; if (emptyView != null) { if (getAdapter().isEmpty()) { if (getHeaderViewsCount() == 1) { emptyView.setHeight(getHeight()); emptyView.setText(noItemsText); addHeaderView(emptyView); } } else { if (getHeaderViewsCount() == 2) { removeHeaderView(emptyView); } } } resetHeader(); }

//in init() add noItemsText = getContext().getString(R.string.ptr_no_items_text); //modify string xml file

    emptyView = buildDefaultEmptyView();

//add this method - this can be refactored to use styles private TextView buildDefaultEmptyView() { TextView emptyView = new TextView(getContext()); emptyView.setText(noItemsText); emptyView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT)); emptyView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL); emptyView.setTextColor(Color.BLACK); emptyView.setTextSize(20); return emptyView; }

public void setNoItemsText(String noItemsText) { this.noItemsText = noItemsText; }

public void setEmtpyView(TextView view) { this.emptyView = view; }

This can be refactored but simply gives a default empty view with a modifiable TextView

vincentjames501 avatar Sep 25 '12 14:09 vincentjames501

a pull request >?

tagrudev avatar Oct 10 '12 12:10 tagrudev

A pull request would be nice...

erikwt avatar Oct 25 '12 09:10 erikwt

I need the setEmptyView functionality and would be really happy if it´s solved. The solution vincentjames501 provided works, but not initially. The list is empty from the beginning and the text is not shown until a refresh is done. I want to tell the user that he needs to refresh. I tried to call the onRefreshComplete() manually but it will not update the header for some reason?

Joohansson avatar Jun 09 '13 10:06 Joohansson