java 显示时对 Listview 的每个项目进行动画处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16851195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:10:10  来源:igfitidea点击:

Animate each item of Listview when displaying

javaandroidandroid-listviewandroid-animation

提问by kaibuki

I am trying to show listview elments in a manner that each one of them animates and then become visible so after one by one they animate and get visible to user. but When I implemented the animation, its not working on individial item, but working on the whole listview :(

我试图以一种方式显示列表视图元素,它们中的每个元素都具有动画效果,然后变得可见,因此它们一个接一个地进行动画处理并对用户可见。但是当我实现动画时,它不是在单个项目上工作,而是在整个列表视图上工作:(

public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row =layoutInflater.inflate(R.layout.categories_row, parent, false);




        tvCatName = (TextView) row.findViewById(R.id.tvCatName);

        tvCatName.setText(Data.alCategoriesModels.get(position).catname);


        row.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Toasts.pop(activity, "Category id :  " + Data.alCategoriesModels.get(position).catID); 

            }
        });

//      row.setAnimation(animation);
        row.startAnimation(animation);

        return row;
    }

enter image description here

在此处输入图片说明

How to make one by one animation on each element of list view. I am extend ArrayAdapter.

如何在列表视图的每个元素上制作一个一个的动画。我正在扩展 ArrayAdapter。

回答by frogatto

You can apply a android:layoutAnimationon the ListView.

您可以android:layoutAnimationListView.

  • Create your animation XML file in animfolder. For example (slide_right_in.xml):

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true"
        android:duration="400"
        android:fromXDelta="-100%"
        android:toXDelta="0%"/>
    
  • Create another animation XML file with root element layoutAnimation:(my_layout_animation.xml)

    <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:animation="@anim/slide_right_in"
        android:delay="0.5"/>
    
  • Apply it on any ViewGroupyou want. For example ListView:

    <ListView
        android:layoutAnimation = "@anim/my_layout_animation"
        ... />
    
  • anim文件夹中创建动画 XML 文件。例如(slide_right_in.xml):

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true"
        android:duration="400"
        android:fromXDelta="-100%"
        android:toXDelta="0%"/>
    
  • 使用根元素创建另一个动画 XML 文件layoutAnimation:( my_layout_animation.xml)

    <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:animation="@anim/slide_right_in"
        android:delay="0.5"/>
    
  • 将它应用到任何ViewGroup你想要的。例如ListView

    <ListView
        android:layoutAnimation = "@anim/my_layout_animation"
        ... />
    

回答by user1971705

int delay=(position-list.getFirstVisiblePosition)*200;
if(delay<=200)
    delay=200;
animation.setStartOffset(delay);

回答by ccpizza

After messing with my own Animation implementation which kind of worked half of the times, I've found the ListViewAnimations#ExpandableListItemAdapterwhich was exactly what I needed.

在搞乱了我自己的动画实现后,我发现了ListViewAnimations#ExpandableListItemAdapter这正是我需要的。

Here is their API demo app on Google Play: ListViewAnimations

这是他们在 Google Play 上的 API 演示应用程序:ListViewAnimations

回答by Hamid

here is a tutorial to Android ListView animation

EDIT :
the idea is creating a listener and animate view inside it:

这是Android ListView 动画编辑的教程这个想法是在其中创建一个侦听器和动画视图:


lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> adapterView,final View view, final int position,
      long id) {
           anim.setAnimationListener(new Animation.AnimationListener() {

               @Override
              public void onAnimationStart(Animation animation) {
              }

              @Override
              public void onAnimationRepeat(Animation animation) {}

              @Override 
              public void onAnimationEnd(Animation animation) {
                    ItemDetail item = aAdpt.getItem(position);
                    aAdpt.remove(item);
              }
          });
      view.startAnimation(anim);
  }
});