Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Animated ListView with TransitionManager

View MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package org.lucasr.transition.samples;
 
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.transition.TransitionManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
 
import java.util.Random;
 
public class MainActivity extends ActionBarActivity {
private ListView listView;
private TransitionAdapter adapter;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
listView = (ListView) findViewById(R.id.list);
adapter = new SomeAdapter(this);
adapter.add("One");
adapter.add("Another");
 
listView.setAdapter(adapter);
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
 
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int id = item.getItemId();
if (id == R.id.action_add) {
TransitionManager.beginDelayedTransition(listView);
adapter.add(1, "New item");
return true;
} else if (id == R.id.action_remove) {
TransitionManager.beginDelayedTransition(listView);
adapter.remove(1);
return true;
}
 
return super.onOptionsItemSelected(item);
}
}
View MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
package org.lucasr.transition.samples;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
import java.util.ArrayList;
import java.util.List;
 
public class SomeAdapter extends BaseAdapter {
private static class Item {
public final long id;
public final String value;
 
public Item(long id, String value) {
this.id = id;
this.value = value;
}
}
 
private final Context context;
private final List<Item> items = new ArrayList<Item>();
private int currentId = 0;
 
public SomeAdapter(Context context) {
super();
this.context = context;
}
 
@Override
public int getCount() {
return items.size();
}
 
@Override
public Object getItem(int position) {
return items.get(position);
}
 
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.text_item, parent, false);
}
 
Item item = items.get(position);
((TextView) convertView).setText(item.value + " (" + item.id + ")");
 
return convertView;
}
 
@Override
public long getItemId(int position) {
return items.get(position).id;
}
 
@Override
public boolean hasStableIds() {
return true;
}
 
private Item createItem(String value) {
long id = ++currentId;
return new Item(id, value);
}
 
public void add(String value) {
items.add(createItem(value));
notifyDataSetChanged();
}
 
public void add(int index, String value) {
items.add(index, createItem(value));
notifyDataSetChanged();
}
 
public void remove(int index) {
items.remove(index);
notifyDataSetChanged();
}
}
View MainActivity.java
1 2 3 4 5 6 7
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.lucasr.transition.samples.MainActivity"/>
View MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="2dp"
android:paddingLeft="64dp"
android:paddingRight="64dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:background="#ffcc00"/>

If the Adapter can get a reference to the ListView, is there anything stopping you from calling TransitionManager.beginDelayedTransition(listView) in notifyDataSetChanged()? I'd like to do this in a ListView populated by a CursorAdapter where the contents can change at any time.

@keyboardr It's better to leave it in the ListView or Activity, and use PageAdapter.registerDataSetObserver(DataSetObserver) to register to be notified with notifyDataSetChanged() is triggered. Then you can do the TransitionManager.beginDelayedTransition(listView) there. You really don't want the adapter to deal with the ListView.

@lucasr You have SomeAdapter as the class name, but elsewhere (including the constructor) you say TransitionAdapter (line 14 of SomeAdapter.java). Do you mean for them to always say TransitionAdapter instead of SomeAdapter?

well a good approach but you wont be needing this anymore since the RecyclerView has come into the picture, it supports 2 types of animations 1) Animators that manage structural or data change and animate them and 2)Animations that you can apply on the ViewHolder root inside onBindViewHolder for animating how the recyclerview is loaded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.