2016-12-01 127 views
0

我想給用戶一個提示,他們可以使用刷卡刪除某個項目,所以我試圖在新項目上實現部分刷卡。我將使用ItemTouchHelper.startswipe爲此,但我需要項目視圖持有人。獲取新添加項目的視圖

我的問題是,當我添加一個項目到我的房車並調用notifyItemInserted()時,我如何獲得新添加的項目的視圖?我試過每次都會返回null的recyclerView.findViewHolderForAdapterPosition(pos)。如果有人有任何信息,我會很感激它

回答

0

我曾經做過這樣的事情。當我使用ItemTouchHelper設置滑動時,我用簡單的動畫實現了動畫。這是我做到的。動畫:

private void setupAnimation() { 
    swipeAnimation = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      int direction = animationRepeatTime < 2 ? 1 : -1; 
      if (animatingView != null) { 
       animatingView.setTranslationX(direction * interpolatedTime * Utils.convertDpToPixel(100, context)); 
      } else { 
       swipeAnimation.cancel(); 
      } 
     } 
    }; 
    swipeAnimation.setInterpolator(new OvershootInterpolator()); 
    swipeAnimation.setRepeatMode(Animation.REVERSE); 
    swipeAnimation.setRepeatCount(3); 
    swipeAnimation.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      animationRepeatTime++; 
     } 
    }); 
    swipeAnimation.setStartOffset(600); 
    swipeAnimation.setFillAfter(false); 
    swipeAnimation.setDuration(500); 
} 

在onBindViewHolder停止動畫當項目被觸摸:

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    if (holder instanceof ActivityStreamViewHolder) { 
     bindActivityHolder((ActivityStreamViewHolder) holder, position); 
     if (position == 1 && shouldAnimate) { 
      startAnimation(holder.itemView); 
     } 
     holder.itemView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        if (swipeAnimation != null && swipeAnimation.hasStarted() && !swipeAnimation.hasEnded()) { 
         shouldAnimate = false; 
         swipeAnimation.cancel(); 
         animatingView.clearAnimation(); 
         animatingView.setTranslationX(0); 
        } 
       } 
       return false; 
      } 
     }); 
    } 

爲了啓動動畫:

public void startAnimation(View view) { 
    animatingView = view; 
    if (shouldAnimate && getItemCount() > 1 && animatingView.getAnimation() == null) { 
     animatingView.startAnimation(swipeAnimation); 
     shouldAnimate = false; 
    } 
} 

你可以修改它以符合你的情況。

相關問題