【问题标题】:RecyclerView onClick ViewHolder not triggering AnimationRecyclerView onClick ViewHolder不触发动画
【发布时间】:2015-09-28 10:34:19
【问题描述】:

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

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 事件。如果我用其他任何东西替换动画,它可以工作,但动画不会工作。

【问题讨论】:

  • 你可以用这个RecyclerViewItemAnimatorlink

标签: android android-animation android-recyclerview


【解决方案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 的父级无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    相关资源
    最近更新 更多