Created
February 5, 2017 17:56
-
-
Save AlexZhukovich/537eaa1e3c82ef9f5d5cd22efdc80c54 to your computer and use it in GitHub Desktop.
How to setEmptyView to RecyclerView - tutorial (http://alexzh.com/tutorials/how-to-setemptyview-to-recyclerview/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<com.alexzh.recyclerviewsetemptyview.EmptyRecyclerView | |
android:id="@+id/recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<TextView | |
android:id="@+id/empty_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="Add more notes" | |
android:textSize="36sp" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexzh.recyclerviewsetemptyview; | |
import android.content.Context; | |
import android.support.annotation.Nullable; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.AttributeSet; | |
import android.view.View; | |
public class EmptyRecyclerView extends RecyclerView { | |
private View mEmptyView; | |
public EmptyRecyclerView(Context context) { | |
super(context); | |
} | |
public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
private void initEmptyView() { | |
if (mEmptyView != null) { | |
mEmptyView.setVisibility( | |
getAdapter() == null || getAdapter().getItemCount() == 0 ? VISIBLE : GONE); | |
EmptyRecyclerView.this.setVisibility( | |
getAdapter() == null || getAdapter().getItemCount() == 0 ? GONE : VISIBLE); | |
} | |
} | |
final AdapterDataObserver observer = new AdapterDataObserver() { | |
@Override | |
public void onChanged() { | |
super.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(); | |
super.setAdapter(adapter); | |
if (oldAdapter != null) { | |
oldAdapter.unregisterAdapterDataObserver(observer); | |
} | |
if (adapter != null) { | |
adapter.registerAdapterDataObserver(observer); | |
} | |
} | |
public void setEmptyView(View view) { | |
this.mEmptyView = view; | |
initEmptyView(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<TextView | |
android:id="@+id/title_text_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="12dp" | |
android:layout_marginRight="12dp" | |
android:layout_marginTop="8dp" | |
android:layout_marginBottom="8dp" | |
android:textSize="24sp"/> | |
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexzh.recyclerviewsetemptyview; | |
import android.os.Handler; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.DefaultItemAnimator; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
private final static int DELAY = 3000; | |
private Handler mHandler; | |
private NotesAdapter mAdapter; | |
private Runnable updateAdapterRunnable = new Runnable() { | |
@Override | |
public void run() { | |
mAdapter.setNotes(getTmpNotes()); | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mAdapter = new NotesAdapter(); | |
EmptyRecyclerView recyclerView = (EmptyRecyclerView) findViewById(R.id.recycler_view); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
recyclerView.setItemAnimator(new DefaultItemAnimator()); | |
recyclerView.setEmptyView(findViewById(R.id.empty_view)); | |
recyclerView.setAdapter(mAdapter); | |
mHandler = new Handler(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
mHandler.postDelayed(updateAdapterRunnable, DELAY); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mHandler.removeCallbacks(updateAdapterRunnable); | |
} | |
private static List<Note> getTmpNotes() { | |
final List<Note> list = new ArrayList<>(); | |
list.add(new Note("1st note")); | |
list.add(new Note("2nd note")); | |
list.add(new Note("3rd note")); | |
list.add(new Note("4th note")); | |
list.add(new Note("5th note")); | |
list.add(new Note("6th note")); | |
list.add(new Note("7th note")); | |
list.add(new Note("8th note")); | |
list.add(new Note("9th note")); | |
list.add(new Note("10th note")); | |
list.add(new Note("11th note")); | |
list.add(new Note("12th note")); | |
list.add(new Note("13th note")); | |
list.add(new Note("15th note")); | |
return list; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexzh.recyclerviewsetemptyview; | |
public class Note { | |
private String mTitle; | |
public Note(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; | |
Note note = (Note) o; | |
return mTitle != null ? mTitle.equals(note.mTitle) : note.mTitle == null; | |
} | |
@Override | |
public int hashCode() { | |
return mTitle != null ? mTitle.hashCode() : 0; | |
} | |
@Override | |
public String toString() { | |
return "Note{" + | |
"title='" + mTitle + '\'' + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexzh.recyclerviewsetemptyview; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.util.List; | |
public class NotesAdapter extends EmptyRecyclerView.Adapter<NoteViewHolder> { | |
private List<Note> mNotes; | |
public void setNotes(List<Note> notes) { | |
this.mNotes = notes; | |
notifyDataSetChanged(); | |
} | |
@Override | |
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View itemView = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.item_note, parent, false); | |
return new NoteViewHolder(itemView); | |
} | |
@Override | |
public void onBindViewHolder(NoteViewHolder holder, int position) { | |
holder.mTitleTextView.setText(mNotes.get(position).getTitle()); | |
} | |
@Override | |
public int getItemCount() { | |
return mNotes == null ? 0 : mNotes.size(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexzh.recyclerviewsetemptyview; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.widget.TextView; | |
class NoteViewHolder extends RecyclerView.ViewHolder { | |
protected TextView mTitleTextView; | |
NoteViewHolder(View itemView) { | |
super(itemView); | |
mTitleTextView = (TextView) itemView.findViewById(R.id.title_text_view); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What happens when you have a SwipeRefreshView? The textview doesn't show up