- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个RealmRecyclerViewAdapter,它可以在领域中侦听查询,并且已经使用ItemTouchHelper在其上实现了拖放功能。
private final ItemTouchHelper.Callback _ithCallback = new ItemTouchHelper.Callback() {
private int fromPosition;
private int toPosition;
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
if (viewHolder.getItemViewType() != target.getItemViewType()) {
return false;
}
toPosition = target.getAdapterPosition();
adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder.getItemViewType() == TaskListRecyclerViewAdapter.FOOTER) {
return 0;
} else {
return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
}
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
fromPosition = viewHolder.getAdapterPosition();
toPosition = viewHolder.getAdapterPosition();
} else if (actionState == ItemTouchHelper.ACTION_STATE_IDLE
&& fromPosition != toPosition) {
// adapter.onDetachedFromRecyclerView(mRecyclerView);
dragAndDropManager.executeDragAndDrop(liveRealm, store.getStoreUid(), fromPosition, toPosition);
...
...
// adapter.onAttachedToRecyclerView(mRecyclerView);
}
}
};
adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
dragAndDropManager.executeDragAndDrop(liveRealm, store.getStoreUid(), fromPosition, toPosition);
adapter.onDetachedFromRecyclerView(mRecyclerView);
adapter.onAttachedToRecyclerView(mRecyclerView);
最佳答案
迈克尔-您是否满意过这项工作?
我只是碰到你的帖子,遇到同样的问题。我正在使用Realm的本地Java DB版本,并查看了几个示例,这些示例显示了使用stock recyclerView实现拖放的类似技术(通常它们都使用字符串数组作为数据模型,而不是像实时DB例如Realm),这是我的经验-我认为“标准”技术存在问题以及如何解决这些问题:
要解决的问题
问题1:自定义回调类转发大量的onMove事件
我发现的各种示例(例如here这样的示例)总是利用
扩展ItemTouchHelper.Callback
的自定义类。此类通常定义
依次由适配器实现的接口。然后此类覆盖
从回调上调用onMove()/ onSwiped来调用适配器实现的接口的onViewMoved()/onViewSwiped()
方法:
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
contract.onViewMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
onViewMoved()
实现中,示例通常会更新数据源,然后调用
notifyItemMoved()
:
public void onViewMoved(int oldPosition, int newPosition) {
User targetUser = usersList.get(oldPosition);
User user = new User(targetUser);
usersList.remove(oldPosition);
usersList.add(newPosition, user);
notifyItemMoved(oldPosition, newPosition);
}
onMove()
被调用很多。
public class RecyclerViewSwipeAndDragHelper extends ItemTouchHelper.Callback {
private SwipeAndDraggable implementor;
private static final int POSITION_UNKNOWN = -1;
private int oldPosition = POSITION_UNKNOWN;
private int newPosition = POSITION_UNKNOWN;
public interface SwipeAndDraggable {
void onViewMoved(int oldPosition, int newPosition);
void onViewSwiped(int position);
void onClearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder);
}
onMove()
回调整合到对适配器的单个调用中。幸运的是,ItemTouchHelper已经为我们完成了许多此类工作。从
onMove()
返回的TRUE值将触发对
onMoved()
的后续调用。返回FALSE不会。因此,在我的
Callback
实现中,当我覆盖
onMove()
时,我不会调用该接口的实现者。相反,我只跟踪我是否已拖动相同的目标位置,因此它仅在我第一次命中新目标时才返回TRUE(而且我还通过测试注意到有时我会得到初始-1
viewHolder.getAdapterPosition()
的值,因此我也对此进行了测试):
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
oldPosition = viewHolder.getAdapterPosition();
if ( oldPosition > POSITION_UNKNOWN) {
if ( newPosition != target.getAdapterPosition() ) {
newPosition = target.getAdapterPosition();
return true;
}
}
return false;
}
onMove()
返回TRUE时,随后的回调中的
onMoved()
才会被调用。我在该方法中调用onViewMoved()的实现:
@Override
public void onMoved(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, int fromPos, @NonNull RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
implementor.onViewMoved(fromPos, toPos);
}
onMove()
回调整合到一个更新中。现在,当用户将鼠标拖到各个查看器上时,我只接到一次电话。
holder.getMediaImageView().setOnTouchListener((View view, MotionEvent motionEvent) -> {
if (motionEvent.getAction()== MotionEvent.ACTION_DOWN) {
// Turn off the data change listener within the RealmRecyclerViewAdapter superclass, so that
// when we make changes via the drag it doesn't automatically update - we will handle that manually
onDetachedFromRecyclerView(mMediaRecyclerView);
mItemTouchHelper.startDrag(holder);
}
return false;
});
public void onViewMoved(int oldPosition, int newPosition) {
Timber.d("Recognized a movement to a new position: old position, new position is: %d, %d", oldPosition, newPosition);
MediaDataMgr.get().moveMediaInPlaylist(oldPosition, newPosition, mPlaylist.getId());
// RealmRecyclerViewAdapter is no longer automatically notified when underlying data changes, so need to make notify()* calls here
notifyItemMoved(oldPosition, newPosition);
}
public void moveMediaInPlaylist(int from, int to, String playlistId) {
boolean success = false;
boolean mainThread = Thread.currentThread().equals(Looper.getMainLooper().getThread());
Realm realm = null;
if (mainThread) {
realm = mUIThreadRealm;
} else {
realm = Realm.getDefaultInstance();
}
try {
Playlist p = getPlaylist(playlistId);
if ( p== null) {
throw new Exception("Error retrieving playlist with ID: "+ playlistId );
}
realm.beginTransaction();
p.getMediaList().move(from, to);
success = true;
} catch (Exception e) {
Timber.d( "Exception deleting a Media in Playlist: %s", e.getMessage());
success = false;
} finally {
if ( success ) {
realm.commitTransaction();
} else {
realm.cancelTransaction();
}
if (!mainThread) {
realm.close();
}
}
}
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
oldPosition = POSITION_UNKNOWN;
newPosition = POSITION_UNKNOWN;
implementor.onClearView(recyclerView, viewHolder);
}
public void onClearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
// reset RealmRecyclerViewAdapter data listener
onAttachedToRecyclerView(mMediaRecyclerView);
}
关于java - 使用RealmRecyclerViewAdapter和Drag&Drop进行运动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51915739/
我很好地使用了 RealmRecyclerViewAdapter。我的问题是实现一个不起作用的 Filterable。这是代码: private class AirportAdapter exten
我在使用 RealmRecyclerViewAdapter 时遇到了一个奇怪的问题。我重写了类,为初始数据指定了 null,为 autoUpdate 指定了 true。适配器已经为 RecyclerV
我正在尝试使用 SearchView 来过滤 RealmRecyclerViewAdapter 提供的列表。我已经尝试了 StackOverflow 上建议的几种方法,包括: EpicPandaFor
一直在使用 realm,它很棒。 遇到了一些事情。想知道我是否做错了什么。 我有一个 RealmRecyclerViewAdapter 用来显示 Realm 查询的结果。如果我在 Realm 中添加或
AndroidStudio 2.2.3 无法解析我的 RealmRecyclerViewAdapter 引用。我已经直接从 Realm 站点关注了 RecyclerViewAdaper 示例。有人可以
我是一名优秀的程序员,十分优秀!