Get child view inside a Listview Adapter

10,055

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.

Share:
10,055

Related videos on Youtube

Tobias Moe Thorstensen
Author by

Tobias Moe Thorstensen

.NET & Azure Developer. https://www.moethorstensen.no

Updated on June 04, 2022

Comments

  • Tobias Moe Thorstensen
    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 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?

    • Selvin
      Selvin about 11 years
      you don't wana do this ... belive me ... what's you wanna achieve?
    • Tobias Moe Thorstensen
      Tobias Moe Thorstensen about 11 years
      I just want to animate a specific View in my ViewGroup. Why is this bad pratice? I'm just making a copy of the view and do operations on this copy.
    • Pragnani
      Pragnani about 11 years
      IFAIK, 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