开发者

Android ListView Laggy and Child Views Shrinking

I am programatically building a ListView that does FILL_PARENT for width, and WRAP_CONTENT for height, then centering it in its parent view.

The ListView is semi-transparent so the activity behind it is visible, so I use:

list.setCacheColorHint(0);

list.setBackgroundColor(Color.argb(200, 0, 0, 0));

Other than that, it is a completely standard out-of-the-box ListView.

Two issues:

A) The ListView is extremely laggy when scrolling (even with only a few children). Why, and how can I optimize it?

B) Whenever the list is scrolled, some of the child views contained in the ListView are shrinking and expanding randomly and it looks awful. Sometimes they get stuck being small. I have no idea why it is doing this -- I'm not doing anything special outside of populating a normal ListView.

The ListView uses this adapter:

class ActionListAdapter extends BaseAdapter
    {
        private final ArrayList<MapActionListViewItem> _items = new ArrayList<MapActionListViewItem>();
        @Override public final MapActionListViewItem getItem(int position)
        {
            return _items.get(position);
        }
        @Override public final long getItemId(int position)
        {
            return position;
        }
        @Override public final int getCount()
        {
            return _items.size();
        }
        @Override public final View getView(int position, View convertView, ViewGroup parent)
        {
            return _items.get(position).Row;
        }
        public final void clear()
        {
            _items.clear();
            notifyDataSetChanged();
        }
        public final void addItem(MapActionListViewItem item)
        {
            _items.add(item);
            notifyDataSetChanged();
        }
        public final void removeItem(MapActionListViewItem item)
        {
            _items.remove(item);
            notifyDataSetChanged();
        }
        public final void refresh()
        {
            final Iterator<MapActionListViewItem> i = _items.iterator();
            while (i.hasNext())
                if (!i.next().refresh())
                    i.remove();
        }
    }

The MapActionListViewItem object is a container class that updates the child views:

public abstract class MapActionListViewItem
{
    public final ImageView      Icon;
    public final TextView       Title;
    public final TextView       Subtitle;
    public final TextView       Info;
    public final View           Row;
    public final int            MapAction;
    protected final GlobalState State;
    public MapActionListViewItem(GlobalState state, int mapActionType)
    {
        MapAction = mapActionType;
        State = state;
        Row = state.Inflater.inflate(R.layout.actionlistviewitem, null);
        Icon = (ImageView) Row.findViewById(R.id.actionitem_image);
        Title = (TextView) Row.findViewById(R.id.actionitem_title);
        Subtitle = (TextView) Row.findViewById(R.id.actionitem_subtitle);
        Info = (TextView) Row.findViewById(R.id.actionitem_info);
    }
    public abstract boolean refresh();
}

I've disabled refreshing for troubleshooting my issues, so that isn't relevant. Each child Row contained in the ListView is inflated from actionlistviewitem.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:orientation="vertical"
    android:layout_width="wrap_content">
    <LinearLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <ImageView android:id="@+id/actionitem_image"
            android:scaleType="center" android:layout_height="25dp"
            android:layout_width="25dp" />
        <TextView android:id="@+id/actionitem_title"
            android:textColor="@color/white" android:text="TITLE"
            android:textSize="14dp" android:layout_height="wrap_content"
            android:layout_gravity="center" android:layout_width="wrap_content" />
    </LinearLayout>
    <TextView android:layout_height="wrap_content" android:id="@+id/actionitem_subtitle"
        android:text="Sub Title" android:textSize="12dp"
        android:layout_marginLeft="25dp" android:layout_width="wrap_content" />
    <TextView android:layout_height="w开发者_如何转开发rap_content"
        android:textSize="10dp" android:text="Information"
        android:layout_marginLeft="25dp" android:layout_width="wrap_content"
        android:id="@+id/actionitem_info" />
</LinearLayout>

I've been banging my head against this for days with no success. No matter what I try the Child views from the above XML shrink randomly while scrolling in a very ugly and unexpected way, and is nearly useless with scroll lag on my DroidX. Any help would be greatly appreciated.

(Developing in Eclipse 3.5.2, targeting SDK 8 - 2.2)


Inflate & findViewById is quite expensive on ListView, you might want to view this video presentation first to know how ListView works and provide efficient coding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