Created
July 14, 2021 08:12
-
-
Save puke3615/b1a5a4e777a015336d70f02b35b8676a to your computer and use it in GitHub Desktop.
RecyclerViewExceptionHelper
This file contains hidden or 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
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* 带有RecyclerView的列表页统一封装 | |
* | |
* @author puke | |
* @version 2019/2/13 | |
*/ | |
public class RecyclerViewExceptionHelper<V extends View> { | |
private final RecyclerView recyclerView; | |
private RecyclerView.Adapter actualAdapter; | |
private ExceptionAdapter exceptionAdapter; | |
private ExceptionViewFactory<V> viewFactory; | |
private ExceptionViewRender<V> viewRender; | |
public interface ExceptionViewFactory<V extends View> { | |
V createExceptionView(ViewGroup parent); | |
} | |
public interface ExceptionViewRender<V extends View> { | |
void renderView(V exceptionView); | |
} | |
public static RecyclerViewExceptionHelper<View> newInstance(RecyclerView recyclerView) { | |
return new RecyclerViewExceptionHelper<>(recyclerView); | |
} | |
public RecyclerViewExceptionHelper(RecyclerView recyclerView) { | |
this.recyclerView = recyclerView; | |
this.actualAdapter = recyclerView.getAdapter(); | |
} | |
/** | |
* 绑定Adapter | |
*/ | |
public void setAdapter(RecyclerView.Adapter adapter) { | |
this.actualAdapter = adapter; | |
} | |
/** | |
* 设置异常View工厂类 | |
*/ | |
public RecyclerViewExceptionHelper<V> setViewFactory(ExceptionViewFactory<V> viewFactory) { | |
this.viewFactory = viewFactory; | |
return this; | |
} | |
/** | |
* 设置异常View绑定函数 | |
*/ | |
public RecyclerViewExceptionHelper<V> setViewRender(ExceptionViewRender<V> viewRender) { | |
this.viewRender = viewRender; | |
return this; | |
} | |
public void showException(ExceptionViewRender<V> viewRender) { | |
boolean forceRefresh = this.viewRender != viewRender && viewRender != null; | |
this.viewRender = viewRender; | |
showException(forceRefresh); | |
} | |
public void showException() { | |
showException(false); | |
} | |
public void showException(boolean forceRefresh) { | |
if (exceptionAdapter == null) { | |
exceptionAdapter = new ExceptionAdapter(); | |
} | |
if (isExceptionShow()) { | |
if (forceRefresh) { | |
refreshExceptionView(); | |
} | |
} else { | |
recyclerView.setAdapter(exceptionAdapter); | |
} | |
} | |
public void hideException() { | |
if (isExceptionShow()) { | |
recyclerView.setAdapter(actualAdapter); | |
} | |
} | |
protected void refreshExceptionView() { | |
exceptionAdapter.notifyItemChanged(0); | |
} | |
private boolean isExceptionShow() { | |
return recyclerView.getAdapter() == exceptionAdapter; | |
} | |
private class ExceptionAdapter extends RecyclerView.Adapter<ViewHolder> { | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
if (viewFactory == null) { | |
throw new NullPointerException("The exception view factory is null."); | |
} | |
V exceptionView = viewFactory.createExceptionView(parent); | |
if (exceptionView == null) { | |
throw new NullPointerException("The exception view is null."); | |
} | |
return new ViewHolder(exceptionView); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
if (viewRender != null) { | |
viewRender.renderView(holder.exceptionView); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return 1; | |
} | |
} | |
private class ViewHolder extends RecyclerView.ViewHolder { | |
final V exceptionView; | |
ViewHolder(V itemView) { | |
super(itemView); | |
this.exceptionView = itemView; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment