1

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.

Atheryl
  • 297
  • 4
  • 10
  • Explain Clearly please – Engr Waseem Arain May 08 '14 at 03:48
  • 1
    dont place a ListView inside a ScrollView, why are you doing so? – pskink May 08 '14 at 03:51
  • @EngrWaseemArain hum ... the problem is quite simple in fact. I have only 4 elements visible while getView is called for every position, from 0 to 181. – Atheryl May 08 '14 at 03:54
  • @pskink For UI issue I don't have any other way to make it. Moreover I would like to find out what happen, it doesn't seems to be normal either. Also why not ? It works perfectly and no extra code to make it works. Unless there is a clear reason why my problem is related to this point. – Atheryl May 08 '14 at 03:55
  • @Atheryl it happens because the ScrollView will expand to fit all the content, and the ListView will expand to fit the ScrollView. – panini May 08 '14 at 04:09
  • @Atheryl see http://blog.mettletech.co/wp/blog/listview-inside-a-scrollview/ http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing http://kennethflynn.wordpress.com/2012/09/12/putting-android-listviews-in-scrollviews/ and many more... – pskink May 08 '14 at 04:14
  • @panini hummm I expected to avoid this by set the ListView size. Well ... kind of ... I will try something since the activity seems to know what elements are visible and then accept the evidence that I might need to change ... Thanks for the quick reply. – Atheryl May 08 '14 at 04:22
  • I fixed the issue by set the ListView to an arbitrary Height. – Atheryl May 08 '14 at 04:29

3 Answers3

1

I assume your aq.id(stuff) loads an image asyncronous.

If so, check your layout/list_view_item.xml it's height is probably set to wrap_content and the ImageView also has no fixed height. Meaning while loading the image this row has a zero or very small height. Therefore all 182 rows fit into the ListView at once and getView() gets called for every single one.

Set a fixed height or minHeight for every row or the ImageView the ListView will only call getView() for as many rows fit into the ListView.

Hope this helps

Su-Au Hwang
  • 4,131
  • 16
  • 23
  • Unfortunately my image in the cell layout is set to 60dp. Meanwhile the relativeLayout contain it is wrap_content by I will set fixed to give a try. The cell itself is 70dp whatever the content. – Atheryl May 08 '14 at 04:09
  • On a side note having a ScrollView or ListView have it's height set to `wrap_content` makes no sense, i don't think it matters in this case though, as android will automatically set it to `match_parent` when detected (don't take my word for it). – Su-Au Hwang May 08 '14 at 04:12
  • Actually I didn't put a lot of other part of the layout. I have several constraint to make it fill the screen and animate resizing some part depending on actions. Meanwhile I will rethink about redo this part since lot of people advice so. – Atheryl May 08 '14 at 04:15
  • Because of the animation and resizing the views all the rows re-inflated I think. Better choice you have decided. Check it after removing those parts. – Sripathi May 08 '14 at 05:58
  • Now it's working perfectly with the height set. @panini found out. Listview inside scrollview consider it can show infinity of elements which result on what you say (Same as if no height). Thanks for your help. – Atheryl May 10 '14 at 14:04
1

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" />
Atheryl
  • 297
  • 4
  • 10
0

If ListView or RecyclerView used in ScrollView, All items will initiate at the same time.