0

我正在使用自定义适配器来显示 ListView,但我的 ListView 表现得非常生涩

我的自定义适配器的代码

   class ListAdapter extends BaseAdapter
    {
    Context context;
    ArrayList<T> arr ;

    public ListAdapter(Context con,ArrayList<T> arrayList) {
    arr = arrayList;
    context = con;
    }

    @Override
    public int getCount() {
    return arr.size();
    }
    @Override
            public Object getItem(int arg0) {
                return arg0;
            }
    @Override
            public long getItemId(int arg0) {
                return arg0;
            }

@Override
        public View getView(int pos, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null ){
                viewHolder = new ViewHolder();
                convertView = View.inflate( context,R.layout.match_introspection_list_item, null);
                viewHolder.teamFlagOneImageView = (ImageView) convertView.findViewById(R.id.imageView1);
                viewHolder.teamFlagTwoImageView = (ImageView) convertView.findViewById(R.id.imageView3);
                viewHolder.teamOneTextView =(TextView) convertView.findViewById(R.id.textView1);
                viewHolder.teamTwoTextView =(TextView) convertView.findViewById(R.id.textView4);
                viewHolder.dateTextView =(TextView) convertView.findViewById(R.id.textView2);
                viewHolder.placeTextView =(TextView) convertView.findViewById(R.id.textView3);
                viewHolder.descTextView = (TextView) convertView.findViewById(R.id.desc);
                convertView.setTag(viewHolder);
            }else{
                viewHolder =(ViewHolder) convertView.getTag();
            }

            final MatchIntrospectionObjects obj = arr.get(pos);
            viewHolder.teamOneTextView.setText(obj.home_team_abbrev);
            viewHolder.teamTwoTextView.setText(obj.away_team_abbrev);
            viewHolder.descTextView.setText(Html.fromHtml("<b>Description: </b>")+obj.matchdescription);
            if(obj.date.equals("")){}else
                viewHolder.dateTextView.setText(splitDate(obj.date));

            viewHolder.placeTextView.setText(obj.venue);

            imageLoader.displayImage(FLAG_BASE_URL+obj.home_flag, viewHolder.teamFlagOneImageView, options, animateFirstListener);
            imageLoader.displayImage(FLAG_BASE_URL+obj.away_flag, viewHolder.teamFlagTwoImageView, options, animateFirstListener);

            return convertView;
        }
}

这是 ListAdpater 的我的代码

我在 Oncreate() 方法中像这样设置 ListAdapter

ListAdapter adapter =new ListAdapter(this,listArray);
            mListView.setAdapter(adapter);

这段代码帮助我在我的 ListView 中最小化某种程度的生涩效应。

我正在使用通用图像加载器从 URL 获取图像如果相同的图像想要从相同的 URL 获取不同的单元格,那么 Universal ImageLoader 会再次获取并再次获取,或者它在生命周期中获取一次,然后从缓存中获取可用的图像?

4

2 回答 2

1

@Sam 推荐的视频的症结在于,每次滚动时您都在夸大您的视图,而不是使用recycler内置的ListView. Array您正在尝试通过使用of来加快速度RelativeLayout,但更好的方法是ViewHolder为您的视图创建一个以便可以重用它。这ViewHolder是一个静态内部类。像这样定义它:

public static class ViewHolder {
    public TextView currentRatingText;
    public TextView countryName;
    public GradientTextView rating;
    public TextView rank;
    ...

}

然后在自定义适配器的构造函数中定义充气器,如下所示:rowInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

然后,您的getView()方法应该看起来像这样。

public View getView(int pos, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
    viewHolder = new ViewHolder;
    convertView = rowInflater.inflate(context,R.layout.team_ranking_list_item, null);
    viewHolder.currentRatingText = (TextView) convertView.findViewById(R.id.Rating);
    viewHolder.countryName = (TextView) convertView.findViewById(R.id.TeamNameTextView);
    viewHolder.rating = (GradientTextView) convertView.findViewById(R.id.RatingTextView);
    viewHolder.rank =(TextView) convertView.findViewById(R.id.RankTextView);
    ...
    //Views can hold a tag, which is an object. By setting the viewHolder as the
    //view tag, we can grab it next time so we don't need to re-inflate everything.
    convertView.setTag(viewHolder);
} else {
    viewHolder = (ViewHolder)convertView.getTag();
}
//Here is where you set all of you views' data.
T t = arr.get(pos);
viewHolder.currentRatingText.setText(t.getText());
viewHolder.countryName.setText("UAE");
...
return convertView;

}

ConvertView是离开屏幕的视图,现在可以重新用于屏幕底部的新视图。如上所述,缓存图像肯定会有所帮助,但我认为这足以让列表平滑滚动。

该视频仍然是强制观看的;罗曼盖伊是老板。

于 2013-03-25T20:30:09.370 回答
1
public View getView(int pos, View convertView, ViewGroup parent) { 
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder;
convertView = rowInflater.inflate(context,R.layout.team_ranking_list_item, null);
viewHolder.currentRatingText = (TextView) convertView.findViewById(R.id.Rating);
viewHolder.countryName = (TextView) convertView.findViewById(R.id.TeamNameTextView);
viewHolder.rating = (GradientTextView) convertView.findViewById(R.id.RatingTextView);
viewHolder.rank =(TextView) convertView.findViewById(R.id.RankTextView);
convertView.setTag(viewHolder);
} else 
 {
viewHolder = (ViewHolder)convertView.getTag();
}
//set data here

return convertView;
}


 static class ViewHolder {
 TextView currentRatingText;
 TextView countryName;
 GradientTextView rating;
 TextView rank;
}

使用视图支架来实现平滑滚动和性能。如果您每次都显示 100 张图像而不是 findviewById,则在您的视图为空并且您设置数据时执行此操作。回收竞争。

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

您也可以使用惰性列表。https://github.com/thest1/LazyList

要显示大量图像,您可以使用 Universal Image Loader。(为您提供比惰性列表更多的配置选项)

https://github.com/nostra13/Android-Universal-Image-Loader。它基于惰性列表。您可以显示来自 sdcard、手机内存或服务器的图像。如果下载失败,您可以显示错误图像。可以显示带圆角的图像。可以缓存在磁盘或内存上。可以压缩图像。

在您的自定义适配器构造函数中

   File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

  // Get singletone instance of ImageLoader
  imageLoader = ImageLoader.getInstance();
  // Create configuration for ImageLoader (all options are optional)
  ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
      // You can pass your own memory cache implementation
     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
     .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
     .enableLogging()
     .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
 .showStubImage(R.drawable.stub_id)//display stub image
 .cacheInMemory()
 .cacheOnDisc()
 .displayer(new RoundedBitmapDisplayer(20))
 .build();

在你的 getview()

   ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
   imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options
于 2013-03-25T20:46:29.810 回答