我曾尝试在我的适配器中添加标头,但我无法绑定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:FATAL EXCEPTION:main 过程:com.esantiago.pagination,PID:2151 显示java.lang.NullPointerException 在com.esantiago.pagination.adapter.AdapterItem $ override.onBindViewHolder(AdapterItem.java:105) at com.esantiago.pagination.adapter.AdapterItem $ override.access $ dispatch(AdapterItem.java) 在com.esantiago.pagination.adapter.AdapterItem.onBindViewHolder(AdapterItem.java:0) 在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5465) 在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5498) 在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) 在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384) 在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) 在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2901) 在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277) 在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.java:1525) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 在android.view.View.layout(View.java:14817) 在android.view.ViewGroup.layout(ViewGroup.java:4631) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 在android.view.View.layout(View.java:14817) 在android.view.ViewGroup.layout(ViewGroup.java:4631) 在android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435) 在android.view.View.layout(View.java:14817) 在android.view.ViewGroup.layout(ViewGroup.java:4631) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 在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.java:1525) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 在android.view.View.layout(View.java:14817) 在android.view.ViewGroup.layout(ViewGroup.java:4631) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 在android.view.View.layout(View.java:14817) 在android.view.ViewGroup.layout(ViewGroup.java:4631) 在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5600) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761) 在android.view.Choreographer.doCallbacks(Choreographer.java:574) 在android.view.Choreographer.doFrame(Choreographer.java:544) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在c