2016-12-16 83 views
2

我有一個列表視圖。我想添加一個動畫,如果我選擇一個列表項目,那麼它將被刪除,該項目下的其他項目將隨着向上滑動動畫向上移動。我已通過獲取其子位置來完成線性佈局,但我無法理解如何滑動listview的其餘元素。請幫助向上滑動選定的列表視圖項目的動畫

下面是我在動態創建的線性佈局動畫代碼

plus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      Animation animation1 = AnimationUtils.loadAnimation(getActivity(), R.anim.move); 
      final Animation animation2 = AnimationUtils.loadAnimation(getActivity(), R.anim.moveup); 

這是該項目的動畫製作上,我們點擊

  lay1.startAnimation(animation1); 

這是代碼動畫休息的孩子向上滑動

  final int i = lay1.getId() + 1; 


      final Handler handler = new Handler(); 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        //Do something after 100ms 

        try { 
         LinearLayout l = (LinearLayout)      layoutall.getChildAt(i); 
         l.startAnimation(animation2); 
         layoutall.removeView(lay1); 
        } catch (Exception e) { 

         //connectToDatabase(); 
        } 
       } 
      }, 600); 

更新代碼 非常感謝。我能夠實現這個功能,但問題是我在一個列表視圖中使用了兩個動​​畫,但無法實現第一個動畫。

我的列表項的代碼就是我要刷上點擊

 viewHolder.accept.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       removeListItem(viewHolder.order_card_layout, position); 

      } 
     }); 

和代碼動畫左邊,而爲

 protected void removeListItem(final View rowView, final int positon) { 
    // TODO Auto-generated method stub 


    final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move); 
    rowView.startAnimation(animation); 

    Animation.AnimationListener al = new Animation.AnimationListener() { 
     @Override 
     public void onAnimationEnd(Animation arg0) { 

      ViewHolder vh = (ViewHolder) rowView.getTag(); 
      //vh.needInflate = true; 

     } 
     @Override public void onAnimationRepeat(Animation animation) {} 
     @Override public void onAnimationStart(Animation animation) {} 
    }; 


    collapse(rowView, al); 

}

但問題是我的第一個動畫不能正常工作,那就是

final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move); 
    rowView.startAnimation(animation); 

新更新的代碼 我能夠實現這個功能,但問題是,我用了兩個動​​畫和想要做的關於該項目的第一個動畫人,我點擊,並在所有的列表項另一個動畫但是我的問題是,我單擊幻燈片右側的項目,它也重新出現並向上滑動整個列表,但我希望我點擊的項目將向右滑動,其餘部分將滑動。

我的列表項的代碼就是我要刷上點擊

 viewHolder.accept.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       removeListItem(viewHolder.order_card_layout, position); 

      } 
     }); 

和代碼動畫左邊,而爲

保護無效removeListItem(最終查看rowView,最終詮釋當前位置){ // TODO自動生成方法存根

final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move); 
    rowView.startAnimation(animation); 


    final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      //Do something after 100ms 

      try { 


       Animation.AnimationListener al = new Animation.AnimationListener() { 
        @Override 
        public void onAnimationEnd(Animation arg0) { 

         ViewHolder vh = (ViewHolder) rowView.getTag(); 
         //vh.needInflate = true; 

        } 

        @Override 
        public void onAnimationRepeat(Animation animation) { 
        } 

        @Override 
        public void onAnimationStart(Animation animation) { 
        } 
       }; 


       collapse(rowView, al); 


      } catch (Exception e) { 

       //connectToDatabase(); 
      } 
     } 
    }, 600); 





} 






      private void collapse(final View v, Animation.AnimationListener al) { 
    final int initialHeight = v.getMeasuredHeight(); 

    Animation anim = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      if (interpolatedTime == 1) { 
       v.setVisibility(View.GONE); 
      } 
      else { 
       v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime); 
       v.requestLayout(); 
      } 
     } 

     @Override 
     public boolean willChangeBounds() { 
      return true; 
     } 
    }; 

    if (al!=null) { 
     anim.setAnimationListener(al); 
    } 
    anim.setDuration(600); 
    v.startAnimation(anim); 
} 

回答

0

這裏是代碼,我在哪裏都實現動畫。

1)從右側滑動,同時點擊列表視圖項目單擊。 2)當點擊刪除按鈕時,列表視圖中的剩餘行的滑動動畫(已經在我給出的鏈接中完成)。

