I have a ListView
that displays a list of states. Whenever the user selects a state, I will load a list of cities that are in this state. For this, I have a manager that, whenever an item is selected, will replace the underlying list that my custom BaseAdapter
reads with the item's children.
The problem is, this works fine, but the items are just replaced on the spot, I'd really want a nice transition for this, that is, the list of states moving to the left and being replaced by the list of cities coming from the other edge
Is there a class or method that could help me animate that? The only option I've thought is to keep two different Listview
and fade the first one out while the other one fades in. Not the most elegant solution, I was wondering if this could be achieved somehow with only one ListView
I'm not sure how to do this with ListView
, but I know you could use a RecyclerView
and add animations in the adapter's onBindViewHolder
method.
This method is called once for every visible item when you call notifyDataSetChanged()
.
Edit:
Something like this:
Some Class:
RecyclerView recyclerv;
MyAdapter mAdapter;
LinearLayoutManager mLayoutManager;
List<String> mDataset;
//instantiate recyclerview, adapter, layoutmanager, dataset. populate dataset.
recyclerv.setDataset(mDataset);
MyAdapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> dataset;
public MyAdapter(){}
//extend ViewHolder class here
public class ViewHolder extends RecyclerView.Holder { ... }
//implement onCreateViewHolder
/*implement onBindViewHolder.
*animate your view here, but be sure to add an animation count
else the items will keep getting animated everytime a new item appears */
//implement getItemCount
public void setDataset(List<String> dataset) {
this.dataset = dataset;
notifyDataSetChanged(); //this will trigger onBindViewHolder for every visible item
}
}