在RecyclerView中等效ListView.setEmptyView

时间:2014-12-11 02:01:17

标签: android android-recyclerview recycler-adapter

RecyclerView中,我想设置一个空视图,以便在适配器为空时显示。是否有ListView.setEmptyView()的等价物?

13 个答案:

答案 0 :(得分:111)

这是一个类似@dragon出生的类,但更完整。基于this gist

public class EmptyRecyclerView extends RecyclerView {
    private View emptyView;
    final private AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkIfEmpty();
        }
    };

    public EmptyRecyclerView(Context context) {
        super(context);
    }

    public EmptyRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            final boolean emptyViewVisible = getAdapter().getItemCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        final Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            oldAdapter.unregisterAdapterDataObserver(observer);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(observer);
        }

        checkIfEmpty();
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
        checkIfEmpty();
    }
}

答案 1 :(得分:66)

使用新的data binding feature,您也可以直接在布局中实现此目的:

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

在这种情况下,您只需要在XML的数据部分添加变量和导入:

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

答案 2 :(得分:26)

this link中提供的解决方案似乎很完美。它使用viewType来标识何时显示emptyView。无需创建自定义RecyclerView

从以上链接添加代码:

package com.example.androidsampleproject;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RecyclerViewActivity extends Activity {

RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler_view);
    recyclerView = (RecyclerView) findViewById(R.id.myList);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(new MyAdapter());
}


private class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<String> dataList = new ArrayList<String>();

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView data;

        public ViewHolder(View v) {
            super(v);
            data = (TextView) v.findViewById(R.id.data_view);
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size() > 0 ? dataList.size() : 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (dataList.size() == 0) {
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder vho, final int pos) {
        if (vho instanceof ViewHolder) {
            ViewHolder vh = (ViewHolder) vho;
            String pi = dataList.get(pos);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;

        if (viewType == EMPTY_VIEW) {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view, parent, false);
            EmptyViewHolder evh = new EmptyViewHolder(v);
            return evh;
        }

        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_row, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    private static final int EMPTY_VIEW = 10;
}

}

答案 3 :(得分:8)

我只是喜欢像

这样的简单解决方案

让您的RecyclerView在FrameLayout或RelativeLayout中使用TextView或其他视图显示空数据消息,默认情况下可见性为GONE,然后在适配器类中应用逻辑

在这里,我有一个TextView,消息没有数据

@Override
public int getItemCount() {
    textViewNoData.setVisibility(data.size() > 0 ? View.GONE : View.VISIBLE);
    return data.size();
}

答案 4 :(得分:2)

我的版本,基于https://gist.github.com/adelnizamutdinov/31c8f054d1af4588dc5c

public class EmptyRecyclerView extends RecyclerView {
    @Nullable
    private View emptyView;

    public EmptyRecyclerView(Context context) { super(context); }

    public EmptyRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }

    public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            emptyView.setVisibility(getAdapter().getItemCount() > 0 ? GONE : VISIBLE);
        }
    }

    private final AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkIfEmpty();
        }
    };

    @Override
    public void setAdapter(@Nullable Adapter adapter) {
        final Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            oldAdapter.unregisterAdapterDataObserver(observer);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(observer);
        }
        checkIfEmpty();
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (null != emptyView && (visibility == GONE || visibility == INVISIBLE)) {
            emptyView.setVisibility(GONE);
        } else {
            checkIfEmpty();
        }
    }

    public void setEmptyView(@Nullable View emptyView) {
        this.emptyView = emptyView;
        checkIfEmpty();
    }
}

答案 5 :(得分:2)

我更愿意在Recycler.Adapter

中实现此功能

在重写的getItemCount方法中,在那里注入空的校验码:

@Override
public int getItemCount() {
    if(data.size() == 0) listIsEmtpy();
    return data.size();
}

答案 6 :(得分:2)

如果您想支持更多状态,例如加载状态,错误状态,那么您可以结帐https://github.com/rockerhieu/rv-adapter-states。否则,可以使用(https://github.com/rockerhieu/rv-adapter)中的RecyclerViewAdapterWrapper轻松实现支持空视图。这种方法的主要优点是您可以轻松支持空视图而无需更改现有适配器的逻辑:

public class StatesRecyclerViewAdapter extends RecyclerViewAdapterWrapper {
    private final View vEmptyView;

    @IntDef({STATE_NORMAL, STATE_EMPTY})
    @Retention(RetentionPolicy.SOURCE)
    public @interface State {
    }