將以下文件複製到您的anim文件夾中。

1)fade_out.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <alpha 
     android:duration="700" 
     android:fromAlpha="1" 
     android:toAlpha="0" /> 
</set> 

2)fade_in.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <alpha 
     android:duration="700" 
     android:fromAlpha="0" 
     android:toAlpha="1" /> 
</set> 

3)slide_in_right.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <translate 
     android:duration="700" 
     android:fromXDelta="100%" 
     android:toXDelta="0%" /> 
</set> 

4)slide_out_right.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <translate 
     android:duration="700" 
     android:fromXDelta="0%" 
     android:toXDelta="100%" /> 
</set> 

現在添加下面的方法和類的活動,

//Animation 
    private void fadeOutView(View view) { 
     Animation fadeOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_out); 
     if (fadeOut != null) { 
      fadeOut.setAnimationListener(new ViewAnimationListener(view) { 
       @Override 
       protected void onAnimationStart(View view, Animation animation) { 

       } 

       @Override 
       protected void onAnimationEnd(View view, Animation animation) { 
        view.setVisibility(View.GONE); 
       } 
      }); 
      view.startAnimation(fadeOut); 
     } 
    } 

    private void fadeInView(View view) { 
     Animation fadeIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in); 
     if (fadeIn != null) { 
      fadeIn.setAnimationListener(new ViewAnimationListener(view) { 
       @Override 
       protected void onAnimationStart(View view, Animation animation) { 
        view.setVisibility(View.VISIBLE); 
       } 

       @Override 
       protected void onAnimationEnd(View view, Animation animation) { 

       } 
      }); 
      view.startAnimation(fadeIn); 
     } 
    } 

    private void slideInView(View view) { 
     Animation slideIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_in_right); 
     if (slideIn != null) { 
      slideIn.setAnimationListener(new ViewAnimationListener(view) { 
       @Override 
       protected void onAnimationStart(View view, Animation animation) { 
        view.setVisibility(View.VISIBLE); 
       } 

       @Override 
       protected void onAnimationEnd(View view, Animation animation) { 

       } 
      }); 
      view.startAnimation(slideIn); 
     } 
    } 

    private void slideOutView(View view) { 
     Animation slideOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_out_right); 
     if (slideOut != null) { 
      slideOut.setAnimationListener(new ViewAnimationListener(view) { 
       @Override 
       protected void onAnimationStart(View view, Animation animation) { 

       } 

       @Override 
       protected void onAnimationEnd(View view, Animation animation) { 
        view.setVisibility(View.GONE); 
       } 
      }); 
      view.startAnimation(slideOut); 
     } 
    } 

    private abstract class ViewAnimationListener implements Animation.AnimationListener { 

     private final View view; 

     protected ViewAnimationListener(View view) { 
      this.view = view; 
     } 

     @Override 
     public void onAnimationStart(Animation animation) { 
      onAnimationStart(this.view, animation); 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      onAnimationEnd(this.view, animation); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 

     protected abstract void onAnimationStart(View view, Animation animation); 

     protected abstract void onAnimationEnd(View view, Animation animation); 
    } 

現在,如果你想顯示在列表項動畫點擊然後,給動畫適配器類,其中您已經爲listView充氣raw_layout。

5)raw_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:id="@+id/linLayout"> 

    <ImageButton 
     android:id="@+id/cell_trash_button" 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:src="@drawable/trash_can" 
     android:scaleType="fitXY" /> 

    <TextView 
     android:id="@+id/cell_name_textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|left" 
     android:layout_marginLeft="16dp" 
     android:layout_weight="1" 
     android:text="cell name" /> 

</LinearLayout> 

6)最後將動畫應用於在getView()父母的適配器的onClick的方法raw_layout的父佈局Layout.Here我對raw_layout父佈局的LinearLayout其編號分別爲linLayout

vh.linLayout.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        if (vh.linLayout.getVisibility() == View.VISIBLE) { 
         fadeOutView(vh.linLayout); 
         slideInView(vh.linLayout); 
        } else { 
         fadeInView(vh.linLayout); 
         slideOutView(vh.linLayout); 
        } 
       } 
      }); 

我已經試過這個,它的工作。所以試試這一次!

+0

不,這不起作用 –

+0

發佈您的代碼。 –

+0

已經發布,我已在上面發佈。這是我的代碼。並且你的代碼不能以我需要的方式工作 –