sparkonhdfs
sparkonhdfs

Reputation: 1343

Wait until View is visible on Activity

I want to get Child Views from ListView, however, the method

listview.getChildAt(pos) returns null if the View is not actually visible on screen. Calling the method at the tail of

protected void onCreate(Bundle saved) also returns null because it seems that the background process for inflating the view is still running in the background. How can I check that the View is actually visible on screen before calling getchildAt(pos) . I have tried using a timer of 30ms, and it does return the right object at that time, but I do not want to use values hard-coded into the app.

Edit Code:

I am attempting to modify the background colors of individual items dynamically:

View view = list.getChildAt(list.getFirstVisiblePosition());

View.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));

Upvotes: 0

Views: 954

Answers (1)

Budius
Budius

Reputation: 39846

I am attempting to modify the background colors of individual items dynamically

the easiest way to accomplish that in a ListView is probably by extending/customizing your adapter.

for example:

list.setAdapter = new ArrayAdapter( /* do here all initialisation of the adapter */){

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
         v.setBackgroundColor( /* your color goes here */ );
    }
}

of course you can use that to any type of adapter, including if it's your own CustomAdapter, just put in there.

Upvotes: 3

Related Questions