如何仅将android布局动画应用于特定索引以上的子项?

发布于 2024-10-11 07:09:13 字数 302 浏览 8 评论 0原文

我有一个包含一系列注释的 ListView。

目前,当列表首次加载时,我使用布局动画将所有笔记从侧面滑入;这很有效。

但是,我试图弄清楚如何仅将布局动画应用于列出低于某一点的项目。假设我删除了列表中的一个项目:我希望将其下方的所有项目向上移动到已删除笔记的旧位置。

我尝试找到一种通过子索引自定义动画延迟或插值器的方法,但没有找到适合此位置的任何内容。有没有办法使用自定义布局动画(例如扩展 LayoutAnimationController)来执行此操作,或者我必须执行此低级别操作并单独为每个视图设置动画?

有什么建议吗?

I have a ListView containing a series of notes.

Currently I use a layout animation to slide all the notes in from the side when the list first loads; this works perfectly.

However, I'm trying to figure out how to apply a layout animation only to list items below a certain point. Say I delete an item on the list: I'd like all items below it to shift up into the deleted note's old spot.

I've tried finding a way to customize the animation delays or interpolators by child index but haven't found anything appropriate for this location. Is there a way to do this using a custom layout animation (such as extending LayoutAnimationController) or would I have to do this low level and animate each view individually?

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攀登最高峰 2024-10-18 07:09:13

创建动画并在列表中调用它 OnItemClickListener。之后,您可以使用适配器的 notifyDataSetChanged 刷新列表内容。

在此示例中,我创建了一个名为 removeListItem 的方法,该方法接收要删除的行以及该行在列表内容数组中的位置。

public class MainActivity extends ListActivity implements OnItemClickListener{

ArrayList<String> values;
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    values = generateMockData(50);

    adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, values);

    setContentView(R.layout.activity_main);

    getListView().setAdapter(adapter);
    getListView().setOnItemClickListener(this);
}

private ArrayList<String> generateMockData(int number) {

    ArrayList<String> result = new ArrayList<String>();

    for(int i = 0; i < number; i++)
        result.add(""+i+" "+ (int)Math.random() * 13);

    return result;
}

private void removeListItem(View rowView, final int positon) {

    Animation anim = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_out_right);
    anim.setDuration(500);
    rowView.startAnimation(anim);

    new Handler().postDelayed(new Runnable() {

        public void run() {

            values.remove(positon);//remove the current content from the array

            adapter.notifyDataSetChanged();//refresh you list

        }

    }, anim.getDuration());

}

public void onItemClick(AdapterView<?> arg0, View row, int position, long arg3) {

       if(position == YOUR_INDEX) //apply your conditions here!
          removeListItem(row,position);
}

Create your animation and call it in your list OnItemClickListener. After that you might use the adapter's notifyDataSetChanged to refresh the list content.

In this example, I created a method called removeListItem with receives the row you want to remove and the position of that row in you list content array.

public class MainActivity extends ListActivity implements OnItemClickListener{

ArrayList<String> values;
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    values = generateMockData(50);

    adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, values);

    setContentView(R.layout.activity_main);

    getListView().setAdapter(adapter);
    getListView().setOnItemClickListener(this);
}

private ArrayList<String> generateMockData(int number) {

    ArrayList<String> result = new ArrayList<String>();

    for(int i = 0; i < number; i++)
        result.add(""+i+" "+ (int)Math.random() * 13);

    return result;
}

private void removeListItem(View rowView, final int positon) {

    Animation anim = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_out_right);
    anim.setDuration(500);
    rowView.startAnimation(anim);

    new Handler().postDelayed(new Runnable() {

        public void run() {

            values.remove(positon);//remove the current content from the array

            adapter.notifyDataSetChanged();//refresh you list

        }

    }, anim.getDuration());

}

public void onItemClick(AdapterView<?> arg0, View row, int position, long arg3) {

       if(position == YOUR_INDEX) //apply your conditions here!
          removeListItem(row,position);
}
戴着白色围巾的女孩 2024-10-18 07:09:13

我遇到了一个非常类似的问题,并且能够找到一个带有简单 View 子类的解决方案,该子类允许您使用布局动画控制器仅对您指定的视图进行动画处理。请参阅此链接:

LayoutAnimationController 能否仅对指定视图进行动画处理

I had a very similar problem and was able to find a solution with a simple View subclass that allows you to use a layout animation controller to animate only the views that you specify. Please see this link:

Can LayoutAnimationController animate only specified Views

尾戒 2024-10-18 07:09:13

在布局 xml 中,尝试添加:

<ListView  
  android:id="@android:id/list"
  ... 
  android:animateLayoutChanges="true"/>  

这将自动为列表视图中的插入和删除设置动画。
如果您有名为 anim_translate_left 的自定义动画,请改用:

<ListView  
  android:id="@android:id/list"
  ... 
  android:animateLayoutChanges="@anim/anim_translate_left"/>   

来源:Google API

In your layout xml, try adding:

<ListView  
  android:id="@android:id/list"
  ... 
  android:animateLayoutChanges="true"/>  

This will automatically animate insertions and deletions from your listview.
If you have a custom animation, named anim_translate_left, use instead:

<ListView  
  android:id="@android:id/list"
  ... 
  android:animateLayoutChanges="@anim/anim_translate_left"/>   

Source: Google API's

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文