我正在通过RecyclerView
填充sqlite database
来自AsyncTaskLoader
的最多数千个条目。该列表需要可过滤,因此我需要一次加载所有条目。 RecyclerView的ViewHolders中的一些TextViews
包含进一步数据库查询的结果(基于初始结果)或其他类型的计算。
扩展初始查询以包含JOIN(至少会使辅助查询过时)确实会降低加载速度。然而,在RecyclerView的onBind方法中进行查询/计算,使得投入比没有它们时更加流畅。
有没有办法延迟加载这些计算出的TextViews
的内容?我搜索了一些例子,但像“毕加索”这样的图书馆"和" Glide"只允许加载图片...
任何帮助表示赞赏!
谢谢, 罗布
答案 0 :(得分:0)
我最后使用callMessage(isDirty) {
// Your conditional message with respect to "isDirty" parameter
isDirty ? this.myMessage = 'Dirty Message' : false;
return isDirty;
}
类使用Singleton
在单独的线程中进行加载/计算,然后让ExecutorService
更新我的Handler
。不知道,如果这是最好的方法,但到目前为止它没有任何问题。
ViewHolder
在我private static class LazyLoadManager {
private static LazyLoadManager INSTANCE;
private final ExecutorService pool;
private Map<ViewHolder, String> viewHolders = Collections.synchronizedMap(new WeakHashMap<ViewHolder, String>());
private Context context;
private LazyLoadManager(Context context) {
this.context = context;
pool = Executors.newFixedThreadPool(5);
}
public static LazyLoadManager getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new LazyLoadManager(context.getApplicationContext());
}
return INSTANCE;
}
private void loadData(final ViewHolder viewHolder, final Model myModel) {
// Put ViewHolder and respective tag into Map
viewHolders.put(viewHolder, myModel.getTag());
final Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
// Get result object
Model myResultModel = (Model) msg.obj;
// Result was initialized
if (myResultModel != null) {
// Get ViewHolder tag from map
String tag = viewHolders.get(viewHolder);
// Set ViewHolder content, if saved tag matches tag of this ViewHolder
if (tag != null && tag.equals(viewHolder.getTag())) {
viewHolder.viewA.setText(myResultModel.getThis());
viewHolder.viewB.setText(myResultModel.getThat());
}
}
return true;
}
});
pool.submit(new Runnable() {
@Override
public void run() {
Model myResultModel = null;
if (myModel != null) {
// do required calculations and secondary queries based on myModel
myResultModel.setThis(result1);
myResultModel.setThat(result2);
}
Message message = Message.obtain();
message.obj = myResultModel;
handler.sendMessage(message);
}
}
);
}
的{{1}}方法中,我就像这样调用我的LazyLoadManager
onBindViewHolder