Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply android layout animation only to children above a certain index?

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?

like image 815
CodeFusionMobile Avatar asked Jan 11 '11 14:01

CodeFusionMobile


People also ask

What is property animation in android?

A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.

How do you animate visibility on Android?

The easiest way to animate Visibility changes is use Transition API which available in support (androidx) package. Just call TransitionManager. beginDelayedTransition method then change visibility of the view. There are several default transitions like Fade , Slide .

What is Animatelayoutchanges?

Android offers pre-loaded animation that the system runs each time you make a change to the layout. All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you.


1 Answers

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);
}
like image 172
Thiago M Rocha Avatar answered Oct 22 '22 11:10

Thiago M Rocha