2013-04-24 57 views
2

我已经定制了我的ListAdapter,并且我在1行中显示了3个不同的图像(项目)。 它完美的工作(根据它的功能)。 但是,无法顺利滚动ListView。如何优化Android ListView?

我在ImageViews上使用setBackgroundImage,我使用HashMap来缓存resourceId;所以我不必反复使用

resId=getContext().getResources().getIdentifier(resName, "drawable",appContext.getPackageName());

我想我错过了一些ListView不能很好的滚动。另外,如果我在平板电脑上尝试它,我的代码会自动填充超过3行的项目,平板电脑的列表视图几乎不可滚动。

我在这里做错了什么?

UPDATE:

我编程我的标志(国家的国旗)活动的onCreate方法创建的ListView:

root=(ViewGroup)this.findViewById(R.id.root);

listView=new ListView(this); 
    listView.setLayoutParams(new LayoutParams(
     LayoutParams.MATCH_PARENT, 
     LayoutParams.MATCH_PARENT)); 

    /* 
    ArrayList<Country> dataList=new ArrayList<Country>(){{ 
     add(new Country("jp")); 
     add(new Country("de")); 
     add(new Country("it")); 
    }};*/ 

    CountryListAdapter countryListAdapter=new CountryListAdapter(this); 


    countryListAdapter.setDataSource(allCountries); 


    listView.setAdapter(regionListAdapter); 
    listView.setBackgroundColor(0xFF000000); 
    listView.setDividerHeight(0); 

    root.addView(listView); 

+0

“不好”和“顺利”是什么意思? – 2013-04-24 19:56:28

+0

向我们展示至少为您创建ListView项目视图的代码。 – SimonSays 2013-04-24 19:57:40

+1

使用Traceview精确确定你在哪里花费时间。另外,激活'StrictMode'来对你的主应用程序线程显而易见的问题大喊大叫。 – CommonsWare 2013-04-24 20:09:21

回答

9

学习,学习,学习;-)

而且我是使用ViewHolder模式,为大量项目的布局相同的(即使它是一个simpliest,如单一的TextView的)

而且也在本ViewHolder实现示例/库

此外,如果你做你的ListView项目布局中使用的图像,你可以使用一些库异步下载图像,如:

3
  1. 使用静态ViewHolder模式

  2. 充气布局是昂贵的任务,从而避免充气布局尽可能
    更换

    LayoutInflater mInflater = LayoutInflater.from(context); 
    convertView = mInflater.inflate(R.layout.listview_item, null); 
    

    随着

    if (convertView == null) { 
    
        LayoutInflater mInflater = LayoutInflater.from(context); 
        convertView = mInflater.inflate(R.layout.listview_item, null); 
    } 
    
  3. 在单独的线程做图像的加载任务,以便getView只专注于查看图纸

  4. 您的活动主题添加<item name="android:windowNoTitle">true</item>

  5. ListView中

  6. add添加属性android:animationCache="false" property List android:scrollingCache="false" in ListView