Baserecyclerviewadapterhelper: 自定义ViewHolder的bug

Created on 11 Jun 2018  ·  5Comments  ·  Source: CymChad/BaseRecyclerViewAdapterHelper

Adapter

/**
 * 时间:2018/6/8 0008 17:21
 * 描述:商家详情页面-->商品列表页面商品的适配器
 * 修改人:
 * 修改时间:
 * 修改备注:
 *
 * @author WangYoule
 * @qq 270628297
 */
public class GoodAdapter extends BaseMultiItemQuickAdapter<GoodEntity, GoodViewHolder> {
    public static final int TYPE_HEADER = 1;
    public static final int TYPE_DATA = 2;
    private Context context;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public GoodAdapter(Context context, List<GoodEntity> data) {
        super(data);
        this.context = context;
        addItemType(TYPE_HEADER, R.layout.item_seller_header);
        addItemType(TYPE_DATA, R.layout.item_seller_goods);
    }

    @Override
    protected void convert(GoodViewHolder helper, GoodEntity item) {
        int type = helper.getItemViewType();
        if (TYPE_HEADER == type) {
            helper.setText(R.id.tv_header, item.header);
        } else if (TYPE_DATA == type) {
            helper.setText(R.id.tv_name, item.name);
            Glide.with(context).load(item.icon).into((ImageView) helper.getView(R.id.iv_icon));
        }
    }

}

ViewHodler:

/**
 * 时间:2018/6/11 0011 16:27
 * 描述:商家详情页面-->商品列表页面商品适配器的ViewHolder
 * 修改人:
 * 修改时间:
 * 修改备注:
 *
 * @author WangYoule
 * @qq 270628297
 */
public class GoodViewHolder extends BaseViewHolder implements View.OnClickListener {
    ImageButton ib_add;
    ImageButton ib_minus;
    TextView tv_count;


    public GoodViewHolder(View view) {
        super(view);
        ib_add = getView(R.id.ib_add);
        ib_minus = getView(R.id.ib_minus);
        tv_count = getView(R.id.tv_money);
        tv_count.setOnClickListener(this);
        ib_add.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ib_add) {
            //点击"+"号
            // 动画处理
            AnimationSet showMinusAnimation = getShowMinusAnimation();
            ib_minus.startAnimation(showMinusAnimation);
            ib_minus.startAnimation(showMinusAnimation);

            ib_minus.setVisibility(View.VISIBLE);
            ib_minus.setVisibility(View.VISIBLE);

        } else if (id == R.id.ib_minus) {
            //点击"-"号
            // 动画处理
            AnimationSet animation = getHideMinusAnimation();
            ib_minus.startAnimation(animation);
            ib_minus.startAnimation(animation);

            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    ib_minus.setVisibility(View.GONE);
                    ib_minus.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
    }

    /**
     * 显示动画
     *
     * @return
     */
    private AnimationSet getShowMinusAnimation() {
        AnimationSet set = new AnimationSet(false);
        TranslateAnimation translate = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 5,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0);
        RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AlphaAnimation alpha = new AlphaAnimation(0, 1);
        set.addAnimation(rotate);
        set.addAnimation(translate);
        set.addAnimation(alpha);
        set.setDuration(200);
        return set;
    }

    /**
     * 隐藏动画
     *
     * @return
     */
    private AnimationSet getHideMinusAnimation() {
        AnimationSet set = new AnimationSet(false);
        TranslateAnimation translate = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 5,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0);
        RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AlphaAnimation alpha = new AlphaAnimation(1, 0);
        set.addAnimation(rotate);
        set.addAnimation(translate);
        set.addAnimation(alpha);
        set.setDuration(200);
        return set;
    }

}

Exception:
java.lang.ClassCastException: com.chad.library.adapter.base.BaseViewHolder cannot be cast to com.wyl.mytakeout.ui.holder.GoodViewHolder

Most helpful comment

@YouLe2016 我的找到原因了 是因为item中有一个view类型转换异常了 但是报错报的是自定义viewholder异常 所以第一时间没查到原因

All 5 comments

你应该是哪个地方导包错误了,自定义viewholder已经在线上稳定运行很久了,不应该有这个bug。可以下载demo 体验一下,demo里面有内置自定义viewholder部分的代码

没有什么问题呀,不是导包的问题,那样直接编译不过了。
会不会是集成BaseMultiItemQuickAdapter引发的错误啊

/**
 * 时间:2018/6/8 0008 17:21
 * 描述:商家详情页面-->商品列表页面商品的适配器
 * 修改人:
 * 修改时间:
 * 修改备注:
 *
 * @author WangYoule
 * @qq 270628297
 */
public class GoodAdapter extends BaseQuickAdapter<GoodEntity, GoodViewHolder> {
    public static final int TYPE_HEADER = 1;
    public static final int TYPE_DATA = 2;
    private Context context;

    public GoodAdapter(Context context, List<GoodEntity> data) {
        super(data);
        this.context = context;
        setMultiTypeDelegate(new MultiTypeDelegate<GoodEntity>() {
            @Override
            protected int getItemType(GoodEntity goodEntity) {
                return goodEntity.type;
            }
        });
        getMultiTypeDelegate()
                .registerItemType(TYPE_HEADER, R.layout.item_seller_header)
                .registerItemType(TYPE_DATA, R.layout.item_seller_goods);
    }

    @Override
    protected void convert(GoodViewHolder helper, GoodEntity item) {
        int type = helper.getItemViewType();
        if (TYPE_HEADER == type) {
            helper.setText(R.id.tv_header, item.header);
        } else if (TYPE_DATA == type) {
            helper.setText(R.id.tv_name, item.name);
            Glide.with(context).load(item.icon).into((ImageView) helper.getView(R.id.iv_icon));
        }
    }
}

经过修改为BaseQuickAdapter 还是不行=.=,求助大神们

我也遇见同样问题
使用版本:com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30

升级到2.9.40同样报错
image

@YouLe2016 我的找到原因了 是因为item中有一个view类型转换异常了 但是报错报的是自定义viewholder异常 所以第一时间没查到原因

Was this page helpful?
0 / 5 - 0 ratings