    public static final int STATE_NORMAL = 0;
    public static final int STATE_EMPTY = 2;

    public static final int TYPE_EMPTY = 1001;

    @State
    private int state = STATE_NORMAL;

    public StatesRecyclerViewAdapter(@NonNull RecyclerView.Adapter wrapped, @Nullable View emptyView) {
        super(wrapped);
        this.vEmptyView = emptyView;
    }

    @State
    public int getState() {
        return state;
    }

    public void setState(@State int state) {
        this.state = state;
        getWrappedAdapter().notifyDataSetChanged();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        switch (state) {
            case STATE_EMPTY:
                return 1;
        }
        return super.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        switch (state) {
            case STATE_EMPTY:
                return TYPE_EMPTY;
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_EMPTY:
                return new SimpleViewHolder(vEmptyView);
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (state) {
            case STATE_EMPTY:
                onBindEmptyViewHolder(holder, position);
                break;
            default:
                super.onBindViewHolder(holder, position);
                break;
        }
    }

    public void onBindEmptyViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    public static class SimpleViewHolder extends RecyclerView.ViewHolder {
        public SimpleViewHolder(View itemView) {
            super(itemView);
        }
    }
}

用法:

Adapter adapter = originalAdapter();
StatesRecyclerViewAdapter statesRecyclerViewAdapter = new StatesRecyclerViewAdapter(adapter, emptyView);
rv.setAdapter(endlessRecyclerViewAdapter);

// Change the states of the adapter
statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_EMPTY);
statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_NORMAL);

答案 7 :(得分:2)

我已经解决了这个问题:
创建布局layout_recyclerview_with_emptytext.xml文件 创建了EmptyViewRecyclerView.java
---------

EmptyViewRecyclerView emptyRecyclerView =(EmptyViewRecyclerView)findViewById(R.id.emptyRecyclerViewLayout);
 emptyRecyclerView.addAdapter(mPrayerCollectionRecyclerViewAdapter,&#34;所选类别没有祷告。&#34;);

layout_recyclerview_with_emptytext.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/switcher"
>

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

<com.ninestars.views.CustomFontTextView android:id="@+id/recyclerViewEmptyTextView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Empty Text"
    android:layout_gravity="center"
    android:gravity="center"
    android:textStyle="bold"
    />

    </merge>


EmptyViewRecyclerView.java

public class EmptyViewRecyclerView extends ViewSwitcher {
private RecyclerView mRecyclerView;
private CustomFontTextView mRecyclerViewExptyTextView;

public EmptyViewRecyclerView(Context context) {
    super(context);
    initView(context);
}

public EmptyViewRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);
}


private void initView(Context context) {
    LayoutInflater.from(context).inflate(R.layout.layout_recyclerview_with_emptytext, this, true);
    mRecyclerViewExptyTextView = (CustomFontTextView) findViewById(R.id.recyclerViewEmptyTextView);
    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
}

public void addAdapter(final RecyclerView.Adapter<?> adapter) {
    mRecyclerView.setAdapter(adapter);
    adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            if(adapter.getItemCount() > 0) {
                if (R.id.recyclerView == getNextView().getId()) {
                    showNext();
                }
            } else {
                if (R.id.recyclerViewEmptyTextView == getNextView().getId()) {
                    showNext();
                }
            }
        }
    });
}

public void addAdapter(final RecyclerView.Adapter<?> adapter, String emptyTextMsg) {
    addAdapter(adapter);
    setEmptyText(emptyTextMsg);
}

public RecyclerView getRecyclerView() {
    return mRecyclerView;
}

public void setEmptyText(String emptyTextMsg) {
    mRecyclerViewExptyTextView.setText(emptyTextMsg);
}

}

答案 8 :(得分:2)

尝试http://sample.com/

这是RVEmptyObserver的实现,您只需将AdapterDataObserver设置为View的默认空布局即可。这样,您可以轻松地将其与现有代码一起使用,而不是使用自定义RecylerView并使您的生活更加艰难:

使用示例:

RecyclerView

您可以在此处的实际应用中see the codeexample usage

<强>类别:

RVEmptyObserver observer = new RVEmptyObserver(recyclerView, emptyView)
rvAdapter.registerAdapterDataObserver(observer);

答案 9 :(得分:1)

public class EmptyRecyclerView extends RecyclerView {
  @Nullable View emptyView;

