使用GridView实现Palette时,我遇到滚动问题。
基本上,情况如下:GridView中的每个项目都有一个标题栏和一个加载的图像。使用调色板,标题栏应该更改为调色板提取的提取颜色。
但是,每当我向下滚动网格视图,然后向上滚动时,定位会随着条形背景颜色而变化。
以下是一个例子:
然后当我向下滚动并再次向上滚动时:
此外,着色似乎也不是正确的吗?出于某种原因,它看起来只是选择加载的最后一种颜色,有时它甚至根本无法加载,因为你可以看到条形图。
我在我的相册适配器中执行此操作,这是代码,希望有人可以指导我朝正确的方向发展。
public class AlbumAdapterNew extends ArrayAdapter<String> {
ArrayList<String> names;
Activity context;
ArrayList<String> coverPaths;
String coverPath;
Drawable img;
Bitmap bitmap;
ViewHolder mViewHolder = null;
ImageLoader imageLoader = ImageLoader.getInstance();
private RelativeLayout background;
static class ViewHolder {
private TextView text;
private ImageView image;
}
public AlbumAdapterNew(Activity context, ArrayList<String> names,
ArrayList<String> coverPaths) {
super(context, R.layout.albums_row, names);
this.names = names;
this.context = context;
this.coverPaths = coverPaths;
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.displayer(new FadeInBitmapDisplayer(500))
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config); // Do it on Application start
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
mViewHolder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.albums_row, parent, false);
mViewHolder.text = (TextView) convertView
.findViewById(R.id.albumTextView);
mViewHolder.image = (ImageView) convertView
.findViewById(R.id.album_photo);
background = (RelativeLayout) convertView
.findViewById(R.id.containerText);
convertView.setTag(mViewHolder);
}
else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.text.setText(names.get(position));
if (coverPaths.get(position) != null && !coverPaths.isEmpty()) {
mViewHolder.image.setScaleType(ScaleType.CENTER_CROP);
Glide.with(context).load("file:///" + coverPaths.get(position))
.asBitmap()
.into(new BitmapImageViewTarget(mViewHolder.image) {
@Override
protected void setResource(Bitmap resource) {
// Do bitmap magic here
Palette.from(resource).generate(
new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
Palette.Swatch vibrantSwatch = palette
.getVibrantSwatch();
if (vibrantSwatch != null) {
background
.setBackgroundColor(vibrantSwatch
.getRgb());
}
}
});
super.setResource(resource);
}
});
} else {
mViewHolder.image.setScaleType(ScaleType.CENTER_INSIDE);
imageLoader.displayImage("drawable://" + R.drawable.music_record,
mViewHolder.image);
}
return convertView;
}
}
答案 0 :(得分:0)
创建Palette
时,应将其保存到回调中的ViewHolder
。除了Palette
创建成本高昂之外,当您绑定Palette
中的数据时,这始终会使您的ViewHolder
实例与正在处理的网格项保持同步。
编辑:根据要求,举个例子。我没有配备SDK的计算机,所以这是来自内存,但它应该让你朝着正确的方向前进!
public class AlbumAdapterNew extends ArrayAdapter<String> {
ArrayList<String> names;
Activity context;
ArrayList<String> coverPaths;
String coverPath;
Drawable img;
Bitmap bitmap;
ImageLoader imageLoader = ImageLoader.getInstance();
static class ViewHolder {
private TextView text;
private ImageView image;
private Palette palette;
private RelativeLayout background;
}
public AlbumAdapterNew(Activity context, ArrayList<String> names,
ArrayList<String> coverPaths) {
super(context, R.layout.albums_row, names);
this.names = names;
this.context = context;
this.coverPaths = coverPaths;
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.displayer(new FadeInBitmapDisplayer(500))
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config); // Do it on Application start
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// This should be local so you don't get conflicts
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater vi = LayoutInflater.from(parent.getContext());
convertView = vi.inflate(R.layout.albums_row, parent, false);
viewHolder.text = (TextView) convertView
.findViewById(R.id.albumTextView);
viewHolder.image = (ImageView) convertView
.findViewById(R.id.album_photo);
viewHolder.background = (RelativeLayout) convertView
.findViewById(R.id.containerText);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(names.get(position));
if (coverPaths.get(position) != null && !coverPaths.isEmpty()) {
viewHolder.image.setScaleType(ScaleType.CENTER_CROP);
Glide.with(context)
.load("file:///" + coverPaths.get(position))
.asBitmap()
.into(new BitmapImageViewTarget(viewHolder.image) {
@Override
protected void setResource(Bitmap resource) {
// Do bitmap magic here
if(viewHolder.palette != null) {
setViewBackgroundColor(viewHolder)
} else {
Palette.from(resource).generate(
new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
viewHolder.palette = palette;
setViewBackgroundColor(vh);
}
});
}
super.setResource(resource);
}
});
} else {
mViewHolder.image.setScaleType(ScaleType.CENTER_INSIDE);
imageLoader.displayImage("drawable://" + R.drawable.music_record,
mViewHolder.image);
}
return convertView;
}
private void setViewBackgroundColor(ViewHolder vh) {
Palette.Swatch swatch = vh.palette.getVibrantSwatch();
if(swatch != null) {
vh.background.setBackgroundColor(swatch.getRgb());
}
}
}