2

我正在使用ArrayAdapter我的ListView,在第一次一切都正确显示时,问题只是ListView当我滚动浏览它时它是滞后的。所以我找到了一个解决方案ViewHolder,最后我的 ListView 可以平滑滚动,但有个缺点,我的项目ListView混淆了。

我该如何解决?

这是我的getView()方法ArrayAdapter

public class VideoLocationAdapter extends ArrayAdapter<VideoLocationDB> {
        public ImageLoader imageLoader;
        ImageLoader loader = null;

        public VideoLocationAdapter(Context context, int resource,
                VideoLocationDB[] videoLocationDBs) {
            super(context, resource, videoLocationDBs);
            loader = new ImageLoader(context);
        }

        private LayoutInflater layoutInflator;

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {

            FeedViewHolder feedViewHolder;

            if (convertView == null) {

                convertView = LocationsListActivity.this.getLayoutInflater()
                        .inflate(R.layout.listitems, null, true);

                feedViewHolder = new FeedViewHolder();
                feedViewHolder.titleView = (TextView) convertView.findViewById(R.id.txt_title);
                feedViewHolder.descView = (TextView) convertView.findViewById(R.id.txt_list_desc);
                feedViewHolder.more = (TextView) convertView.findViewById(R.id.txt_more);
                feedViewHolder.distanceView = (TextView) convertView.findViewById(R.id.txt_distance);
                feedViewHolder.v  = (ImageView) convertView.findViewById(R.id.image);

                final VideoLocationDB vidLocation = videoLocationsDB[position];

                // Image
                String url = vidLocation.documentary_thumbnail_url;
                String name = vidLocation.name;
                feedViewHolder.v.setTag(url);

                loader.DisplayImage(url, LocationsListActivity.this, feedViewHolder.v, name);

                // Title        
                String title = vidLocation.name;
                feedViewHolder.titleView.setText(title.toUpperCase());
                Typeface fontRegular = Typeface.createFromAsset(getAssets(),
                        "miso.otf");
                feedViewHolder.titleView.setTypeface(fontRegular);

                // Description          
                String desc = vidLocation.text;
                feedViewHolder.descView.setText(desc);
                Typeface fontLight = Typeface.createFromAsset(getAssets(),
                        "miso-light.otf");
                feedViewHolder.descView.setTypeface(fontLight);

                // More     
                feedViewHolder.more.setText(getString(R.string.de_list_more));
                feedViewHolder.more.setTypeface(fontLight);

                // Distance
                feedViewHolder.distanceView.setTypeface(fontRegular);

                if (location != null) {
                    double lat = location.getLatitude();
                    double lng = location.getLongitude();

                    double lat2 = roundDown(vidLocation.latitude);
                    double lng2 = roundDown(vidLocation.longitude);

                    if (countDistance(lat, lng, lat2, lng2) >= 500) {
                        double kilometer = countDistance(lat, lng, lat2, lng2) / 1000;
                        int decimalPlaces = 1;
                        BigDecimal decimal = new BigDecimal(kilometer);
                        decimal = decimal.setScale(decimalPlaces,
                                BigDecimal.ROUND_HALF_UP);
                        double new_km = decimal.doubleValue();
                        feedViewHolder.distanceView.setText(new_km + " km");
                    } else {
                        int decimalPlaces = 1;
                        BigDecimal decimal = new BigDecimal(countDistance(lat, lng,
                                lat2, lng2));
                        decimal = decimal.setScale(decimalPlaces,
                                BigDecimal.ROUND_HALF_UP);
                        double meter = decimal.doubleValue();
                        feedViewHolder.distanceView.setText(meter + " m");
                    }
                    videoLocationAdapter.notifyDataSetChanged();
                }

                convertView.setTag(feedViewHolder);

            } else
            {
                feedViewHolder = (FeedViewHolder) convertView.getTag();
            }
            return convertView; 
            }

static class FeedViewHolder{

        TextView titleView;
        TextView descView;
        TextView more;
        TextView distanceView;
        ImageView v;
    }
4

1 回答 1

3

你已经在 if (convertView == null) {} 和 else 之后设置了文本

if (convertView == null) { //创建视图 }else { //从 convertView 获取视图 }

这里设置视图中的文本

http://android.amberfog.com/?p=296

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("getView " + position + " " + convertView);
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item1, null);
                holder = new ViewHolder();
                holder.textView = (TextView)convertView.findViewById(R.id.text);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }

         holder.textView.setText(mData.get(position));//<========== see it .....
            return convertView;
        }

    }

将此代码放在 if/else 之后

 final VideoLocationDB vidLocation = videoLocationsDB[position];

                // Image
                String url = vidLocation.documentary_thumbnail_url;
                String name = vidLocation.name;
                feedViewHolder.v.setTag(url);

                loader.DisplayImage(url, LocationsListActivity.this, feedViewHolder.v, name);

                // Title        
                String title = vidLocation.name;
                feedViewHolder.titleView.setText(title.toUpperCase());
                Typeface fontRegular = Typeface.createFromAsset(getAssets(),
                        "miso.otf");
                feedViewHolder.titleView.setTypeface(fontRegular);

                // Description          
                String desc = vidLocation.text;
                feedViewHolder.descView.setText(desc);
                Typeface fontLight = Typeface.createFromAsset(getAssets(),
                        "miso-light.otf");
                feedViewHolder.descView.setTypeface(fontLight);

                // More     
                feedViewHolder.more.setText(getString(R.string.de_list_more));
                feedViewHolder.more.setTypeface(fontLight);

                // Distance
                feedViewHolder.distanceView.setTypeface(fontRegular);

                if (location != null) {
                    double lat = location.getLatitude();
                    double lng = location.getLongitude();

                    double lat2 = roundDown(vidLocation.latitude);
                    double lng2 = roundDown(vidLocation.longitude);

                    if (countDistance(lat, lng, lat2, lng2) >= 500) {
                        double kilometer = countDistance(lat, lng, lat2, lng2) / 1000;
                        int decimalPlaces = 1;
                        BigDecimal decimal = new BigDecimal(kilometer);
                        decimal = decimal.setScale(decimalPlaces,
                                BigDecimal.ROUND_HALF_UP);
                        double new_km = decimal.doubleValue();
                        feedViewHolder.distanceView.setText(new_km + " km");
                    } else {
                        int decimalPlaces = 1;
                        BigDecimal decimal = new BigDecimal(countDistance(lat, lng,
                                lat2, lng2));
                        decimal = decimal.setScale(decimalPlaces,
                                BigDecimal.ROUND_HALF_UP);
                        double meter = decimal.doubleValue();
                        feedViewHolder.distanceView.setText(meter + " m");
                    }
                    videoLocationAdapter.notifyDataSetChanged();
                } 
于 2012-05-30T13:27:51.283 回答