Latest

How to setEmptyView to RecyclerView

Right now I would really like to begin new collection about ideas for RecyclerView. Virtually all purposes display info in the lists. We’ve two primary elements for displaying record of knowledge in Android. It’s ListView and RecyclerView. The RecyclerView is extra environment friendly element from an Android documentation.

After first run some purposes has an empty record of knowledge, as instance Notes or ToDo record. In fact, it’s actually dangerous consumer experience to present empty listing. Ideally, we should always show some message which explains present state of affairs and/or how to add a knowledge.

Once we come again to ListView, this element has a easy technique which permits display a view when listing is empty. It is referred to as setEmptyView technique and I would really like to show you ways we will add this easy technique to the RecyclerView.

First, I’ll present how we will create a easy software with the RecyclerView and after it we’ll implement setEmptyView technique for our RecyclerView. In this article, I’ll present you the simplified code.

Easy RecyclerView

Let us start with dependencies. We should always add required dependencies to construct.gradle file.

apply plugin: ‘com.android.software’

android
compileSdkVersion 25
buildToolsVersion “25.0.2”

dependencies

compile ‘com.android.help:recyclerview-v7:25.1.zero’

Subsequent step is creating format for an activity for an Exercise or a Fragment.


And format for RecyclerView merchandise


We also should implement a easy mannequin class for Notes

public class Notice
personal String mTitle;

public Word(String title)
this.mTitle = title;

public String getTitle()
return mTitle;

@Override
public boolean equals(Object o)
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Notice observe = (Word) o;

return mTitle != null ? mTitle.equals(word.mTitle) : notice.mTitle == null;

@Override
public int hashCode()
return mTitle != null ? mTitle.hashCode() : 0;

@Override
public String toString()
return “Note” +
“title=’” + mTitle + ”’ +
‘’;

And we should always implement an adapter.

public class NotesAdapter extends RecyclerView.Adapter
personal Listing mNotes;

public void setNotes(Record notes)
this.mNotes = notes;
notifyDataSetChanged();

@Override
public NoteViewHolder onCreateViewHolder(ViewGroup mum or dad, int viewType)
View itemView = LayoutInflater.from(mum or dad.getContext())
.inflate(R.format.item_note, mum or dad, false);

return new NoteViewHolder(itemView);

@Override
public void onBindViewHolder(NoteViewHolder holder, int position)
holder.mTitleTextView.setText(mNotes.get(place).getTitle());

@Override
public int getItemCount()
return mNotes == null ? 0 : mNotes.measurement();

Our Adapter additionally required ViewHolder.

class NoteViewHolder extends RecyclerView.ViewHolder

protected TextView mTitleTextView;

NoteViewHolder(View itemView)
super(itemView);
mTitleTextView = (TextView) itemView.findViewById(R.id.title_text_view);

And eventually, we should always add an activity.

public class MainActivity extends AppCompatActivity
personal remaining static int DELAY = 3000;

personal Handler mHandler;
personal NotesAdapter mAdapter;

personal Runnable updateAdapterRunnable = new Runnable()
@Override
public void run()
mAdapter.setNotes(getTmpNotes());

;

@Override
protected void onCreate(Bundle savedInstanceState)
tremendous.onCreate(savedInstanceState);
setContentView(R.format.activity_main);

mAdapter = new NotesAdapter();

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);

mHandler = new Handler();

@Override
protected void onResume()
tremendous.onResume();
mHandler.postDelayed(updateAdapterRunnable, DELAY);

@Override
protected void onPause()
tremendous.onPause();
mHandler.removeCallbacks(updateAdapterRunnable);

personal static Listing getTmpNotes()
Listing listing = new ArrayList<>();
record.add(new Notice(“1st note”));
record.add(new Word(“2nd note”));
record.add(new Notice(“3rd note”));
record.add(new Observe(“4th note”));
record.add(new Notice(“5th note”));
listing.add(new Notice(“6th note”));
record.add(new Notice(“7th note”));
record.add(new Observe(“8th note”));
record.add(new Observe(“9th note”));
listing.add(new Notice(“10th note”));
record.add(new Observe(“11th note”));
listing.add(new Word(“12th note”));
listing.add(new Notice(“13th note”));
record.add(new Notice(“15th note”));
return record;

So, we have now

RecyclerView with a setEmptyView technique

The primary concept right here is to display a view when listing is empty. To start with, we should always create a custom RecyclerView. I would really like to inform you few phrases about AdapterDataObserver.

The AdapterDataObserver class include few strategies, like onChanged, onItemRangeChanged, onItemRangeChanged, onItemRangeInserted, onItemRangeMoved, onItemRangeRemoved.

onChanged()
onItemRangeChanged(int positionStart, int itemCount, Object payload)
onItemRangeChanged(int positionStart, int itemCount)
onItemRangeInserted(int positionStart, int itemCount)
onItemRangeMoved(int fromPosition, int toPosition, int itemCount)
onItemRangeRemoved(int positionStart, int itemCount)

These methods can notify your observer about any modifications.

After it might create a customized RecyclerView.

public class EmptyRecyclerView extends RecyclerView
personal View mEmptyView;

public EmptyRecyclerView(Context context)
super(context);

public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs)
tremendous(context, attrs);

public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);

personal void initEmptyView()
if (mEmptyView != null)
mEmptyView.setVisibility(
getAdapter() == null || getAdapter().getItemCount() == zero ? VISIBLE : GONE);
EmptyRecyclerView.this.setVisibility(
getAdapter() == null || getAdapter().getItemCount() == 0 ? GONE : VISIBLE);

last AdapterDataObserver observer = new AdapterDataObserver()
@Override
public void onChanged()
tremendous.onChanged();
initEmptyView();

@Override
public void onItemRangeInserted(int positionStart, int itemCount)
super.onItemRangeInserted(positionStart, itemCount);
initEmptyView();

@Override
public void onItemRangeRemoved(int positionStart, int itemCount)
super.onItemRangeRemoved(positionStart, itemCount);
initEmptyView();

;

@Override
public void setAdapter(Adapter adapter)
Adapter oldAdapter = getAdapter();
tremendous.setAdapter(adapter);

if (oldAdapter != null)
oldAdapter.unregisterAdapterDataObserver(observer);

if (adapter != null)
adapter.registerAdapterDataObserver(observer);

public void setEmptyView(View view)
this.mEmptyView = view;
initEmptyView();

We also needs to replace the format for an activity or a fragment.


Right here we now have new view (empty_view) and we changed android.help.v7.widget.RecyclerView to our new EmptyRecyclerView (com.alexzh.recyclerviewsetemptyview.EmptyRecyclerView).

We additionally ought to update the activity.

@Override
protected void onCreate(Bundle savedInstanceState)

EmptyRecyclerView recyclerView = (EmptyRecyclerView) findViewById(R.id.recycler_view);

recyclerView.setEmptyView(findViewById(R.id.empty_view));

We will additionally update Adapter and change public class NotesAdapter extends RecyclerView.Adapter to public class NotesAdapter extends EmptyRecyclerView.Adapter.

Right now, we’ve got better consumer experience.

Finally, I would really like to present a gist with simplified used code in this tutorial.

If this article was helpful to you, please share it. You can even subscribe to me on Twitter and get extra useful info related to Android improvement.