【问题标题】:Android Add to cart Animation from Recyclerview来自 Recyclerview 的 Android 添加到购物车动画
【发布时间】:2017-05-20 19:28:33
【问题描述】:

我有一个显示产品列表的回收站视图,并且每个回收站视图行项目中都有一个购物车图标。 单击购物车图标时,我希望产品图像复制重新缩放并移动到右上角进入 操作栏上的购物车图标

我的方法:

在recyclerview的row item xml中创建了一个ImageView,并使其最初不可见。 单击 recyclerview 中的购物车图标后,该图像视图可见并使用图像 url 填充图像视图,并从开始和结束位置添加了翻译动画。

当前结果:

图像在相应的单个项目行内移动,一旦到达行项目的边界就会消失。我的意思是图像不会从行项目中出来,没有到达顶部操作栏菜单项。

已尝试的解决方案:

public static void setAllParentsClip(View v, boolean enabled) {
        while (v.getParent() != null && v.getParent() instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) v.getParent();
            viewGroup.setClipChildren(enabled);
            viewGroup.setClipToPadding(enabled);
            v = viewGroup;
        }
    }

同样在xml中手动设置所有的父母:

android:clipChildren="false"
android:clipToPadding="false"

图像在触及行项目边界后仍然消失。任何建议都会有很大帮助

我的 RecyclerView 适配器的代码:

public class TrendingItemsAdapter extends RecyclerView.Adapter<TrendingItemsAdapter.MyViewHolder> {

    private List<ProductEventDetail> productEventDetailList;
    private Context context;


    public TrendingItemsAdapter (List<ProductEventDetail> productEventDetailList){
        this.productEventDetailList=productEventDetailList;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView productName;
        private TextView progressDisplay;
        private ImageView imageView, addToCart,animImg;
        private ProgressBar progressBar;

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public MyViewHolder(View view) {
            super(view);
            productName=(TextView)view.findViewById(R.id.trending_Product_Name);
            imageView=(ImageView)view.findViewById(R.id.trending_product_img);
            addToCart=(ImageView)view.findViewById(R.id.add_to_cart);
            animImg=(ImageView)view.findViewById(R.id.animProductImg);
            progressDisplay=(TextView)view.findViewById(R.id.event_progress_value);
            progressBar=(ProgressBar)view.findViewById(R.id.event_progress);
            progressBar.setProgressDrawable(context.getDrawable(R.drawable.progress_tint));

            addToCart.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(v.getId()==addToCart.getId()){

                Toast.makeText(context,productEventDetailList.get(getAdapterPosition()).getProductName(),Toast.LENGTH_SHORT).show();
                setAllParentsClip(animImg,false);

                // Animation code

                Animations anim = new Animations();
                int fromLoc[] = new int[2];
                imageView.getLocationOnScreen(fromLoc);
                animImg.setVisibility(View.VISIBLE);

                Glide.with(context)
                        .load(productEventDetailList.get(getAdapterPosition()).getProductImageUrl())
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .fitCenter()
                        .into(animImg);

                float startX = fromLoc[0];
                float startY = fromLoc[1];
                Animation a = anim.fromAtoB(startX,startY, 1028, 143, animL,2000);
                animImg.setAnimation(a);
                a.startNow();

            }



        }

        Animation.AnimationListener animL = new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //this is just a method call you can create to delete the animated view or hide it until you need it again.
                animImg.setVisibility(View.GONE);
            }
        };
    }



    @Override
    public TrendingItemsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        context=parent.getContext();

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.trending_item_row_detail, parent, false);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callProductEventDetails();
            }
        });


        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(TrendingItemsAdapter.MyViewHolder holder, int position) {

        ProductEventDetail productEventDetail= productEventDetailList.get(position);
        holder.productName.setText(productEventDetail.getProductName()+ " xxxx xxxxxxxxxxxxxxxx..");
        holder.progressBar.setProgress(productEventDetail.getEventProgressStatus());
        holder.progressDisplay.setText(String.valueOf(productEventDetail.getEventProgressStatus())+"% ");

        Glide.with(context)
                .load(productEventDetail.getProductImageUrl())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .fitCenter()
                .into(holder.imageView);


    }

    @Override
    public int getItemCount() {
        return productEventDetailList.size();
    }

private void callProductEventDetails(){
    Intent intent= new Intent(context, ProductEventDetailActivity.class);
    intent.putExtra("eventStatus","inProgress");
    context.startActivity(intent);
}

    public static void setAllParentsClip(View v, boolean enabled) {
        while (v.getParent() != null && v.getParent() instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) v.getParent();
            viewGroup.setClipChildren(enabled);
            viewGroup.setClipToPadding(enabled);
            v = viewGroup;
        }
    }
    }

【问题讨论】:

  • 你做到了吗?
  • @FlorescuGeorgeCătălin 是的...我有点做到了,我可以将图像发送到购物车,其大小为静态...但是在向上移动时调整它的大小会导致它远离目标。 ..但如果它是静态大小的图像,它会完全移动到购物车中......
  • 有这个代码吗?
  • 没有人这个代码不会工作......因为我部分实现了它我无法将它作为解决方案发布......你需要任何帮助吗??
  • 是的,如果你愿意的话.. 我想看看你的代码,你在哪里创建动画并开始它,拜托。也许我们可以弄清楚该怎么做..

标签: android android-animation


【解决方案1】:

我认为您正在寻找添加到购物车的动画, 你可以在这里找到解决方案 fly to cart animation

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 2017-06-24
    • 2011-12-19
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多