  public EmptyRecyclerView(Context context) { super(context); }

  public EmptyRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }

  public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  void checkIfEmpty() {
    if (emptyView != null) {
      emptyView.setVisibility(getAdapter().getItemCount() > 0 ? GONE : VISIBLE);
    }
  }

  final @NotNull AdapterDataObserver observer = new AdapterDataObserver() {
    @Override public void onChanged() {
      super.onChanged();
      checkIfEmpty();
    }
  };

  @Override public void setAdapter(@Nullable Adapter adapter) {
    final Adapter oldAdapter = getAdapter();
    if (oldAdapter != null) {
      oldAdapter.unregisterAdapterDataObserver(observer);
    }
    super.setAdapter(adapter);
    if (adapter != null) {
      adapter.registerAdapterDataObserver(observer);
    }
  }

  public void setEmptyView(@Nullable View emptyView) {
    this.emptyView = emptyView;
    checkIfEmpty();
  }
}

这样的事情可能会有所帮助

答案 10 :(得分:1)

RecyclerView为空时,您可以在empty上绘制文字。以下自定义子类支持failedloadingofflinerecyclerView_stateText模式。要成功编译,请为您的资源添加/** * {@code RecyclerView} that supports loading and empty states. */ public final class SupportRecyclerView extends RecyclerView { public enum State { NORMAL, LOADING, EMPTY, FAILED, OFFLINE } public SupportRecyclerView(@NonNull Context context) { super(context); setUp(context); } public SupportRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); setUp(context); } public SupportRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setUp(context); } private Paint textPaint; private Rect textBounds; private PointF textOrigin; private void setUp(Context c) { textPaint = new Paint(); textPaint.setAntiAlias(true); textPaint.setColor(ContextCompat.getColor(c, R.color.recyclerView_stateText)); textBounds = new Rect(); textOrigin = new PointF(); } private State state; public State state() { return state; } public void setState(State newState) { state = newState; calculateLayout(getWidth(), getHeight()); invalidate(); } private String loadingText = "Loading..."; public void setLoadingText(@StringRes int resId) { loadingText = getResources().getString(resId); } private String emptyText = "Empty"; public void setEmptyText(@StringRes int resId) { emptyText = getResources().getString(resId); } private String failedText = "Failed"; public void setFailedText(@StringRes int resId) { failedText = getResources().getString(resId); } private String offlineText = "Offline"; public void setOfflineText(@StringRes int resId) { offlineText = getResources().getString(resId); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); String s = stringForCurrentState(); if (s == null) return; canvas.drawText(s, textOrigin.x, textOrigin.y, textPaint); } private void calculateLayout(int w, int h) { String s = stringForCurrentState(); if (s == null) return; textPaint.setTextSize(.1f * w); textPaint.getTextBounds(s, 0, s.length(), textBounds); textOrigin.set( w / 2f - textBounds.width() / 2f - textBounds.left, h / 2f - textBounds.height() / 2f - textBounds.top); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); calculateLayout(w, h); } private String stringForCurrentState() { if (state == State.EMPTY) return emptyText; else if (state == State.LOADING) return loadingText; else if (state == State.FAILED) return failedText; else if (state == State.OFFLINE) return offlineText; else return null; } } 颜色。

barcodes.valueAt(0).displayValue.getBytes("iso-8859-1");

答案 11 :(得分:1)

从我的观点来看,如何进行空视图的最简单方法是创建新的空RecyclerView,其中包含要作为背景充气的布局。 当您检查数据集大小时,将设置此空白适配器。

答案 12 :(得分:0)

一种更易用、更节省资源的设置空标签的方式 的 RecyclerView。

简而言之,引入了一个名为 RecyclerViewEmpty 的新视图。 在其 onDraw 方法中,如果适配器为空,则只绘制一个空标签 在它的中心,否则继续 super.onDraw();

class RecyclerViewEmpty extends RecyclerView {
    ....
    @Override
    public void onDraw(Canvas canvas) {
        Adapter a = this.getAdapter();
        if(a==null || a.getItemCount()<1) {
            int x= (this.getWidth()-strWidth)>>1;
            int y = this.getHeight()>>1 ;
            canvas.drawText(this.emptyLabel, x, y, labelPaint);
        }
        else {
            super.onDraw(canvas);
        }
    }
    ....
}

详情请参考代码: https://github.com/stzdzyhs/recyclerview-demo

相关问题