0

ユーザーにスワイプを使用して特定のアイテムを削除できるというヒントを与えたいので、新しいアイテムの部分スワイプを実装しようとしています。これに対してItemTouchHelper.startswipeを使用しますが、アイテムビューの所有者が必要です。新しく追加されたアイテムのビューホルダーを取得

私のRVにアイテムを追加してnotifyItemInserted()を呼び出すと、新しく追加されたアイテムのビューホルダーはどうやって取得できますか?私は毎回nullを返すrecyclerView.findViewHolderForAdapterPosition(pos)を試しました。もし誰かが何か情報を持っていれば、私はそれを感謝しました

答えて

0

私はこのようなことをしました。 ItemTouchHelperを使ってスワイプをセットアップしている間、私は単純なアニメーションでアニメーションを達成しました。ここで私はそれをやった。アニメーション:onBindViewHolderで

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); 
} 

項目をタッチするアニメーションを停止する:アニメーションを開始するために

@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; 
    } 
} 

あなたはあなたのケースに合わせて、それを修正することができます。

関連する問題