アダプターにヘッダーを追加しようとしましたが、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: 致命的な例外: メイン プロセス: com.esantiago.pagination、PID: com.esantiago.pagination.adapter で 2151 java.lang.NullPointerException。 AdapterItem$override.onBindViewHolder(AdapterItem.java:105) 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.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 android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2977) の .onLayoutChildren(LinearLayoutManager.java:584) android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2901) android.view.View.layout(View.java:14817) で android.view.ViewGroup.layout(ViewGroup.java:4631) で android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277) ) 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. 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. 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. 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) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600) at 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) で java.lang.reflect.Method.invokeNative(Native Method) で java.lang.reflect.Method.invoke(Method.java:515) で cReflect.Method.invoke(Method.java:515) at cReflect.Method.invoke(Method.java:515) at c