逐个动画RecyclerView项目

时间:2016-08-24 13:16:33

标签: java android xamarin android-recyclerview android-animation

我正在尝试为我的应用中的RecyclerView项设置动画。我尝试在我的适配器中使用此代码:

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        myHolder = holder as MyView;
        if (position > mCurrentPosition)
        {
            int currentAnim = Android.Resource.Animation.SlideInLeft;
            SetAnimation(holder.ItemView, currentAnim);
            mCurrentPosition = position;
        }

    }

    private void SetAnimation(View view, int currentAnim)
    {

        Animation anim = AnimationUtils.LoadAnimation(mContext, currentAnim);
        anim.SetInterpolator(mContext, Android.Resource.Interpolator.Bounce);
        view.StartAnimation(anim);
    }

但是,因为我的所有项目在创建视图时一起显示,而在用户单击按钮时不会添加,例如,所有项目一起动画。

我希望这些项目按照其位置的顺序依次制作动画。
这怎么可能呢?

4 个答案:

答案 0 :(得分:2)

这个怎么样:

  Started PATCH "/campings/1" for 127.0.0.1 at 2016-08-28 17:24:27 +0200
    Processing by CampingsController#update as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"I7otqLV4d8oqWdCncp5yeZK1tX/NeijUMHpA+XtH+zGDu8qMtNc3PdcvvDx+iz8UTS0Fe+t88ees6yahyducwQ==", "camping"=>{"name"=>"nomducamp", "adresse"=>"adresse1", "code_postale"=>"10100", "commune"=>"SAINT-HILAIRE-SOUS-ROMILLY", "courriel"=>"mail@mail.com", "site_internet"=>"www.example.com", "tel"=>"0101010101", "description"=>"desc", "etoile"=>"3", "piscine"=>"Non renseigné"}, "commit"=>"Enregistrer", "id"=>"1"}
      [1m[36mCamping Load (0.0ms)[0m  [1m[34mSELECT  "campings".* FROM "campings" WHERE "campings"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
      [1m[36mProprietaire Load (0.0ms)[0m  [1m[34mSELECT  "proprietaires".* FROM "proprietaires" WHERE "proprietaires"."id" = ? ORDER BY "proprietaires"."id" ASC LIMIT ?[0m  [["id", 7], ["LIMIT", 1]]
    Unpermitted parameter: piscine
      [1m[35m (0.0ms)[0m  [1m[36mbegin transaction[0m
      [1m[35m (1.0ms)[0m  [1m[36mcommit transaction[0m
    Redirected to http://localhost:3000/campings/1
    Completed 302 Found in 579ms (ActiveRecord: 1.0ms)

更新:如果要按顺序为所有项目设置动画,则应将逻辑代码放在适配器旁边。
例如,在活动中,在为RecyleView设置适配器

之后
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
    myHolder = holder as MyView;
    if (position > mCurrentPosition)
    {
        Handler h = new Handler();
        int currentAnim = Android.Resource.Animation.SlideInLeft;
        Action myAction = () => 
        {
            SetAnimation(holder.ItemView, currentAnim);
        };

        h.PostDelayed(myAction, 1000);


        mCurrentPosition = position;
    }

}

答案 1 :(得分:0)

这可能不是答案,但可能有所帮助。我认为你应该在你的动画之间设置一个延迟。

new Handler().postDelayed(new Runnable()
{
   @Override
   public void run()
   {
     // your code that you want to delay here
   }
}, 1000/* 1000ms = 1sec delay */);

答案 2 :(得分:0)

您需要为动画添加延迟时间并增加每个项目绑定的延迟。

int delayAnimate = 300; //global variable

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    //set view to INVISIBLE before animate
    holder.viewAnimate.setVisibility(View.INVISIBLE);
    setAnimation(holder.ll_root);
}

private void setAnimation(final View view) {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left);
            if(view!=null){
                view.startAnimation(animation);
                view.setVisibility(View.VISIBLE);
            }
        }
    }, delayAnimate);
    delayAnimate+=300;
}

答案 3 :(得分:0)

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    Handler().postDelayed({
        // animate here
    }, DURATION * position)
}

DURATION *位置有助于逐个动画回收物品。
当设定在50-75之间时,DURATION最佳。