滚动时添加 Headers 更多,无法BindViewHolder recyclelerview

我试图在我的适配器中添加标头但我不能bindViewHolder . 任何人都可以帮我解决这些问题吗?这是我的适配器代码:

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);
        }
    }
}

这是我的错误日志

11-10 14:02:12.115 2151-2151 / com.esantiago.pagination E / AndroidRuntime:FATAL EXCEPTION:main进程:com.esantiago.pagination,PID:2151 com.esantiago.pagination.adapter中的java.lang.NullPointerException .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)在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5465)android.support的android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5498) . v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4735)在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4611)的android.support.v7.widget.LinearLayoutManager $ LayoutState . next(LinearLayoutManager.java:1988)在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)在android.support.v7.widget.LilloutManager.fill(LinearLayoutManager.java:1347)android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:584)android.support.v7.widget.RecyclerView.dispatchLayoutStep1上的android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347) (RecyclerView.java:2977)位于android.sview的android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2901)android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277) . 在android.view.View.View的android.sview.v4上安装了android.view.View( . View.java:4631)的android.view视频(View.java:14817),以及android.view.View上的android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596) .layout(View.java:14817)位于Android.widget.LinearLayout.setoutVertical(LinearLayout . )的android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)上的android.view.ViewGroup.layout(ViewGroup.java:4631) . java:1525)在android.view.Layout.onLayout(LinearLayout.java:1434)的android.view.View.layout(View.java:14817)android.view.ViewGroup.layout(ViewGroup.j) ava:4631)android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)在android.view.View.layout(View.java:14817)的android.widget.FrameLayout.onLayout(FrameLayout.java:388) android.view.View.layout(ViewGroup.java:4631)在android.sview.v.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)在android.view.View.layout(View.java:14817)在android .view.ViewGroup.layout(ViewGroup.java:4631)位于android.view.View.View.View.View的Android.widget.FrameLayout.onLayout(FrameLayout.java:388)的android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) . 在Android.widget.LinearLayout.setoutVertical(LinearLayout.java)的android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)的android.view.ViewGroup.layout(ViewGroup.java:4631)上的布局(View.java:14817) :1525)在android.view.Layout.onLayout(LinearLayout.java:1434)的android.view.View.layout(View.java:14817)在android.view.ViewGroup.layout(ViewGroup.java:4631)在android .widget.FrameLayout.layoutChild ren(FrameLayout.java:453)位于android.view.View.Loutout(ViewLayout.java:388)的android.view.View.layout(View.java:14817)android.view.ViewGroup.layout(ViewGroup.java) :4631)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983) .view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5600)在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)的android.view.Choreographer.doCallbacks(Choreographer.java:574) . 在android.view.Handler的android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)的android.O. android.app.Looper.loop(Looper.java:136)中的.dispatchMessage(Handler.java:95)位于android.app.ActivityThread.main(ActivityThread.java:5001)的java.lang.reflect.Method.invokeNati在c的java.lang.reflect.Method.invoke(Method.java:515)的ve(Native Method)

回答(0)