多视图回收者视图未加载屏幕视图

时间:2016-01-15 06:30:31

标签: java android android-recyclerview android-viewholder

我正在创建Recyclerview并拥有3个不同的ViewHolders。 我的适配器类看起来像这样:

public class ProfileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<Object> items = new ArrayList<>();
private Context context;

public ProfileAdapter(Context context, List<Object> items) {
    this.context = context;
    this.items = items;
}



@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = null;
    RecyclerView.ViewHolder viewHolder = null;
    int listViewItemType = getItemViewType(viewType);
    switch (listViewItemType) {
        case 0:
            view = LayoutInflater.from(context).inflate(R.layout.fragment_profile_user, parent, false);
            return new ProfileMainItemViewHolder(context,view);
        case 1:
            view = LayoutInflater.from(context).inflate(R.layout.view_search_playlist_item, parent, false);
            return new PlaylistItemViewHolder(context,view);
        case 2:
            view = LayoutInflater.from(context).inflate(R.layout.card_item_home_new, parent, false);
            return new EventItemViewHolder(context, view);
        default:
            viewHolder = null;
    }
    return viewHolder;
}



@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case 0:
            ((ProfileMainItemViewHolder) holder).render((User) items.get(position));
            break;
        case 1:
            ((PlaylistItemViewHolder) holder).render(context, (Playlist) items.get(position), "");
            break;
        case 2:
            ((EventItemViewHolder) holder).render((Event) items.get(position));
            break;
    }
}

public void updateItems(List<Object> items){
    this.items = sortItemList(items);
    notifyDataSetChanged();
}

@Override
public int getItemViewType(int position) {
    if(items.get(position) instanceof User){
        return 0;
    } else if (items.get(position) instanceof Playlist){
        return 1;
    } else if (items.get(position) instanceof Event){
        return 2;
    }
    return -1;
}

private List<Object> sortItemList(List<Object> list){
    ArrayList<Object> sortedList = new ArrayList<>();
    //Add User object first
    for (Object o : list) {
        if(o instanceof User){
            sortedList.add(o);
        }
    }
    //Then Playlist objects
    for (Object o : list) {
        if(o instanceof Playlist){
            sortedList.add(o);
        }
    }
    //Then Events
    for (Object o : list) {
        if(o instanceof Event){
            sortedList.add(o);
        }
    }
    return sortedList;
}

@Override
public int getItemCount() {
    return this.items.size();
}

}

它可以完美地加载所有可见项目,但是当我向下滚动并且加载了一个在顶部不可见的实例对象时,应用程序会出错:

  

AndroidRuntime:java.lang.ClassCastException:com.surfsup.viewholder.PlaylistItemViewHolder无法强制转换为com.surfsup.viewholder.EventItemViewHolder   AndroidRuntime:at com.surfsup.adapters.ProfileAdapter.onBindViewHolder(ProfileAdapter.java:76)   AndroidRuntime:在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5138)   AndroidRuntime:在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4433)   AndroidRuntime:在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4326)

我在监督什么吗? 感谢

1 个答案:

答案 0 :(得分:1)

您无需致电getItemViewType(viewType)即可获取视图类型。 viewType参数已经是RecyclerView.Adapter从覆盖的getItemViewType()方法评估并通过onCreateViewHolder()方法传递的视图类型值。

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = null;
    RecyclerView.ViewHolder viewHolder = null;
    switch (viewType) {
        case 0:
            view = LayoutInflater.from(context).inflate(R.layout.fragment_profile_user, parent, false);
            return new ProfileMainItemViewHolder(context,view);
        case 1:
            view = LayoutInflater.from(context).inflate(R.layout.view_search_playlist_item, parent, false);
            return new PlaylistItemViewHolder(context,view);
        case 2:
            view = LayoutInflater.from(context).inflate(R.layout.card_item_home_new, parent, false);
            return new EventItemViewHolder(context, view);
        default:
            viewHolder = null;
    }
    return viewHolder;
}