由于计算位置之间的距离,滚动ListView滞后

时间:2012-05-31 10:01:15

标签: android

我有ListView,其每个条目都显示一些信息,包括用户当前位置与感兴趣的地点之间的距离。

不幸的是,因为我计算getView()内的距离(通过调用距离计算方法并返回字符串来设置TextView)滚动列表有点滞后。

任何人都可以给我一个解决方案,这样我就能在我的ListView上滚动更顺畅吗?

这是我的getView()方法:

@Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            FeedViewHolder feedViewHolder = null;
            final VideoLocationDB vidLocation = videoLocationsDB[position];
            String url = vidLocation.documentary_thumbnail_url;
            String name = vidLocation.name;
            String title = vidLocation.name;
            String desc = vidLocation.text;
            double lat = vidLocation.latitude;
            double lng = vidLocation.longitude;
            String distance = calculateDistance(lat,lng);


            if (convertView == null) {

                convertView = layoutInflater.inflate(R.layout.listitems, parent,false);
                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);

                Typeface fontRegular = Typeface.createFromAsset(getAssets(),
                        "miso.otf");
                feedViewHolder.titleView.setTypeface(fontRegular);
                Typeface fontLight = Typeface.createFromAsset(getAssets(),
                        "miso-light.otf");
                feedViewHolder.descView.setTypeface(fontLight);
                feedViewHolder.more.setTypeface(fontLight);
                feedViewHolder.distanceView.setTypeface(fontRegular);

                convertView.setTag(feedViewHolder);

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


            feedViewHolder.v.setTag(url);
            loader.DisplayImage(url, LocationsListActivity.this, feedViewHolder.v, name);
            feedViewHolder.titleView.setText(title.toUpperCase());
            feedViewHolder.descView.setText(desc);  
            feedViewHolder.more.setText(getString(R.string.de_list_more));
            feedViewHolder.distanceView.setText(distance);

            return convertView; 
        }

以下是距离计算方法:

public double countDistance(double lat1, double lng1, double lat2,
            double lng2) {
        Location locationUser = new Location("point A");
        Location locationPlace = new Location("point B");
        locationUser.setLatitude(lat1);
        locationUser.setLongitude(lng1);
        locationPlace.setLatitude(lat2);
        locationPlace.setLongitude(lng2);

        double distance = locationUser.distanceTo(locationPlace);

        return distance;
    }

    public String calculateDistance(double lat2, double lng2){

        String distance = null;

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

            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();
                distance = String.valueOf(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();
                distance = String.valueOf(meter + " m");
            }
            videoLocationAdapter.notifyDataSetChanged();
        }
        return distance;
    }   

1 个答案:

答案 0 :(得分:1)

Greendroid(https://github.com/cyrilmottier/GreenDroid)正在使用Future(http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html)来在列表视图中从Web加载异步映像。即使列表包含数百个条目,它也非常高效。所以我认为这是你正在寻找的东西。期货由处理池的执行程序管理,以便一切都在硬件限制内完成。

有些代码吗? Greendroid,这里有一个有趣的例子,你可以在这里找到更多信息。它在我的书签中,但我不知道它是否是最新的: http://blog.tomgibara.com/post/7665158012/android-adapter-view-rendering