在onCreateViewHolder上对其进行充气时获取空布局

时间:2017-07-20 20:03:07

标签: android android-layout android-recyclerview android-viewholder

我有一个RecycleView,它的项目在点击时会进行缩放和过渡动画。在onBindViewHolder()中,我有时会在动画后状态中获得views,并且我试图通过保存原始布局位置和大小并将其设置为当前布局来撤消动画所做的缩放和转换。 viewHolder:

private void returnToOriginalSate(ItemViewHolder holder) {
    View groupLogo1 = holder.parent.findViewById(R.id.group_logo1);
    View groupLogo2 = holder.parent.findViewById(R.id.group_logo2);
    View leagueLogo = holder.parent.findViewById(R.id.league_logo_parent);

    groupLogo1.setX(originalLayout.findViewById(R.id.group_logo1).getX());
    groupLogo1.setY(originalLayout.findViewById(R.id.group_logo1).getY());
    groupLogo1.setLayoutParams(originalLayout.findViewById(R.id.group_logo1).getLayoutParams());

    groupLogo2.setX(originalLayout.findViewById(R.id.group_logo2).getX());
    groupLogo2.setY(originalLayout.findViewById(R.id.group_logo2).getY());
    groupLogo2.setLayoutParams(originalLayout.findViewById(R.id.group_logo2).getLayoutParams());

    leagueLogo.setX(originalLayout.findViewById(R.id.league_logo_parent).getX());
    leagueLogo.setY(originalLayout.findViewById(R.id.league_logo_parent).getY());
    leagueLogo.setLayoutParams(originalLayout.findViewById(R.id.league_logo_parent).getLayoutParams());
}

要获取originalLayout我正在对其进行充气并将其保存在onCreateViewHolder

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ItemViewHolder viewHolder = new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_in_feed, parent, false));
    if(originalLayout == null){
        originalLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_in_feed, parent, false);

    }
    return viewHolder;
}

问题是我得到了奇怪的originalLayout值,例如此布局中的ImageView具有LayoutParams:-1和x,y位置:(0,0)

为什么?我如何获得真正的originalLayout并且有更好的方法吗?

编辑:当我重置循环视图的位置时,这是onbindview

 @Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
         if(this view is recycled and should be reset){
             returnToOriginalState(holder);

    }
}

1 个答案:

答案 0 :(得分:0)

关于我们在评论中的讨论,当动画结束时,将反向()应用于持有者视图的动画师:

animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        animation.removeListener(this);
        animation.setDuration(0);
        ((ValueAnimator) animation).reverse();
    }
});