如何用RealmRecyclerViewAdapter替换RecyclerView.Adapter

时间:2017-09-20 16:17:21

标签: android android-recyclerview realm

我有RecyclerView.Adapter使用八个不同的RealmObjects

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private String tag;
    private Realm realm;

    private RealmResults<HashtagObject> hashtags;
    private RealmResults<SearchtagObject> searchtags;
    etc.

   public SearchAdapter(Realm realm,final  String tag) {

        setHasStableIds(true);

        RealmChangeListener realmChangeListener = new RealmChangeListener() {
            @Override
            public void onChange(Object element) {
                 notifyDataSetChanged();
            }
        };

        this.realm=realm;
        this.tag=tag;
        this.hashtags = realm.where(HashtagObject.class).findAll();
        this.searchtags = realm.where(SearchtagObject.class).findAll();
        ...
        this.hashtags.addChangeListener(realmChangeListener);
        this.searchtags.addChangeListener(realmChangeListener);
        ...
   }
}

我正在尝试创建RealmRecyclerViewAdapter构造函数,但那里有OrderedRealmCollection。如何将OrderedRealmCollection用于我的计划?

第二个问题是notifyDataSetChanged()的问题。每次Realm数据库更改时,都会激活onBindViewHolder。但是我需要这种方法只有在用户与recyclerView交互时才能工作,而不是永久工作。以及notifyDataSetChanged()方法在RealmRecyclerViewAdapter中的工作原理。

0 个答案:

没有答案