我正在使用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;
}