I have watched Android developer's videos on animating ListView insertion and deletion on YouTube. These have had very complex code in them and I tried searching for something else and found this.
http://karnshah8890.blogspot.fi/2013/04/listview-animation-tutorial.html
It has pretty simple implementation that works in most parts. My problem is that when I insert a new item in the ArrayList that is supplied to the ArrayAdapter, all the existing elements get reinserted due to adapter.notifyDataSetChanged()... I think. When I insert an entry in the ListView, how do I prevent all the entries from getting animated again? I just want the new one to animate in.
I use my ListView in the code this way:
toggles = (ListView) v.findViewById(R.id.toggleListView);
adapter = new ToggleListAdapter(getActivity(), toggleTasks);
toggles.setAdapter(adapter);
When a new item is inserted, I update the ArrayList (toggleTasks) and then call notifyDataSetChanged() on the adapter.
toggleTasks.add(task);
adapter.notifyDataSetChanged();
Here is my getView() method from the ArrayAdapter.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ToggleTask task = toggles.get(position);
String startActionName = "";
String finishActionName = "";
int imageDrawableId = -1;
// Deleted the part where I get the values for those three variables above
final Holder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.mute_toggles_toggle_view, null);
holder = new Holder();
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.startAction = (TextView) convertView.findViewById(R.id.startAction);
holder.finishAction = (TextView) convertView.findViewById(R.id.finishAction);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.image.setImageResource(imageDrawableId);
holder.startAction.setText(startActionName);
holder.finishAction.setText(finishActionName);
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.listview_push_left_in); // Third animation example on the site linked earlier
animation.setDuration(500);
convertView.startAnimation(animation);
animation = null;
return convertView;
}
in the ArrayAdatper class add a global integer (I'll call it changedIndex) and create getter and setters for it. Then before you add an item to the list say adapter.setChangedIndex(index) (whatever you name your setter). You are essentially telling it ahead of time which one you want to animate.
Change this in getView
if(changedIndex == position){
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.listview_push_left_in); // Third animation example on the site linked earlier
animation.setDuration(500);
convertView.startAnimation(animation);
animation = null;
}
then change
toggleTasks.add(task);
adapter.notifyDataSetChanged();
to
toggleTasks.add(task);
toggles.getPosition(task);
adapter.notifyDataSetChanged();
*I haven't checked any of this code but I believe it should work
Well if you want to animate only the last item inserted, then before animating check if it is last item in the List.
if (position == getCount() - 1) {
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.listview_push_left_in); // Third animation example on the site linked earlier
animation.setDuration(500);
convertView.startAnimation(animation);
animation = null;
}
Use the ListViewAnimations library. After setting up your ListView and ListAdapter like this:
MyListAdapter mAdapter = new MyListAdapter(this, getItems());
SwingRightInAnimationAdapter swingRightInAnimationAdapter = new SwingRightInAnimationAdapter(mAdapter);
// Assign the ListView to the AnimationAdapter and vice versa
swingRightInAnimationAdapter.setAbsListView(getListView());
getListView().setAdapter(swingRightInAnimationAdapter);
Call AnimationAdapter.setShouldAnimateFromPosition(int position) somewhat like this:
swingRightInAnimationAdapter.setShouldAnimateFromPosition(swingRightInAnimationAdapter.getCount());
swingRightInAnimationAdapter.add(myItem);
Have a look at the wiki for more information about how to use the library.
Related
Classic problem here: I'm fetching some data from a database into a recyclerView using an ArrayList of custom objects (and this happens in a Fragment, not in the Main Activity). Everything works like a charm until I try to refresh the recyclerView using a spinner that changes how the data is sorted. I know the data is fed to the recyclerView correctly. What am I doing wrong? API level is 19.
This is how the fetch is done:
public ArrayList<LEGOSet> getSets(String conditions) {
ArrayList<LEGOSet> sets = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM Sets " + conditions, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
LEGOSet set = new LEGOSet(cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3), cursor.getString(4), cursor.getDouble(5),
cursor.getDouble(6), cursor.getDouble(7), cursor.getDouble(8), cursor.getDouble(9), cursor.getDouble(10), cursor.getString(11),
cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getInt(15), cursor.getInt(16), cursor.getInt(17));
sets.add(set);
cursor.moveToNext();
}
cursor.close();
return sets;
}
The fragment with the data being pulled, the recyclerView being set up and with the spinner onItemSelected code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_catalog, container, false);
// pull data from database
sets = dbManager.getSets("order by pieces desc");
// set up the RecyclerView
final RecyclerView recyclerView = root.findViewById(R.id.rvSets);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new MyRecyclerViewAdapter(getActivity(), sets, portrait);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
// add dividers
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), 1);
dividerItemDecoration.setDrawable(new ColorDrawable(getResources().getColor(R.color.colorSubtle)));
recyclerView.addItemDecoration(dividerItemDecoration);
// spinner Product Sorting
spinnerProductSorting = root.findViewById(R.id.spinnerProductSorting);
spinnerProductSorting.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String s = spinnerProductSorting.getSelectedItem().toString();
switch(s)
{
case "Biggest first":
sets = dbManager.getSets("order by pieces desc");
Toast.makeText(getContext(),sets.get(0).getName(), Toast.LENGTH_SHORT).show();
break;
case "Smallest first":
sets = dbManager.getSets("order by pieces asc");
Toast.makeText(getContext(),sets.get(0).getName(), Toast.LENGTH_SHORT).show();
break;
default:
}
adapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
disclaimer = root.findViewById(R.id.disclaimer);
disclaimer.setMovementMethod(LinkMovementMethod.getInstance());
return root;
}
What I've noticed is that I can get refreshed recyclerView if I replace:
adapter.notifyDataSetChanged();
with
adapter = new MyRecyclerViewAdapter(getActivity(), sets, portrait);
recyclerView.setAdapter(adapter);
But that doesn't seem right. I should be able to refresh the existing adapter with new data instead of creating a brand new adapter, correct?
This is a classic issue faced by many developers frequently.
Quick Fix:
Replace
sets = dbManager.getSets("order by pieces desc");
with
sets.clear()
sets.addAll(dbManager.getSets("order by pieces desc"));
And same for Ascending Order also.
Explanation:
When you initialize the Adapter, you pass an Arraylist whose instance is stored by the Adapter. When you call notifyDataSetChanged(), Adapter reads the instance and refreshes the layout as per the new ArrayList. However, when you reinitialize the ArrayList with sets = dbManager.getSets("order by pieces desc");, the adapter loses the reference to the new list and is unable to refresh the layout. This can be fixed by keeping the instance the same and replacing the values which are done using clear() and addAll(list).
Feel free to ask for any doubts in comments and please mark this answer correct if I am able to solve your problem.
Inside your adapter class create method:
setData(ArrayList<LEGOSet> sets) { // new list here
this.sets = sets; // assing ArrayList from database to your list which is inside adapter class
notifyDataSetChanged();
}
and then just replace adapter.notifyDataSetChanged() with adapter.setData(sets)
I'm using listview custom adapter which with row click i'm changing row color. But when i'm scrolling bot and up again it doesnt have the right position.
It changes color in other rows...
public override View GetView(int position, View convertView, ViewGroup parent)
{
DataViewHolder holder = null;
if (convertView == null)
{
convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.TableItems, null, false);
holder = new DataViewHolder();
holder.txtDescription = convertView.FindViewById<TextView>(Resource.Id.txtDescription);
holder.txtDescription.Click += delegate
{
holder.txtDescription.SetBackgroundColor(Color.Red);
};
convertView.Tag = holder;
}
else
{
holder = convertView.Tag as DataViewHolder;
}
holder.txtDescription.Text = mitems[position].Description;
return convertView;
}
public class DataViewHolder : Java.Lang.Object
{
public TextView txtDescription { get; set; }
}
It looks like it doesnt keep in memory specific row situation.
Don't change the color in the click handler directly, instead change the data from which the adapter draws from and use that to change the color when GetView is called again.
ListView recycles the views it uses to optimize scrolling, instead it just expects the view to represent the data. If you change a color of one view directly, the view then gets recycled and you'll see "another view" (another part of the data) with a different background color.
So in summary: give each data point a color attribute and use that to set the color of each view in GetView, change the data and notify the adapter about the changes to the data.
Edit
I've never used Xamarin but maybe something like this would work
public override View GetView(int position, View convertView, ViewGroup parent)
{
DataViewHolder holder = null;
if (convertView == null)
{
convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.TableItems, null, false);
holder = new DataViewHolder();
holder.txtDescription = convertView.FindViewById<TextView>(Resource.Id.txtDescription);
holder.txtDescription.Click += delegate
{
// instead of setting the color directly here, just modify the data
(holder.txtDescription.Tag as ItemType).ItemColor = Color.Red
notifyDataSetChanged();
};
convertView.Tag = holder;
}
else
{
holder = convertView.Tag as DataViewHolder;
}
holder.txtDescription.Text = mitems[position].Description;
holder.txtDescription.Tag = mitems[position]; // this so that the click handler knows which item to modify
holder.txtDescription.SetBackgroundColor(mitems[position].ItemColor);
return convertView;
}
public class DataViewHolder : Java.Lang.Object
{
public TextView txtDescription { get; set; }
}
ListView will reuse the item layout, you can use List and View.Tag to avoid the problem caused by reusing.
I have posted my demo on github.
as in topic, when I use adapter.notifyDataSetChanged() text color in a cell which i have already changed is seting back to default value. I don't know why it happens im putting here me method for changing color:
for(int l=0;l<list.size();l++){
System.out. println("kolorujemy! "+ list.size() );
LinearLayout root = (LinearLayout) getViewByPosition(l,listView);
((TextView) root.findViewById(R.id.wartosc_calosci)).setTextColor(Color.YELLOW);
I would also add that this part of code is in loop in other thread because the vaules of the cells is updating every 30 seconds. Here is method getViewByPosition:
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount();
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition+1;
return listView.getChildAt(childIndex);
}
}
getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ListViewHolder();
convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ListViewHolder) convertView.getTag();
}
First of all this line
return listView.getAdapter().getView(pos, null, listView);
makes no sense, because with this call by hand you will internally always create and inflate new row for the list view but this view is never used within your ListView. See that you are always passing second parameter convertView null so internally this method will create new view but this view will be never used inside your ListView.
Tip 1. Don't call getView() method yourself
As you may know ListView stores in memory only as many rows/view as they are visible on the screen when you use ViewHolder pattern properly.
So for now you are setting color for every row that is visible and even those not visible that really not exist in ListView.
Tip 2.
Best way to color or change anything about any of your rows, is to do it just inside getView() method implementation depend on your adapter item state. Don't do it from outside because it looks like some kind of a hack or wrong architecture.
I'm parsing a web service to display in a listView backed by my own subclass of ArrayAdapter. The data is static ArrayList<Wait> in Application.java. You'll see it referenced by App.getWaits().
I use a simple refresh method for when there's new data. I've confirmed that it's being updated but it only renders if I navigate away and then return to the view.
In the past I've been able to refresh the listView by calling notifyDataSetChanged() on the adapter but right now none of these have worked for me. Thanks for taking a look... any ideas!?
//1 This is how I'd normally update the listView dynamically, but not tonight.
adapter.notifyDataSetChanged();
//2 It's the same thing really, so no good.
((WaitAdapter) list.getAdapter()).notifyDataSetChanged();
//3 Saw this as the answer to a similar question, doesn't work.
adapter.getWaits().clear();
adapter.getWaits().addAll(App.getWaits());
adapter.notifyDataSetChanged();
//4 Called in onCreate but tried a 2nd time in refresh() to manually reset adapter, doesn't work.
adapter = new WaitAdapter(getHost().getApplicationContext(), App.getWaits());
list.setAdapter(adapter);
//5 Kinda the same thing, new adapter, reset adapter... also no good.
WaitAdapter adapter = new WaitAdapter(getHost().getApplicationContext(), App.getWaits());
list.setAdapter(adapter);
//6 I read ArrayAdapter keeps its own reference to initial data object but this fails too.
adapter = null;
adapter = new WaitAdapter(getHost().getApplicationContext(), App.getWaits());
list.setAdapter(adapter);
*Update to share my WaitAdapter.java.
public class WaitAdapter extends ArrayAdapter<Wait> {
private LayoutInflater inflater;
private ArrayList<Wait> waits;
public WaitAdapter(Context context, ArrayList<Wait> data) {
super(context, R.layout.list_item_wait, data);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
waits = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_wait, parent, false);
holder = new ViewHolder();
holder.checkpointName = (TextView) convertView.findViewById(R.id.checkpointName);
holder.delayAmount = (TextView) convertView.findViewById(R.id.delayAmount);
holder.timeReported = (TextView) convertView.findViewById(R.id.timeReported);
holder.dateReported = (TextView) convertView.findViewById(R.id.dateReported);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Wait wait = waits.get(position);
holder.checkpointName.setText(wait.getName());
holder.delayAmount.setText(wait.getDelayInMinutes());
holder.timeReported.setText(wait.getTimeLabel());
holder.dateReported.setText(wait.getDateLabel());
return convertView;
}
#Override
public boolean isEnabled(int position) {
return false;
}
static class ViewHolder {
TextView checkpointName;
TextView delayAmount;
TextView timeReported;
TextView dateReported;
}
}
12/14/14 Update: General implementation background.
At launch the App class starts WaitAsyncTask, which parses remote XML to fill its ArrayList waits. I'll access these waits in a few places so this way I keep them global.
WaitFragment, working with WaitAdapter, displays waits in a ListView and listens for changes to waits. User's can post waits to the web service via an AlertDialog. A successful response executes WaitAsyncTask again, updating the waits object, triggering a WaitFragment refresh().
Console logs and the web service confirm this flow of control and that waits gets updated. If I leave WaitFragment then return, it shows the updated waits. The code posted with comments #1-6 are what I've tried inside of the refresh() to update the ListView.
I use this general approach with other data and fragments in this app and their UIs refresh as intended, but none are listViews. I'm not sure I could post more source without redacting most of it but I'll share my findings once I get it working. I haven't had trouble with ListView before, but it'll be something embarrassing for sure. Thanks to everyone who took a little time :)
Just create a method in Adapter class to update/refresh the listview as follows,
public class WaitAdapter extends ArrayAdapter<Wait> {
private LayoutInflater inflater;
private ArrayList<Wait> waits;
public WaitAdapter(Context context, ArrayList<Wait> data) {
super(context, R.layout.list_item_wait, data);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
waits = data;
}
/**
* Update content
*/
public void updateListContent(ArrayList<Wait> data)
{
waits = data;
notifyDataSetChanged();
}
}
In your acivity class, just call this adapter method to update the content. Refer the below code
Note:Dont clear the array content of the adapter.
//Dont clear the arraylist of adapter
adapter.updateListContent(App.getWaits());
This may help you.
I'm creating a custom adapter and using the getView method in attempt to display a default text only ONCE. Now I'm having a problem such that when I click the first Item in the list, the default text is kept but that doesn't hold for any other items? Any suggestions?
Thanks! (My code is a bit messy as I was just trying to debug)
boolean firstTime = true;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (firstTime) {
firstTime = false;
TextView firstView = new TextView(ForgotPasswordActivity.this);
firstView.setText("Please select School");
return firstView;
}
TextView view = new TextView(ForgotPasswordActivity.this);
view.setText("Hello");
return view;
}
You must play with the getCount function :
#Override
public int getCount() {
return super.getCount() -1; // This makes the trick;
}
this trick will not show last item that you've added inside your spinner(so when you finish adding your text inside the spinner, add the text that will not be shown in the spinner, and by that it will be show as a default value before clicking the spinner).
Good luck
I'm not exactly sure what your trying to do but you could force the top row to always show the select message by checking if the position is 0. Also notice in the code below that I'm reusing the convertView if it is not null. It's faster to reuse the convertView if it is available than to recreate a new view every time.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = new TextView(ForgotPasswordActivity.this);
}
if(position == 0) {
convertView.setText("Please select School");
} else {
convertView.setText("Hello");
}
return convertView;
}
Also remember that by forcing position zero to show the select message you are not showing the actual data in the adapter at position 0. Make sure this is what you want to do or insert a dummy piece of data in the first position of the backing data array.