Search code examples
androidperformanceandroid-listviewandroid-scrollviewandroid-query

Listview create all elements instead of visible ones


I have an issue which I didn't found any solutions on stackoverflow (or anywhere else so far).

Summary :

I have scrollview and inside of it a ListView with visibility gone (Can switch with a TextView).

When I set the adapter content, in my activity the onScroll get call with 4 visibles Item but the getView of the adapter get call for the whole dataSet (181 items) which bring a lot of performance issues / imageref_ashmem create failed ... (Of course remove image loading remove fail of creation but well :D)

=== Activity ===

Result of Activity in onScroll

firstVisibleItem    0   
visibleItemCount    4   
totalItemCount  181

=== Layout extract ===

<ScrollView 
    android:id="@+id/UserProfileView_ScrollView"       
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:background="#00FFFFFF" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF" >

        <RelativeLayout 
            android:id="@+moreDetails/UserProfileView_BottomLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+profile/UserProfileView_MoreDetails"
            android:background="#FFFFFFFF" >

            <TextView
                android:id="@+id/UserProfileView_About"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ff111111"
                android:visibility="gone" />

            <ListView
                android:id="@+id/UserProfileView_MoreDetails_ListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:footerDividersEnabled="false"
                android:divider="@color/unactivatedLightGray"
                android:dividerHeight="1dp"
                android:listSelector="@android:color/transparent"
                android:visibility="gone" />

        </RelativeLayout>

    </RelativeLayout>

</ScrollView>

The height of the BottomLayout is set to 3/4 of the width of the screen programmatically.

=== Adapter ===

public class CommonAdapter extends BaseAdapter {

public List<Common>     list;
private LayoutInflater      inflater;
private AQuery              aq;

public CommonAdapter(Context context, List<Common> list) {
    this.inflater = LayoutInflater.from(context);
    this.list = list;
    this.aq = new AQuery(context);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

private class ViewHolder {
    ImageView       thumbImg;
    TextView            name;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_view_item, null);
        holder.thumbImg     = (ImageView) convertView.findViewById(R.id.CommonCell_Thumb);
        holder.name = (TextView) convertView.findViewById(R.id.CommonCell_Name);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Common obj = list.get(position);

    if (list.get(position).getPicture() != null)
        aq.id(holder.thumbImg).image(obj.getPicture());
    else
        aq.id(holder.thumbImg).image(Util.Thumb(list.get(position).getId()), options);
    holder.name.setText(obj.getName());

    return convertView;
}
}

EDIT : Every time I scroll, the whole dataset it parse again, so 181 call to getView.


Solution

  • I fixed the issue by set the ListView to an arbitrary Height.

    <ListView
        android:id="@+id/UserProfileView_MoreDetails_ListView"
        android:layout_width="match_parent"
        android:layout_height="1080dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:footerDividersEnabled="false"
        android:divider="@color/unactivatedLightGray"
        android:dividerHeight="1dp"
        android:listSelector="@android:color/transparent"
        android:visibility="gone" />