h6ah4i / android-advancedrecyclerview

RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)
https://advancedrecyclerview.h6ah4i.com/
Apache License 2.0
5.32k stars 862 forks source link

Swipe to end #122

Open pixelbendr opened 9 years ago

pixelbendr commented 9 years ago

Hello, swiping to the end of the screen doesn't call onItemPinned event. Can anyone help. :(

h6ah4i commented 9 years ago

Hi. Could you show me your implementation?

pixelbendr commented 9 years ago

I have extracted the methods related to the library since I have a lot going on in my code base


   public ChatAdapter(RecyclerView recyclerView, final Context context) {

        super();
        this.chatList = new ArrayList<>();
        this.recyclerView = recyclerView;
        this.context = context;

       setHasStableIds(true);
    }

   @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        switch (getItemViewType(i)) {
            case IMAGE:
                break;
            default:
                if (viewHolder.itemView != null) {
                    ChatViewHolder chatViewHolder = (ChatViewHolder) viewHolder;
                    Chat chat = (Chat) chatList.get(i);
                    setChat(chat);
                    chatViewHolder.setChat(chat);
                }
        }
    }

    @Override
    public SwipeResultAction onSwipeItem(RecyclerView.ViewHolder holder, int position, int result) {
        switch (result) {
            // swipe left -- pin
            //edit
            case Swipeable.RESULT_SWIPED_RIGHT:
                MODE = EDIT_MODE;
                return new SwipeLeftResultAction(this, position);
            // swipe left -- pin
            //confirm deletion
            case Swipeable.RESULT_SWIPED_LEFT:
                MODE = DELETE_MODE;
                return new SwipeLeftResultAction(this, position);
            // other --- do nothing
            case Swipeable.RESULT_CANCELED:
            default:
                return new UnpinResultAction(this, position);
        }
    }

    @Override
    public int onGetSwipeReactionType(RecyclerView.ViewHolder holder, int position, int x, int y) {
        if (isPrivateMessage(holder)) {
            return Swipeable.REACTION_CAN_SWIPE_BOTH_H;
        } else if (isRegularMessage(holder)) {
            return Swipeable.REACTION_CAN_SWIPE_RIGHT;
        }
        return Swipeable.REACTION_CAN_NOT_SWIPE_ANY;
    }

    @Override
    public void onSetSwipeBackground(RecyclerView.ViewHolder holder, int position, int type) {
        int bgRes = 0;
        switch (type) {
            case RecyclerViewSwipeManager.DRAWABLE_SWIPE_LEFT_BACKGROUND:
                bgRes = R.drawable.bg_swipe_item_delete_left;
                break;
            case RecyclerViewSwipeManager.DRAWABLE_SWIPE_RIGHT_BACKGROUND:
                bgRes = R.drawable.bg_swipe_item_edit_right;
                break;
        }

        holder.itemView.setBackgroundResource(bgRes);

    }
h6ah4i commented 9 years ago

Thanks, but I cannot see any issues in the part of your code. I also want to see the implementation of the SwipeLeftResultAction class in your code.

pixelbendr commented 9 years ago

Here is the code for it.

public class SwipeLeftResultAction extends SwipeResultActionMoveToSwipedDirection {
    private ChatAdapter adapter;
    private final int position;
    private boolean setPinned;

    public SwipeLeftResultAction(ChatAdapter adapter, int position) {
        this.adapter = adapter;
        this.position = position;
    }

    @Override
    protected void onPerformAction() {
        super.onPerformAction();

        Chat chat = adapter.getChat();

        if (!chat.isPinned()) {
            chat.isPinned(true);
            adapter.notifyItemChanged(position);
            setPinned = true;
        }
    }

    @Override
    protected void onSlideAnimationEnd() {
        super.onSlideAnimationEnd();

        if (setPinned && adapter.getSwipeEventListener() != null) {
            adapter.getSwipeEventListener().onItemPinned(position);
        }
    }

    @Override
    protected void onCleanUp() {
        super.onCleanUp();
        // clear the references
        adapter = null;
    }
}
h6ah4i commented 9 years ago

Thanks the SwipeLeftResultAction also looks good.

Did you confirmed that the onSlideAnimationEnd() method is never called? If so, please take a look at the ItemSlidingAnimator class and debug this class. Maybe the onAnimationEnd() method is not called properly for some reason...

pixelbendr commented 9 years ago

Ok thanks. Will do some debugging on them. "is never called?" or it's suppose to be called?