Get child view inside a Listview Adapter
I know that my adapter holds several items, by doing calling getCount:
Yes it does, but the getCount()
(returning the items in the Adapter) method of an adapter has nothing to do with getting the children of a ListView
with getChildAt()
(where you should use getChildCount()
as this will return the Views
present in the ViewGroup
).
lastAddedItem = parent.getChildAt(parent.getChildCount()-1);
This should return a non-null view, the last view returned by the getView()
method.
I just want to animate a specific View in my ViewGroup
If you are going to animate a list row(or part of that row) when it becomes visible than register a scroll listener for the ListView
and start the animation from there when the target view is appearing on the screen.
Related videos on Youtube
Tobias Moe Thorstensen
.NET & Azure Developer. https://www.moethorstensen.no
Updated on June 04, 2022Comments
-
Tobias Moe Thorstensen almost 2 years
Inside my getView method I want to get a specific child view. I know that my adapter holds several items, by doing calling getCount:
getCount();
According to the JavaDoc:
public abstract int getCount () Added in API level 1 How many items are in the data set represented by this Adapter.
Now in my getView method:
public View getView(final int position, View convertView, ViewGroup parent) { View lastAddedItem = parent.getChildAt(getCount()-1); if(lastAddedItem == null) { Log.w("ListAdapter", "View is null"); } }
This message always appears in the
LogCat
, which means that the View I try to grab isNull
.I've also tried this:
View lastAddedItem = parent.getChildAt(parent.getChildCount()-1);
So how can I grab a specific view from my
ViewGroup
object?-
Selvin about 11 yearsyou don't wana do this ... belive me ... what's you wanna achieve?
-
Tobias Moe Thorstensen about 11 yearsI just want to animate a specific
View
in myViewGroup
. Why is this bad pratice? I'm just making a copy of the view and do operations on this copy. -
Pragnani about 11 yearsIFAIK, it will always give you null.. because listview behavior is to recycle the views. so your view isn't drawn on the screen, so it is null
-