我正在使用此帖子中的RealmRecyclerView:https://realm.io/news/android-realm-listview/ 在此ToDo应用程序中,当刷新项目时,它会自动从RecyclerView和Realm Database中删除。 我想在删除项目时收到通知,还要删除哪个项目,以便我可以对该项目执行某些操作。 我尝试使用Realm Change Listener,但每次提交Realm Transaction时都会调用它。因此,即使添加了新项目,也会调用它。 我该怎么做呢?是否可以使用普通的RecyclerView?
答案 0 :(得分:1)
此时(版本V1.1.0),Realm在删除RealmObject时不提供回调。对于所有类型的数据/视图(listView,RecyclerView,RealmObject,RealmResults等)都是如此,如果您将数据查询为可观察或使用更改侦听器,也是如此。
然而,当从Realm中删除查询对象时发送一个空对象确实有意义,但由于它在Realm中发生了重大变化,我们将不得不等待V2.0。
答案 1 :(得分:1)
According to the answer, they have not yet added this feature. But if you want to achieve this, you can use normal ReacyclerView using the existing RealmAdapter and everything as it is.
Here's how to do it:-
Remove the RealmRecyclerView and add the normal RecyclerView:-
1. Add the normal RecyclerView from the support library.
2. Initialize the recyclerview with the existing adapter i.e the adapter class that extends RealmBasedRecyclerViewAdapter, no need to make a new adapter
recyclerView=(RecyclerView)findViewById(R.id.realm_recyeler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new FilterAdapter(this,results,true,true);
recyclerView.setAdapter(adapter);
3. Next, we will use the ItemTouchHelper class to implement the swipe to dismiss for the RecyclerView :-
ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.remove(viewHolder.getAdapterPosition(),alarmintent);
}
};
I've made a method in my adapter to remove item(shown below), you can do it here as well
The viewHolder.getAdapterPositon() gives the position of item swiped, it is passed to delete the RealmObject from Realm DB at given position(shown below)
0 -> drag flag - since I am not implement drag to move items, I've kept it as zero
ItemTouchHelper.RIGHT - swipe flags - These say in which direction the swipe to dismiss is set
ItemTouchHelper.RIGHT - swipe in right direction to dismiss
ItemTouchHelper.LEFT - swipe in left direction to dismiss
To support both directions, pass- ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(recyclerView);
Create a new ItemTouchHelper object with above callback
Attach the ItemTouchHelper to RecyclerView
4. Here's how to remove the item (below is the code of my remove method of adapter):-
public void remove(int position)
{
RealmConfiguration configuration = new RealmConfiguration.Builder(context).deleteRealmIfMigrationNeeded().build();
realm = Realm.getInstance(configuration);
realm.beginTransaction();
realmResults.deleteFromRealm(position);
realm.commitTransaction();
notifyItemRemoved(position);
}
deleteFromRealm method is used to delete item at given position
call the notifyItemRemoved(position) to indicate item is removed at specified position
5. That's it, very easy and no need to create new adapters etc.
答案 2 :(得分:0)
虽然Realm可能无法在删除项目时提供回调,但有一种方法可以在使用this post中的RealmRecyclerView
时知道项目何时被删除。
适配器(RealmBasedRecyclerViewAdapter
)只要刷一个项目以解雇,就会调用onItemSwipedDismiss(int position)
。在此适配器的子类中,您可以覆盖此方法以添加一些额外的逻辑。
例如,在我的Recycler View中,我想为用户提供撤消删除的选项。因此,我重写onItemSwipedDismiss(int position)
并访问要删除的对象的字段。 (就我而言,这个对象相当小 - 只有三个字段 - 所以这不是太笨重)。然后我调用超级方法:super.onItemSwipedDismiss(position);
,它将为删除设置动画并将其从Realm中删除。
然后,我创建一个Snackbar,其中包含一个从已保存字段重新创建Realm对象的操作。一旦创建,它会立即返回到回收站视图。
以下是此方法覆盖的实现框架:
@Override
public void onItemSwipedDismiss(int position) {
// Gather the object's fields, if you want:
YourObject objectToDelete = realmResults.get(position);
final String title = objectToDelete.getTitle();
final long timestamp = objectToDelete.timestamp;
// Perform delete and animation:
super.onItemSwipedDismiss(position);
// Add code here depending on what you want to do
// (for example, you could add a Snackbar that undoes
// the deletion by "resurrecting" your deleted object)
}