如何用两个列表对recyclerview进行分页

时间:2019-02-06 10:59:34

标签: java android android-recyclerview

我正在尝试通过2个API端点设置包含多个列表(例如Facebook广告和用户供稿)的Recyclerview,但是在尝试对列表进行分页(加载更多)时出现一些错误。

这是我的代码。从不同的API端点加载两个列表后,我尝试在活动中调用addAll方法,但出现错误(我在loadMore listener [paginate stuff]中也调用了此方法)。

private static final int TYPE_VIDEO = 0;
private static final int TYPE_PROMOTED_VIDEO = 1;
private static final int TYPE_LOADING = 3;

private Context context;
List<Video> videosList;
List<Promoted> promotedVideosList;

private boolean isLoadingAdded = true;  

public MyAdapter(Context context){
    this.context = context;
    //Initalization stuffs
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    switch (viewType) {
        case TYPE_VIDEO:
            View viewVideo = inflater.inflate(R.layout.item_video, parent, false);
            viewHolder = new VideoVH(viewVideo);
            break;

        case TYPE_PROMOTED_VIDEO:
            View viewPromoted = inflater.inflate(R.layout.item_promoted_video, parent, false);
            viewHolder = new PromotedVH(viewPromoted);
            break;

        case TYPE_LOADING:
            View viewLoading = inflater.inflate(R.layout.item_loading, parent, false);
            viewHolder = new LoadingVH(viewLoading);
            break;

    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    switch (getItemViewType(position)) {

        case TYPE_VIDEO:
            //DO something when it a normal video
            break;

        case TYPE_PROMOTED_VIDEO:
            //DO something when it's a promoted video

            break;

        case TYPE_LOADING:
            //Loading view
            break;
    }

}


public void add(Video video, Promoted promoted) {
    Toast.makeText(context, "Not Null", Toast.LENGTH_SHORT).show();
    videosList.add(video);
    promotedVideosList.add(promoted);

    notifyItemInserted(videosList.size() + promotedVideosList.size() - 1);
}

public void addAll(List<Video> videoList, List<Promoted> promotedList) {
    add(addVideo(videoList), addPromoted(promotedList));
}

private Video addVideo(List<Video> videoList){
    for (Video result : videoList) return result;
    return null;
}

private Promoted addPromoted(List<Promoted> promotedList{
    for (Promoted result : promotedList) return result;
    return null;
}


@Override
public int getItemCount() {
    return videosList == null && promotedVideosList == null ? 0 : this.videosList.size() + this.promotedVideosList.size();
}

@Override
public int getItemViewType(int position) {
    int videoSize = this.videosList.size();
    int promotedSize = this.promotedVideosList.size();

    if(position < videoSize){
        return TYPE_VIDEO;
    }

    if(position - videoSize < promotedSize){
        return TYPE_PROMOTED_VIDEO;
    }

    if (position == promotedSize + videoSize - 1 && isLoadingAdded){
        return TYPE_LOADING;
    }

    return -1;
}

public static class VideoVH extends RecyclerView.ViewHolder{

    VideoVH(View itemView) {
        super(itemView);

        //findviewbyid stuff

    }
}

public static class PromotedVH extends RecyclerView.ViewHolder{

    public PromotedVH(View itemView) {
        super(itemView);
      //findviewbyid stuff
    }
}

public static class LoadingVH extends RecyclerView.ViewHolder{

    @BindView(R.id.loading_progress)
    ProgressBar loading;

    public LoadingVH(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

我希望它能正常工作,但不知何故,我知道我做的不对。

这是我的logcat的摘要。

 java.lang.NullPointerException: Attempt to invoke interface method'boolean java.util.List.add(java.lang.Object)' on a null object reference
 at com.myproject.adapters.MyAdapter.add(MyAdapter.java:508)
 at com.myproject.adapters.MyAdapter.addAll(MyAdapter.java:515)

2 个答案:

答案 0 :(得分:1)

在将数据添加到列表之前是否已初始化列表。在构造函数中,您必须初始化所有列表。 例如:videosList = new List(); 同样

答案 1 :(得分:1)

您没有初始化videosList和promotedVideosList。做

public MyAdapter(Context context){
  //Initalization stuffs
   this.context = context;
   videosList=new ArrayList();
   promotedVideosList=new ArrayList();

}