滚动时添加标题时不能绑定更多的BindViewHolder recyclerview

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

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:致命异常:主进程: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)在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4611)在android.support.v7.widget.LinearLayoutManager $ LayoutState.next (LinearLayoutManager.java:1988)at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)a 吨android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:584)在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)在android.view.ViewGroup.layout(ViewGroup.java:4631)android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596)android.view.View .layout(View.java:14817)在android.view.ViewGroup.layout(ViewGroup.java:4631)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)在android.widget.LinearLayout.layoutVertical(LinearLayout中。在android.view.View.Group.layout(ViewGroup.jav)的android.view.View.layout(View.java:14817)android.widget.LinearLayout.onLayout(LinearLayout.java:1434) a:4631)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)在android.widget.FrameLayout.onLayout(FrameLayout.java:388)在android.view.View.layout(View.java:14817)at android.view.View.layout(View.java:14817)android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)android.view.ViewGroup.layout(ViewGroup.java:4631) .view.ViewGroup.layout(ViewGroup.java:4631)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)在android.widget.FrameLayout.onLayout(FrameLayout.java:388)在android.view.View。布局(View.java:14817)在android.view.ViewGroup.layout(ViewGroup.java:4631)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)在android.widget.LinearLayout.layoutVertical(LinearLayout.java :1525)在android.view.View.layout(ViewGroup.java:4631)android.view.View.layout(View.java:14817)android.widget.LinearLayout.onLayout(LinearLayout.java:1434)在android .widget.FrameLayout.layoutChildre 在android.view.View.Group.layout(ViewGroup.java)上的android.view.View.layout(View.java:14817)android.widget.FrameLayout.onLayout(FrameLayout.java:388)n(FrameLayout.java:453) :4631)处的Android android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996) .view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5600)在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)在android.view.Choreographer.doCallbacks(Choreographer.java:574)的机器人。 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 (本地方法)在java.lang.reflect.Method.invoke(Method.java:515)at c

链接地址: http://www.djcxy.com/p/93161.html

上一篇: can't BindViewHolder recyclerview when add header on scroll loard more

下一篇: Added View has RecyclerView as parent but view is not a real child