Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. wordAdapter.tracker = new SelectionTracker.Builder<Long>(
  2. "mySelectionId",
  3. recyclerView,
  4. new StableIdKeyProvider(recyclerView),
  5. new MyDetailsLookUp(recyclerView),
  6. StorageStrategy.createLongStorage()
  7. ).withSelectionPredicate(
  8. SelectionPredicates.<Long>createSelectAnything()
  9. ).build();
  10.  
  11. class MyDetailsLookUp extends ItemDetailsLookup<Long> {
  12. RecyclerView recyclerView;
  13.  
  14. MyDetailsLookUp(RecyclerView recyclerView) {
  15. this.recyclerView = recyclerView;
  16. }
  17.  
  18. @Nullable
  19. @Override
  20. public ItemDetails<Long> getItemDetails(@NonNull MotionEvent e) {
  21. View view = recyclerView.findChildViewUnder(e.getX(), e.getY());
  22. if (view != null) {
  23. // getting individual view holders
  24. return ((WordAdapter.MyViewHolder) recyclerView.getChildViewHolder(view)).getItemDetails();
  25. }
  26. return null;
  27. }
  28. }
  29.  
  30. ItemDetailsLookup.ItemDetails<Long> getItemDetails(){
  31. return new ItemDetailsLookup.ItemDetails<Long>(){
  32. @Override
  33. public int getPosition() {
  34. return getAdapterPosition();
  35. }
  36.  
  37. @Nullable
  38. @Override
  39. public Long getSelectionKey() {
  40. return getItemId();
  41. }
  42. };
  43. }
  44.  
  45. java.lang.IllegalArgumentException
  46. at androidx.core.util.Preconditions.checkArgument(Preconditions.java:38)
  47. at androidx.recyclerview.selection.DefaultSelectionTracker.anchorRange(DefaultSelectionTracker.java:269)
  48. at androidx.recyclerview.selection.MotionInputHandler.selectItem(MotionInputHandler.java:60)
  49. at androidx.recyclerview.selection.TouchInputHandler.onLongPress(TouchInputHandler.java:132)
  50. at androidx.recyclerview.selection.GestureRouter.onLongPress(GestureRouter.java:96)
  51. at android.view.GestureDetector.dispatchLongPress(GestureDetector.java:778)
  52. at android.view.GestureDetector.-wrap0(Unknown Source:0)
  53. at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:293)
  54. at android.os.Handler.dispatchMessage(Handler.java:105)
  55. at android.os.Looper.loop(Looper.java:164)
  56. at android.app.ActivityThread.main(ActivityThread.java:6541)
  57. at java.lang.reflect.Method.invoke(Native Method)
  58. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  59. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  60.  
  61. java.lang.IllegalStateException: Two different ViewHolders have the same stable ID. Stable IDs in your adapter MUST BE unique and SHOULD NOT change.
  62. ViewHolder 1:ViewHolder{987472c position=1 id=-1, oldPos=-1, pLpos:-1}
  63. View Holder 2:ViewHolder{e50e5f5 position=2 id=-1, oldPos=-1, pLpos:-1 not recyclable(1)} androidx.recyclerview.widget.RecyclerView{617d73 VFED..... .F....ID 42,42-1038,1542 #7f08007b app:id/recyclerview}, adapter:com.example.roomwordssample.WordAdapter@7ada430, layout:androidx.recyclerview.widget.GridLayoutManager@a15b8a9, context:com.example.roomwordssample.Main2Activity@ca1d275
  64. at androidx.recyclerview.widget.RecyclerView.handleMissingPreInfoForChangeError(RecyclerView.java:4058)
  65. at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3982)
  66. at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3652)
  67. at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1877)
  68. at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:407)
  69. at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
  70. at android.view.Choreographer.doCallbacks(Choreographer.java:723)
  71. at android.view.Choreographer.doFrame(Choreographer.java:655)
  72. at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
  73. at android.os.Handler.handleCallback(Handler.java:789)
  74. at android.os.Handler.dispatchMessage(Handler.java:98)
  75. at android.os.Looper.loop(Looper.java:164)
  76. at android.app.ActivityThread.main(ActivityThread.java:6541)
  77. at java.lang.reflect.Method.invoke(Native Method)
  78. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  79. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  80.  
  81. wordAdapter.tracker = new SelectionTracker.Builder<Long>(
  82. "mySelectionId",
  83. recyclerView,
  84. new GetItemDetails(recyclerView, ItemKeyProvider.SCOPE_MAPPED),
  85. new MyDetailsLookUp(recyclerView),
  86. StorageStrategy.createLongStorage()
  87. ).withSelectionPredicate(
  88. SelectionPredicates.<Long>createSelectAnything()
  89. ).build();
  90.  
  91. class CustomItemKeyProvider extends ItemKeyProvider<Long> {
  92. RecyclerView recyclerView;
  93.  
  94. CustomItemKeyProvider(RecyclerView recyclerView, int scope) {
  95. super(scope);
  96. this.recyclerView = recyclerView;
  97. }
  98.  
  99. @Nullable
  100. @Override
  101. public Long getKey(int position) {
  102. return wordAdapter.getItemId(position);
  103. }
  104.  
  105. @Override
  106. public int getPosition(@NonNull Long key) {
  107. RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForItemId(key);
  108. return viewHolder == null ? RecyclerView.NO_POSITION : viewHolder.getLayoutPosition();
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement