can't BindViewHolder recyclerview when add header on scroll loard more
I have tried to add header in my adapter but I can't bindViewHolder. Can anyone help me fixing these problems? Here is my adapter code:
public class AdapterItem extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_HEADER=0;
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 2;
private ArrayList<Item> itemList;
private OnLoadMoreListener onLoadMoreListener;
private LinearLayoutManager mLinearLayoutManager;
private boolean isMoreLoading = false;
private int visibleThreshold = 1;
int firstVisibleItem, visibleItemCount, totalItemCount;
public interface OnLoadMoreListener{
void onLoadMore();
}
public AdapterItem(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener=onLoadMoreListener;
itemList =new ArrayList<>();
}
public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager){
this.mLinearLayoutManager=linearLayoutManager;
}
public void setRecyclerView(RecyclerView mView){
mView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (!isMoreLoading && (totalItemCount - visibleItemCount)<= (firstVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
isMoreLoading = true;
}
}
});
}
@Override
public int getItemViewType(int position) {
if(itemList.get(position) != null && isPositionHeader(position)){
return VIEW_HEADER;
}else if(itemList.get(position) != null) {
return VIEW_ITEM;
}else{
return VIEW_PROG;
}
}
private boolean isPositionHeader (int position) {
return position == 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
if (viewType == VIEW_HEADER) {
return new StudentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false));
}else
if (viewType == VIEW_ITEM) {
return new StudentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false));
}else {
return new ProgressViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false));
}
}
public void addAll(List<Item> lst){
itemList.clear();
itemList.addAll(lst);
notifyDataSetChanged();
}
public void addItemMore(List<Item> lst){
itemList.addAll(lst);
notifyItemRangeChanged(0,itemList.size());
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof StudentViewHolder) {
Item singleItem = (Item) itemList.get(position);
((StudentViewHolder) holder).tvItem.setText(singleItem.getItem());
}
}
public void setMoreLoading(boolean isMoreLoading) {
this.isMoreLoading=isMoreLoading;
}
@Override
public int getItemCount() {
return itemList.size();
}
public void setProgressMore(final boolean isProgress) {
if (isProgress) {
new Handler().post(new Runnable() {
@Override
public void run() {
itemList.add(null);
notifyItemInserted(itemList.size() - 1);
}
});
} else {
itemList.remove(itemList.size() - 1);
notifyItemRemoved(itemList.size());
}
}
static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView tvItem;
public StudentViewHolder(View v) {
super(v);
tvItem = (TextView) v.findViewById(R.id.tvItem);
}
}
static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar pBar;
public ProgressViewHolder(View v) {
super(v);
pBar = (ProgressBar) v.findViewById(R.id.pBar);
}
}
}
Here is my error log
11-10 14:02:12.115 2151-2151/com.esantiago.pagination E/AndroidRuntime: FATAL EXCEPTION: main Process: com.esantiago.pagination, PID: 2151 java.lang.NullPointerException at com.esantiago.pagination.adapter.AdapterItem$override.onBindViewHolder(AdapterItem.java:105) at com.esantiago.pagination.adapter.AdapterItem$override.access$dispatch(AdapterItem.java) at com.esantiago.pagination.adapter.AdapterItem.onBindViewHolder(AdapterItem.java:0) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5465) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5498) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4735) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4611) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384) a t android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:584) at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2977) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2901) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.jav a:4631) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.widget.FrameLayout.layoutChildre n(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:14817) at android.view.ViewGroup.layout(ViewGroup.java:4631) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at c
链接地址: http://www.djcxy.com/p/93162.html