2015-09-28 60 views
0

我試圖在ImageView上實現AlphaAnimaiton,只要用戶點擊RecyclerView項目。我有以下代碼:Recyclerview onclick viewholder not triggering動畫

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事件。如果我用其他任何工具替換動畫,但動畫無法工作。

+0

您可以將此用於'RecyclerView' ItemAnimator' [link](https://github.com/wasabeef/recyclerview-animators) –

回答

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的父。

相關問題