为什么myViewHolder无法投放?

时间:2017-08-30 07:58:33

标签: android android-recyclerview

我的RecyclerViewAdapter中有3个ViewHolder我用它们来改变视图,具体取决于我动态收到的位置。

    @Override
    public int getItemViewType(int position) {

        int viewType = 0;

        if (position == 0) {
            viewType = 0;
        }else if (position == 1){
            viewType = 1;
        }else if (position == 2){
            viewType = 2;
        }

        return viewType;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_notification, parent, false);
        RecyclerView.ViewHolder viewHolder;
        switch (viewType){
            case 0:
                return new MyViewHolder(v);
            case 1:
                return new MyViewHolderAccepted(v);
            case 2:
                return new MyViewHolderRefused(v);
            default:
                return null;
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final DataNotifications dataNotifications = list.get(position);

    int type = Integer.valueOf(dataNotifications.getStatue());
    switch (getItemViewType(type)){
            case 0:
                MyViewHolder myViewHolder = (MyViewHolder) holder;
                ........
                break;
            case 1:
                MyViewHolderAccepted myViewHolderAccepted = (MyViewHolderAccepted) holder;
                .......
                break;
            case 2:
                MyViewHolderRefused myViewHolderRefused = (MyViewHolderRefused) holder;
                ........
                break;
        }


//        Glide.with(context).load(dataNotifications.getThumbnail()).into(holder.thumnail);




    }

当我运行上面的代码时,我得到了这个例外:

  

NotificationAdapter $ MyViewHolder无法转换为    NotificationAdapter $ MyViewHolderAccepted

我应该为每个ViewHolder设置不同的布局吗? - 但我只更改了一个TextView的颜色,因此每个ViewHolder的布局都很昂贵。

我的viewHolders

public class MyViewHolder extends RecyclerView.ViewHolder {
        ....


        public MyViewHolder(View itemView) {
            super(itemView);
            ....
        }
    }
     public class MyViewHolderAccepted extends MyViewHolder{
        ...

        public MyViewHolderAccepted(View itemView) {
            super(itemView);
            ...
    }

    public class MyViewHolderRefused extends MyViewHolder{


        public MyViewHolderRefused(View itemView) {
            super(itemView);
            ....
        }
    }

1 个答案:

答案 0 :(得分:0)

  

NotificationAdapter $ MyViewHolder无法转换为NotificationAdapter $ MyViewHolderAccepted

最有可能的原因是它MyViewHolderAccepted延伸MyViewHolder而不是相反。或者它根本不延伸它。

class MyViewHolderAccepted extends MyViewHolder {
   ....

修改

注意extends不是双向的。如果课程B延伸A,那么您可以将B投放到A但不A投放到B,这就是您要追求的目标做。

编辑2

  

public void onBindViewHolder(最终的RecyclerView.ViewHolder holder

此。您需要将方法签名中的holder类型更改为MyViewHolder。那应该没问题。

public void onBindViewHolder(final MyViewHolder holder