【发布时间】:2016-07-29 18:36:13
【问题描述】:
我正在查询 SQLite 数据库并将数据放入列表视图中。其中一个数据库行包含一个图像 Url 字段(也可以是一个 Uri)。
图像已按应有的方式加载,但当我滚动列表时,所有图像都开始闪烁,有些图像正在改变位置或显示在不同的位置。
我已经明白这种行为正在发生,因为列表视图在滚动时重用了行,但我不知道如何解决这种行为。我也不能在这个项目中使用像 Picasso 这样的外部库。
这是我的适配器代码:
public class FilmsListCustomAdapter extends CursorAdapter {
private LayoutInflater cursorInflater;
public FilmsListCustomAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView filmTitle = (TextView) view.findViewById(R.id.filmListTitle);
TextView filmScore = (TextView) view.findViewById(R.id.filmListScore);
ImageView filmImage = (ImageView)view.findViewById(R.id.filmListPoster);
ImageView filmSeen = (ImageView)view.findViewById(R.id.filmListSeen);
String title = cursor.getString( cursor.getColumnIndex("title") );
String score = cursor.getString(cursor.getColumnIndex("score"));
String url = cursor.getString( cursor.getColumnIndex("url") );
int seen = cursor.getInt( cursor.getColumnIndex("seen") );
if(Patterns.WEB_URL.matcher(url).matches()){
LoadImage loadImage = new LoadImage(context,filmImage);
loadImage.execute(url);
}
else{
Bitmap bmp = BitmapFactory.decodeFile(url);
CamImage camImage = new CamImage(context,Uri.parse(url));
Bitmap rotetedIm = camImage.rotateCamImage(bmp,url);
if(rotetedIm!=null){filmImage.setImageBitmap(Bitmap.createScaledBitmap(rotetedIm, 850, rotetedIm.getHeight(), false));}
else{filmImage.setImageResource(R.drawable.no_poster);}
}
GlobalMethods methods = new GlobalMethods(context);
filmTitle.setTypeface(methods.getWalkFont());
filmTitle.setText(title);
filmScore.setText(score);
if(seen==1){filmSeen.setImageResource(R.drawable.eye);}
else{filmSeen.setImageResource(0);}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return cursorInflater.inflate(R.layout.film_row, viewGroup, false);
}
}
【问题讨论】:
-
我不明白你为什么不想使用毕加索图书馆。使用 Picasso 或 Glide 库显示具有存储图像功能的图像,因此无需在列表视图中下载。这是 Picasso 的链接:square.github.io/picasso 和 Glide 的链接:github.com/bumptech/glide
-
不是我不想。这是一个学校项目,他们要求我不要使用外部库。
标签: android listview android-asynctask android-adapter