【问题标题】:Get Viewholder of newly added item获取新添加项的 Viewholder
【发布时间】:2016-12-01 15:47:22
【问题描述】:

我想给用户一个提示,他们可以使用滑动来删除某个项目,因此我正在尝试在新项目上实现部分滑动。我将为此使用 ItemTouchHelper.startswipe 但我需要项目视图持有人。

我的问题是,当我将一个项目添加到我的 RV 并调用 notifyItemInserted() 时,我如何获得新添加项目的视图持有者?我试过recyclerView.findViewHolderForAdapterPosition(pos),它每次都返回null。如果有人有任何信息,我将不胜感激

【问题讨论】:

    标签: android android-recyclerview adapter android-viewholder


    【解决方案1】:

    我曾经做过这样的事情。当我使用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 中当 item 被触摸时停止动画:

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

    您可以修改它以匹配您的情况。

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      相关资源
      最近更新 更多