megaturbo
megaturbo

Reputation: 677

When does the ListView items have been drawn to the screen

I want to know when the ListView layout is created. I need to perform an action on the item views, for that I need to know when the layout has been drawn to the screen. I need to get the IDs of the created views.

Is there a way to know when this is done ?

Edit (precision):

Upvotes: 0

Views: 253

Answers (2)

Sid
Sid

Reputation: 14896

In order to gett all the IDs from the view I've tried the following

private List<View> mChildren;

I've declared a List of View which will contain all the children and then I managed to get all the children with the following snippet

ViewTreeObserver obs = mListView.getViewTreeObserver();
obs.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        for(int index = 0; index<((ViewGroup)mListView).getChildCount(); ++index) {
            View nextChild = ((ViewGroup)mListView).getChildAt(index);
            if(!mChildren.contains(nextChild)){
                mChildren.add(nextChild); //New Child to add
            }

        }
    }
});

And then you can you mChildren which contain all the children views. Otherwhise you can also check onFinishInflate. Hope it helps :)

Update

As me and Thomas were discussing in chat, I came back to memory that also onViewAdded (from API 23) can be useful.

Upvotes: 1

Priyank Patel
Priyank Patel

Reputation: 12382

The adapter needs to create a layout for each row of the list. The ListView instance calls the getView() method on the adapter for each data element. In this method the adapter creates the row layout and maps the data to the views in the layout.

Within the getView() method you would inflate an XML based layout and then set the content of the individual views based on the Java object for this row. To inflate the XML layout file, you can use the LayoutInflator system service.

Below is the example adapter for Listveiw.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;

        public MySimpleArrayAdapter(Context context, String[] values) {
                super(context, -1, values);
                this.context = context;
                this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
                TextView textView = (TextView) rowView.findViewById(R.id.label);
                ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
                textView.setText(values[position]);
                // change the icon for Windows and iPhone
                String s = values[position];
                if (s.startsWith("iPhone")) {
                        imageView.setImageResource(R.drawable.no);
                } else {
                        imageView.setImageResource(R.drawable.ok);
                }

                return rowView;
        }
}

Below is rawlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout>

Upvotes: 1

Related Questions