java–CardView在第二次点击时没有扩展

java–CardView在第二次点击时没有扩展,第1张

概述在为这些CardView实现自定义动画后,前两个不按照预期的方式运行.项目A和项目B在第二次点击时不会展开,但是项目C完全正常.publicclassMyRecyclerAdapterextendsRecyclerView.Adapter<RecyclerView.ViewHolder>{privatestaticfinalintTYPE_HEADER=0;privat

在为这些CardVIEw实现自定义动画后,前两个不按照预期的方式运行.项目A和项目B在第二次点击时不会展开,但是项目C完全正常.

public class MyRecyclerAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder> {    private static final int TYPE_header = 0;    private static final int TYPE_ITEM = 1;    private Context mContext;    RecyclerVIEwheader header;    List<MyRecyclerVIEwItem> ListItems;    ValueAnimator mAnimator;    public MyRecyclerAdapter(Context context, RecyclerVIEwheader header, List<MyRecyclerVIEwItem> ListItems)    {        this.mContext = context;        this.header = header;        this.ListItems = ListItems;        }    @OverrIDe    public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) {        if(vIEwType == TYPE_header)        {            VIEw v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclervIEw_header, parent, false);            return new MyRecyclerAdapter.VHheader(v);        }        else if(vIEwType == TYPE_ITEM)        {            VIEw v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclervIEw_item, parent, false);            return new MyRecyclerAdapter.VHItem(v);        }        throw new RuntimeException("there is no type that matches the type " + vIEwType + " + make sure your using types correctly");    }    private MyRecyclerVIEwItem getItem(int position)    {        return ListItems.get(position);    }    @OverrIDe    public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder, int position) {        final Typeface iconFont = FontManager.getTypeface(mContext, FontManager.FontAWESOME);        if (holder instanceof VHheader)        {            final VHheader vhheader = (VHheader)holder;                        }        else if (holder instanceof VHItem)        {            MyRecyclerVIEwItem currentItem = getItem(position-1);            final VHItem vhItem = (VHItem)holder;            vhItem.txtA.setText(currentItem.getContinent());            vhItem.txtB.setText(currentItem.getCountry());            vhItem.txtB.setVisibility(VIEw.GONE);            vhItem.txtExpandCollapse.setText(R.string.fa_icon_chevron_down);            vhItem.txtExpandCollapse.setTypeface(iconFont);            //Add onPreDrawListener            vhItem.txtB.getVIEwTreeObserver().addOnPreDrawListener(            new VIEwTreeObserver.OnPreDrawListener() {                @OverrIDe                public boolean onPreDraw() {                    vhItem.txtB.getVIEwTreeObserver().removeOnPreDrawListener(this);                    vhItem.txtB.setVisibility(VIEw.GONE);                    final int wIDthSpec = VIEw.MeasureSpec.makeMeasureSpec(0, VIEw.MeasureSpec.UnspecIFIED);                    final int heightSpec = VIEw.MeasureSpec.makeMeasureSpec(0, VIEw.MeasureSpec.UnspecIFIED);                    vhItem.txtB.measure(wIDthSpec, heightSpec);                    mAnimator = vhItem.slIDeAnimator(0, vhItem.txtB.getMeasuredHeight());                    return true;                }            });            vhItem.cardVIEw.setonClickListener(new VIEw.OnClickListener() {                @OverrIDe                public voID onClick(VIEw v) {                    if(vhItem.txtB.getVisibility() == VIEw.GONE){                        vhItem.expand();                    } else {                        vhItem.collapse();                    }                }            });        }    }    @OverrIDe    public int getItemVIEwType(int position) {        if(ispositionheader(position))            return TYPE_header;            return TYPE_ITEM;    }    private boolean ispositionheader(int position)    {        return position == 0;    }    // increasing getItemcount to 1. This will be the row of header.    @OverrIDe    public int getItemCount() {        return ListItems.size()+1;    }    class VHheader extends RecyclerVIEw.VIEwHolder{        button btnCollapseAll, btnExpandAll;        public VHheader(VIEw headerVIEw) {            super(headerVIEw);            this.btnCollapseAll = headerVIEw.findVIEwByID(R.ID.btn_collapseall);            this.btnExpandAll = headerVIEw.findVIEwByID(R.ID.btn_expandall);        }    }    public class VHItem extends RecyclerVIEw.VIEwHolder{        CardVIEw cardVIEw;        relativeLayout mrelativeLayout;        TextVIEw txtExpandCollapse, txtA, txtB;        public VHItem(VIEw itemVIEw) {            super(itemVIEw);            this.cardVIEw = itemVIEw.findVIEwByID(R.ID.cv);            this.mrelativeLayout = itemVIEw.findVIEwByID(R.ID.tv_rv_relativelayout);            this.txtExpandCollapse = itemVIEw.findVIEwByID(R.ID.tv_rv_expandcollapse);            this.txtA = itemVIEw.findVIEwByID(R.ID.tv_rv_A);            this.txtB = itemVIEw.findVIEwByID(R.ID.tv_rv_B);        }        private voID expand() {            // set Visible            txtB.setVisibility(VIEw.VISIBLE);            // change direction of chevron to 'up'            txtExpandCollapse.setText(R.string.fa_icon_chevron_up);            mAnimator.start();        }        private voID collapse() {            // change direction of chevron to 'down'            txtExpandCollapse.setText(R.string.fa_icon_chevron_down);            int finalHeight = txtB.getHeight();            ValueAnimator mAnimator = slIDeAnimator(finalHeight, 0);            mAnimator.addListener(new Animator.AnimatorListener() {                @OverrIDe                public voID onAnimationEnd(Animator animator) {                    //Height=0, but it set visibility to GONE                    txtB.setVisibility(VIEw.GONE);                }                @OverrIDe                public voID onAnimationStart(Animator animator) {                }                @OverrIDe                public voID onAnimationCancel(Animator animator) {                }                @OverrIDe                public voID onAnimationRepeat(Animator animator) {                }            });            mAnimator.start();        }        public ValueAnimator slIDeAnimator(int start, int end) {            ValueAnimator animator = ValueAnimator.ofInt(start, end);            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                @OverrIDe                public voID onAnimationUpdate(ValueAnimator valueAnimator) {                    //Update Height                    int value = (Integer) valueAnimator.getAnimatedValue();                    VIEwGroup.LayoutParams layoutParams = txtB.getLayoutParams();                    layoutParams.height = value;                    txtB.setLayoutParams(layoutParams);                }            });            return animator;        }    }}

解决方法:

您需要重新初始化mAnimator对象.

试试下面,

在VHItem类中定义另一个成员变量以保持textB高度

public class VHItem extends RecyclerVIEw.VIEwHolder{    CardVIEw cardVIEw;    relativeLayout mrelativeLayout;    TextVIEw txtExpandCollapse, txtA, txtB;    int textBHeight; // new variable}

然后从onPreDraw方法初始化它

public boolean onPreDraw() {    vhItem.txtB.getVIEwTreeObserver().removeOnPreDrawListener(this);    vhItem.txtB.setVisibility(VIEw.GONE);    final int wIDthSpec = VIEw.MeasureSpec.makeMeasureSpec(0, VIEw.MeasureSpec.UnspecIFIED);    final int heightSpec = VIEw.MeasureSpec.makeMeasureSpec(0, VIEw.MeasureSpec.UnspecIFIED);    vhItem.txtB.measure(wIDthSpec, heightSpec);    vhItem.textBHeight = vhItem.txtB.getMeasuredHeight();    return true;}

然后在开始之前初始化动画师

private voID expand() {    txtB.setVisibility(VIEw.VISIBLE);    txtExpandCollapse.setText(R.string.fa_icon_chevron_up);    mAnimator = slIDeAnimator(0,textBHeight);     mAnimator.start();}
总结

以上是内存溢出为你收集整理的java – CardView在第二次点击时没有扩展全部内容,希望文章能够帮你解决java – CardView在第二次点击时没有扩展所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118541.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存