【说明】
使用RecyclerView的多种类型显示的方法,来判断空数据时显示空布局,就是Adapter里面的getItemViewType(int position) 方法。
- 实现方法很简单,逻辑也很简单... ...可能就是if多了几个
【思路】
通过在添加数据的时候来判断,子条目该显示什么布局(正常的、空布局)
判断:List集合为null, 或者没有数据,则显示正常的布局getItemCount(),空布局是一个item
判断:List集合没有数据,返回1,显空布局getItemViewType(int position)
判断:空布局类型返回1,正常布局类型返回0
【流程】
1.默认显示20个子条目数据
2.下拉显示空布局
3.点击空布局,显示20个子条目数据
【图示】没有动态图
【代码】布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--空布局-->
<TextView
android:id="@+id/tv_empty_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ffffff"
android:drawableTop="@mipmap/nodata"
android:gravity="center"
android:text="没有数据"
android:textColor="#ff00"
android:textSize="18sp"
android:textStyle="bold|italic"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f4f4f4"
android:orientation="vertical"
android:paddingBottom="1dp"
>
<!--子条目布局-->
<TextView
android:id="@+id/tv_itemText"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ffffff"
android:gravity="center"
android:text="123"
android:textColor="#ff00ff"
android:textSize="18sp"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--使用原生下拉刷新控件-->
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_fresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
【代码】逻辑代码
/**
* @author azhao
* @date 2017/11/21
* $desc recyclerView当没有数据的时候,展示空视图
*/
public class EmptyViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private LayoutInflater mInflater;
private List<String> mData;
private OnEmptyClickListener listener;
/**
* 空数据时,显示空布局类型
*/
private final int EMPTY_VIEW = 1;
/**
* 控制空布局的显隐
*/
private int mEmptyType = 0;
public EmptyViewAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mData = new ArrayList<>();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (EMPTY_VIEW != viewType) {
//返回空布局的viewHolder
return new ItemViewHolder(mInflater.inflate(R.layout.recycler_item, parent, false));
}
return new EmptyViewHolder(mInflater.inflate(R.layout.stub_empty, parent, false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int itemViewType = getItemViewType(position);
if (EMPTY_VIEW != itemViewType) {
//子条目布局
String s = mData.get(position);
((ItemViewHolder) holder).mTextView.setText(s);
} else {
//空视图布局 - 点击事件的回调
((EmptyViewHolder) holder).mEmptyTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onEmptyClick();
}
}
});
}
}
@Override
public int getItemCount() {
//注意,空布局---> mEmptyType = 1 显示1个布局
// ---> mEmptyType = 0 所有布局都不显示的
return mData != null ? mData.size() + mEmptyType : mEmptyType;
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public ItemViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_itemText);
}
}
public class EmptyViewHolder extends RecyclerView.ViewHolder {
private TextView mEmptyTextView;
public EmptyViewHolder(View itemView) {
super(itemView);
mEmptyTextView = (TextView) itemView.findViewById(R.id.tv_empty_text);
}
}
@Override
public int getItemViewType(int position) {
if (mEmptyType == EMPTY_VIEW) {
//空布局的类型
return EMPTY_VIEW;
}
return super.getItemViewType(position);
}
public interface OnEmptyClickListener {
/**
* 空视图的点击
*/
void onEmptyClick();
}
/**
* 设置空布局控件的点击监听,回调接口
*
* @param listener 回调接口
*/
public void setOnEmptyClickListener(OnEmptyClickListener listener) {
this.listener = listener;
}
/**
* 添加显示的数据
* 如果数据为空,则显示空布局
*
* @param data List<String>
*/
public void addData(List<String> data) {
if (!mData.isEmpty()) {
//子条目布局 --> 恢复默认的布局
int size = mData.size();
mData.clear();
notifyItemRangeRemoved(0, size);
/*notifyDataSetChanged();*/
}
if (data != null && !data.isEmpty()) {
if (mEmptyType == 1) {
//空布局 --> 恢复默认的布局
mEmptyType = 0;
notifyItemRemoved(0);
}
//刷新,新添加的数据
mData.addAll(data);
notifyItemRangeInserted(0, data.size());
} else {
//如果刷新的数据为空list,则显示空布局
if (mEmptyType != 1) {
//当前布局不是空布局,则刷新显示空布局
mEmptyType = 1;
notifyItemInserted(0);
}
}
}
/**
* 设置为空布局
* 如果当前布局已经是空布局,则不需要在进行刷新显示
*/
public void setEmpty() {
if (!mData.isEmpty()) {
//如果在设置空布局之前,已经显示了子条目类型的数据,那么需要清空还原
int size = mData.size();
mData.clear();
notifyItemRangeRemoved(0, size);
/*notifyDataSetChanged();*/
}
if (mEmptyType != EMPTY_VIEW) {
//当前布局不是空布局,则刷新显示空布局
mEmptyType = EMPTY_VIEW;
notifyItemInserted(0);
}
}
}
/**
* @author azhao
* @date 2017/11/21
* $desc
*/
public class EmpyViewActivity extends AppCompatActivity {
private SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView mRecyclerView;
private EmptyViewAdapter mEmptyViewAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_stub);
initView();
initData();
initListener();
}
private void initData() {
//模拟数据延时显示
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
//结束下拉刷新
mSwipeRefreshLayout.setRefreshing(false);
List<String> data = new ArrayList<>();
for (int i = 0; i < 20; i++) {
data.add("Demo"+i);
}
mEmptyViewAdapter.addData(data);
}
}, 1500);
}
private void initView() {
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_fresh);
mSwipeRefreshLayout.setRefreshing(true);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_recyclerView);
//如果子布局大小是固定的,起到优化的效果
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mEmptyViewAdapter = new EmptyViewAdapter(this);
mRecyclerView.setAdapter(mEmptyViewAdapter);
}
private void initListener() {
//空视图的点击监听
mEmptyViewAdapter.setOnEmptyClickListener(new EmptyViewAdapter.OnEmptyClickListener() {
@Override
public void onEmptyClick() {
//显示下拉刷新
mSwipeRefreshLayout.setRefreshing(true);
//模拟数据延时显示
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
//结束下拉刷新
mSwipeRefreshLayout.setRefreshing(false);
List<String> data = new ArrayList<>();
for (int i = 0; i < 20; i++) {
data.add("Demo" + i);
}
mEmptyViewAdapter.addData(data);
}
}, 2500);
}
});
//下拉刷新的监听
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
mEmptyViewAdapter.setEmpty();
}
}, 1500);
}
});
}
}