android-advancedrecyclerview
android-advancedrecyclerview copied to clipboard
Is there any way to prevent other items from binding data when swiping a specific item?
For example, I got 3 rows of data. When I swiping the first one, I find 2nd, 3rd items' onBindViewHolder methods are also called. I do not want them to be bound when swiping because I have async tasks to retrieve images on each row. I can see they are re-retrieved every time when I swipe a item.
I am thinking to check the swipe flag from the view holder in onBindViewHolder ? but I am not sure how to do it correctly? I have tried below but not working.
if ((swipeState & SwipeableItemConstants.STATE_FLAG_IS_UPDATED) != 0) {
int bgResId;
if ((swipeState & SwipeableItemConstants.STATE_FLAG_IS_ACTIVE) != 0) {
} else if ((swipeState & SwipeableItemConstants.STATE_FLAG_SWIPING) != 0) {
} else {
}
}
I noticed that when swiping: The swiping item comes with flag: -2147483645 Others come with flag: -2147483647
When settled, they come with -2147483648
I also have tried Bitwise OR but not working.
Hi. Try the following code snippet.
public static class MyViewHolder extends AbstractSwipeableItemViewHolder {
public boolean needToBind;
public MyViewHolder(View v) {
super(v);
needToBind = true;
...
}
...
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if (!holder.needToBind) {
return;
}
...
holder.needToBind = false;
}
@Override
public void onViewRecycled(MyViewHolder holder) {
super.onViewRecycled(holder);
holder.needToBind = true;
}
FYI - 1
| signed int | hex | flags |
|---|---|---|
-2147483645 |
0x80000003 |
STATE_FLAG_IS_UPDATED + STATE_FLAG_SWIPING + STATE_FLAG_IS_ACTIVE |
-2147483647 |
0x80000001 |
STATE_FLAG_IS_UPDATED + STATE_FLAG_SWIPING |
-2147483648 |
0x80000000 |
STATE_FLAG_IS_UPDATED |
FYI - 2
For this situation, using onBindViewHolder with payloads parameter is the most relevant solution. However, this library does not that method yet, so please use the above code as workaround.
Thanks h6ah4i. First one is working !!
Just came across another issue. The first solution does not work well if I need to change the data set. So I put data item id in the ViewHolder rather than needToBind flag. Thank check if the id is the same, if is the same then don't bind it.