https://developer.android.com/training/material/lists-cards.html
While using it, I faced the issue of missing a useful functionality that is implemented in ListView.
that feature is setting an empty view in case there was no records.
In ListView it was as simple as this
View emptyView = findViewById(R.id.mylist_empty_view);
ListView myList = ....
myList.setEmptyView(emptyView);
but this method doesn't exist for recycler view so we need a work around until android team fixes this.
and here are the screen shots of both list view and recycler view fix
List view :
Recycler view :
here is how I fixed it:
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.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; | |
} | |
} |
here is the content of empty_view, it can be anything.
Enjoy.
Good approach. But why does it still inflate the empty view when the data is loading, and then gone when the data is loaded? Hope you can provide the fix. Thanks for posting!
ReplyDeletecode error, public RecyclerView.ViewHolder onCreateViewHolder ->return ViewHolder, but EmptyViewHolder evh = new EmptyViewHolder(v);->return EmptyViewHolder
ReplyDeleteThank you! Initially I set EMPTY_VIEW == 0, which caused some weird issues. Setting it to 10 made it work as expected.
ReplyDelete