Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get child view inside a Listview Adapter

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 is Null.

I've also tried this:

View lastAddedItem = parent.getChildAt(parent.getChildCount()-1);

So how can I grab a specific view from my ViewGroup object?

like image 519
Tobias Moe Thorstensen Avatar asked Nov 23 '25 01:11

Tobias Moe Thorstensen


1 Answers

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.

like image 150
user Avatar answered Nov 24 '25 17:11

user



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!