Recyclerview onclick视图持有者不触发动画

时间:2015-09-28 10:34:20

标签: android android-animation android-recyclerview

每当用户点击RecyclerView项目时,我都会尝试在ImageView上实现AlphaAnimaiton。我有以下代码:

class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    TextView vh_idnumber, vh_task;
    Information a;
    View vh_colorTag;
    FrameLayout checkboxLayout;
    ImageView checkboxBackground;
    Drawable checkBoxBackgroundDrawable;
    private ScaleAnimation ZoomOut;
    private ScaleAnimation ZoomIn;

    @SuppressWarnings("deprecation")
    public CustomViewHolder(View itemView)
    {
        super(itemView);

        itemView.getId();
        vh_idnumber = (TextView) itemView.findViewById(R.id.idnumber);
        vh_task = (TextView) itemView.findViewById(R.id.task);
        vh_colorTag = (View) itemView.findViewById(R.id.colortag);

        checkboxLayout = (FrameLayout) itemView.findViewById(R.id.checkBoxLayout);
        checkboxBackground = (ImageView) itemView.findViewById(R.id.checkBoxBackground);
        checkBoxBackgroundDrawable= context.getResources().getDrawable(R.drawable.circle_shape);
        checkboxBackground.setBackgroundDrawable(checkBoxBackgroundDrawable);

        ZoomOut = new ScaleAnimation(1f, 0.3f, 1f, 0.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        checkboxBackground.setAnimation(ZoomOut);
        ZoomOut.setDuration(100);
        ZoomOut.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation)
            {
                checkBoxBackgroundDrawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
            }
        });

        ZoomIn = new ScaleAnimation(0.3f, 1f, 0.3f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        checkboxBackground.setAnimation(ZoomIn);
        ZoomIn.setDuration(100);
        ZoomIn.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation)
            {
                checkBoxBackgroundDrawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
            }
        });



        random = new Random();
        int index = random.nextInt(colorCode.length);
        if(!colorRibbon)
        {
            vh_colorTag.setVisibility(View.GONE);
        }
        else
        {
            vh_colorTag.setBackgroundColor(Color.parseColor(colorCode[index]));
        }

        itemView.setOnClickListener(this);

        vh_task.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                a = MainActivity.data.get(getAdapterPosition());

                if(a.availability.matches("available"))
                {
                    vh_task.setPaintFlags(vh_task.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    vh_task.setTextColor(Color.RED);

                    a.availability="unavailable";
                }

                else if(a.availability.matches("unavailable"))
                {
                    vh_task.setPaintFlags( vh_task.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
                    vh_task.setTextColor(Color.parseColor("#212121"));

                    a.availability="available";
                }
            }
        });

        checkboxLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                changeStatus();
            }
        });
    }

    @Override
    public void onClick(View v)
    {
        changeStatus();
    }

    protected void changeStatus()
    {
        a = MainActivity.data.get(getAdapterPosition());
        if(a.status.matches("checked"))
        {
            /* This dont' work */
            ZoomOut.start();
            a.status="unchecked";
        }
        else if(a.status.matches("unchecked"))
        {
            /* Neither this */
            ZoomIn.start();
            a.status="checked";
        }
    }


}

AlphaAnimation不会被触发。但是onClick事件被触发了。使用Log测试onclick事件。如果我用其他任何东西替换动画它可以工作,但动画不会工作。

1 个答案:

答案 0 :(得分:1)

使用startAnimation代替setAnimation

可以实现您想要的效果
protected void changeStatus() {
        a = MainActivity.data.get(getAdapterPosition());
        if(a.status.matches("checked"))
        {
            checkboxBackground.startAnimation(ZoomOut);
            a.status="unchecked";
        }
        else if(a.status.matches("unchecked")) {
            checkboxBackground.startAnimation(ZoomIn);
            a.status="checked";
        }
}

setAnimation需要设置动画的开始时间,您必须使checkboxBackground的父级无效。