/** * 设置FooterView * * @param recyclerView * @param view */ public static void setFooterView(RecyclerView recyclerView, View view) { RecyclerView.Adapter outerAdapter = recyclerView.getAdapter(); if (outerAdapter == null || !(outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter)) { return; } HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter = (HeaderAndFooterRecyclerViewAdapter) outerAdapter; if (headerAndFooterAdapter.getFooterViewsCount() == 0) { headerAndFooterAdapter.addFooterView(view); } }
/** * 移除HeaderView * * @param recyclerView */ public static void removeHeaderView(RecyclerView recyclerView) { RecyclerView.Adapter outerAdapter = recyclerView.getAdapter(); if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) { int headerViewCounter = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getHeaderViewsCount(); if (headerViewCounter > 0) { View headerView = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getHeaderView(); ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).removeFooterView(headerView); } } }
/** * 设置headerAndFooterAdapter的FooterView State * * @param recyclerView recyclerView * @param pageSize 分页展示时,recyclerView每一页的数量 * @param state FooterView State * @param errorListener FooterView处于Error状态时的点击事件 */ public static void setFooterViewState( Context context, RecyclerView recyclerView, int pageSize, LoadingFooter.State state, View.OnClickListener errorListener) { if (context == null) { return; } RecyclerView.Adapter outerAdapter = recyclerView.getAdapter(); if (outerAdapter == null || !(outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter)) { return; } HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter = (HeaderAndFooterRecyclerViewAdapter) outerAdapter; // 只有一页的时候,就别加什么FooterView了 if (headerAndFooterAdapter.getInnerAdapter().getItemCount() < pageSize) { return; } LoadingFooter footerView; // 已经有footerView了 if (headerAndFooterAdapter.getFooterViewsCount() > 0) { footerView = (LoadingFooter) headerAndFooterAdapter.getFooterView(); footerView.setState(state); if (state == LoadingFooter.State.NetWorkError) { footerView.setOnClickListener(errorListener); } recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1); } else { footerView = new LoadingFooter(context); footerView.setState(state); if (state == LoadingFooter.State.NetWorkError) { footerView.setOnClickListener(errorListener); } headerAndFooterAdapter.addFooterView(footerView); recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1); } }