【问题标题】:How to set Animation Slide Down to card view when click on arrow down in ViewHolder?单击ViewHolder中的向下箭头时如何将动画向下滑动设置为卡片视图?
【发布时间】:2017-03-30 05:14:13
【问题描述】:

当用户单击向下箭头图标时,我尝试将向下滑动动画设置为卡片,但当我 setAnimationListener(this) 时它不起作用。它在“this”关键字处出错。 注意 我在片段中使用回收站视图和卡片。 下面是我的代码

class ListViewHolder extends RecyclerView.ViewHolder{
        TextView tvTitle, tvDescription;
        ImageView ivDropDown;
        LinearLayout ll_footer;
        Button btnClose;
        Animation slideDown;

        public ListViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvDescription = (TextView) itemView.findViewById(R.id.tv_description);
            ivDropDown = (ImageView) itemView.findViewById(R.id.iv_arrow_down);
            ll_footer = (LinearLayout) itemView.findViewById(R.id.ll_footer);
            btnClose = (Button) itemView.findViewById(R.id.btn_close);

            btnClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    callBack.onButtonCloseClick(getAdapterPosition());
                    ll_footer.setVisibility(View.GONE);
                }
            });


            ivDropDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    callBack.onImageClick(getAdapterPosition());
                    slideDown = AnimationUtils.loadAnimation(context, R.anim.slide_down);
                    ll_footer.setAnimation(slideDown);
                    slideDown.setAnimationListener();
                    ll_footer.setVisibility(View.VISIBLE);
                }
            });
        }
    }

【问题讨论】:

    标签: android android-animation android-viewholder


    【解决方案1】:

    你还没有开始动画。 这样做:

    class ListViewHolder extends RecyclerView.ViewHolder implements Animation.AnimationListener{ 
        TextView tvTitle, tvDescription;
        ImageView ivDropDown;
        LinearLayout ll_footer;
        Button btnClose;
        Animation slideDown;
    
        public ListViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvDescription = (TextView) itemView.findViewById(R.id.tv_description);
            ivDropDown = (ImageView) itemView.findViewById(R.id.iv_arrow_down);
            ll_footer = (LinearLayout) itemView.findViewById(R.id.ll_footer);
            btnClose = (Button) itemView.findViewById(R.id.btn_close);
    
            btnClose.setOnClickListener(new View.OnClickListener() {
                @Override 
                public void onClick(View view) {
    //                    callBack.onButtonCloseClick(getAdapterPosition()); 
                    ll_footer.setVisibility(View.GONE);
                } 
            }); 
    
    
            ivDropDown.setOnClickListener(new View.OnClickListener() {
                @Override 
                public void onClick(View view) {
                    callBack.onImageClick(getAdapterPosition()); 
                    slideDown = AnimationUtils.loadAnimation(context, R.anim.slide_down);
                    ll_footer.startAnimation(slideDown);
                    slideDown.setAnimationListener(ListViewHolder.this);
                    ll_footer.setVisibility(View.VISIBLE);
                } 
            }); 
        } 
        @Override
        public void onAnimationStart(Animation animation) {
    
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
    
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {
    
        }
    } 
    

    【讨论】:

    • 当我点击recyclerview索引0处的项目时,索引7或9处的项目也会向下滑动。
    【解决方案2】:

    首先,创建以下名为 slide_down.xml 的动画文件:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromYDelta="-100%"
            android:toYDelta="0" />
    </set>
    

    之后,使用上面的动画文件如下:

    ivDropDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    callBack.onImageClick(getAdapterPosition());
                    Animation slideDown = AnimationUtils.loadAnimation(mContext,
                    R.anim.slide_down);
            slideDown.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    // show the footer
                    ll_footer.setVisibility(View.VISIBLE);
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
                    ll_footer.startAnimation(slideDown);
                }
            });
    

    【讨论】:

      【解决方案3】:
       Pass context from activity to adapter class using adapter constructor. 
      

      然后使用以下几行并删除动画侦听器。

       slideDown = AnimationUtils.loadAnimation(context, R.anim.slide_down);
                              ll_footer.startAnimation(slideDown);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-13
        • 2012-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多