Java Code Examples for androidx.recyclerview.widget.ItemTouchHelper#LEFT

The following examples show how to use androidx.recyclerview.widget.ItemTouchHelper#LEFT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessagesActivity.java    From android with MIT License 6 votes vote down vote up
public SwipeToDeleteCallback(ListMessageAdapter adapter) {
    super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
    this.adapter = adapter;

    int backgroundColorId =
            ContextCompat.getColor(MessagesActivity.this, R.color.swipeBackground);
    int iconColorId = ContextCompat.getColor(MessagesActivity.this, R.color.swipeIcon);

    Drawable drawable =
            ContextCompat.getDrawable(MessagesActivity.this, R.drawable.ic_delete);
    icon = null;
    if (drawable != null) {
        icon = DrawableCompat.wrap(drawable.mutate());
        DrawableCompat.setTint(icon, iconColorId);
    }

    background = new ColorDrawable(backgroundColorId);
}
 
Example 2
Source File: ItemMoveCallback.java    From RecyclerViewHelper with Apache License 2.0 6 votes vote down vote up
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView,
                            @NonNull RecyclerView.ViewHolder viewHolder) {
    // Set movement flags based on the layout manager.
    int swipeFlags = 0;
    int dragFlags;
    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
        dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN
                | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
    } else {
        if (mIsFreedom) {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN
                    | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        } else {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }
    }
    return makeMovementFlags(dragFlags, swipeFlags);
}
 
Example 3
Source File: HistoryFragment.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
private ItemTouchHelper.Callback createTouchHelperCallback() {
    return new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT) {
        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }

        @Override
        public boolean isItemViewSwipeEnabled() {
            return false;
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder from, RecyclerView.ViewHolder to) {
            HistoryAdapter adapter;
            if (recyclerView.getAdapter() instanceof HistoryAdapter) {
                adapter = (HistoryAdapter) recyclerView.getAdapter();
                adapter.swapItems(from.getAdapterPosition(), to.getAdapterPosition());
                return true;
            }

            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {

        }
    };
}
 
Example 4
Source File: ItemTouchHelperCallback.java    From FlexibleAdapter with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                        float dX, float dY, int actionState, boolean isCurrentlyActive) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE &&
            viewHolder instanceof ViewHolderCallback) {

        // Update visibility for RearViews - Convert to custom VH
        ViewHolderCallback viewHolderCallback = (ViewHolderCallback) viewHolder;
        View frontView = viewHolderCallback.getFrontView();

        // Orientation independent
        float dragAmount = dX;
        if (dY != 0) {
            dragAmount = dY;
        }

        // Manage opening - Is Left or Right View?
        int swipingDirection = 0;//0 is to reset the frontView
        if (dragAmount > 0) {
            swipingDirection = ItemTouchHelper.RIGHT;//DOWN
        } else if (dragAmount < 0) {
            swipingDirection = ItemTouchHelper.LEFT;//TOP
        }

        setLayoutVisibility(viewHolderCallback, swipingDirection);
        // Translate the FrontView
        getDefaultUIUtil().onDraw(c, recyclerView, frontView, dX, dY, actionState, isCurrentlyActive);

    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}
 
Example 5
Source File: ItemTouchHelperCallback.java    From FlexibleAdapter with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int dragFlags;
    int swipeFlags;
    // Set movement flags based on the Layout Manager and Orientation
    if (layoutManager instanceof GridLayoutManager || layoutManager instanceof StaggeredGridLayoutManager) {
        dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        swipeFlags = 0;
    } else if (LayoutUtils.getOrientation(recyclerView) == LinearLayoutManager.HORIZONTAL) {
        dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        swipeFlags = mSwipeFlags > 0 ? mSwipeFlags : ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    } else {
        dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        swipeFlags = mSwipeFlags > 0 ? mSwipeFlags : ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
    }
    // Disallow item swiping or dragging
    if (viewHolder instanceof ViewHolderCallback) {
        ViewHolderCallback viewHolderCallback = (ViewHolderCallback) viewHolder;
        if (!viewHolderCallback.isDraggable()) {
            dragFlags = 0;
        }
        if (!viewHolderCallback.isSwipeable()) {
            swipeFlags = 0;
        }
    }
    return makeMovementFlags(dragFlags, swipeFlags);
}
 
Example 6
Source File: PeekabooTouchHelperCallback.java    From materialistic with Apache License 2.0 5 votes vote down vote up
PeekabooTouchHelperCallback(Context context) {
    super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
    mDefaultTextColor = ContextCompat.getColor(context,
            AppUtils.getThemedResId(context, android.R.attr.textColorPrimary));
    mPaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.text_size_small));
    mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    mPadding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
}
 
Example 7
Source File: ItemTouchHelperCallback.java    From FastLib with Apache License 2.0 5 votes vote down vote up
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    //上下拖拽
    int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    //向右侧滑
    int swipeFlags = ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT;
    return makeMovementFlags(dragFlags, swipeFlags);
}
 
Example 8
Source File: ItemSwipeCallback.java    From RecyclerViewHelper with Apache License 2.0 5 votes vote down vote up
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView,
                            @NonNull RecyclerView.ViewHolder viewHolder) {
    // Set movement flags.
    int dragFlags = 0;
    int swipeFlags;
    if (mOrientation == LinearLayout.HORIZONTAL) {
        swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    } else {
        swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
    }
    return makeMovementFlags(dragFlags, swipeFlags);
}
 
Example 9
Source File: QueryItemTouchHelper.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
@Override
public int getSwipeDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
    if (viewHolder instanceof ThreadOverviewAdapter.ThreadOverviewViewHolder) {
        final ThreadOverviewAdapter.ThreadOverviewViewHolder threadOverviewViewHolder = (ThreadOverviewAdapter.ThreadOverviewViewHolder) viewHolder;
        if (onQueryItemSwipe != null) {
            final Swipable swipable = onQueryItemSwipe.onQueryItemSwipe(viewHolder.getAdapterPosition());
            if (swipable == Swipable.NO) {
                return 0;
            }
            @DrawableRes final int resource;
            switch (swipable) {
                case ARCHIVE:
                    resource = R.drawable.ic_archive_white_24dp;
                    break;
                case REMOVE_FLAGGED:
                    resource = R.drawable.ic_star_border_white_24dp;
                    break;
                default:
                    resource = R.drawable.ic_label_off_white_24dp;
                    break;

            }
            threadOverviewViewHolder.binding.endSwipeActionIndicator.setImageResource(resource);
            threadOverviewViewHolder.binding.startSwipeActionIndicator.setImageResource(resource);
            return ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT;
        }
    }
    return 0;
}
 
Example 10
Source File: SearchUsersActivity.java    From Hify with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ViewPump.init(ViewPump.builder()
            .addInterceptor(new CalligraphyInterceptor(
                    new CalligraphyConfig.Builder()
                            .setDefaultFontPath("fonts/bold.ttf")
                            .setFontAttrId(R.attr.fontPath)
                            .build()))
            .build());

    setContentView(R.layout.activity_search_users);
    mDialog = new ProgressDialog(this);
    mDialog.setMessage("Please wait..");
    mDialog.setIndeterminate(true);
    mDialog.setCanceledOnTouchOutside(false);
    mDialog.setCancelable(false);

    Toolbar toolbar=findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setNavigationBarColor(Color.parseColor("#212121"));
    }

    mRecyclerView = findViewById(R.id.usersList);
    searchText = findViewById(R.id.searchText);

    mAuth = FirebaseAuth.getInstance();
    mFirestore = FirebaseFirestore.getInstance();
    fab = findViewById(R.id.search_button);
    usersList = new ArrayList<>();
    usersAdapter = new SearchFriendAdapter(usersList, this, findViewById(R.id.layout));

    itemTouchHelperCallback = new RecyclerViewTouchHelper(0, ItemTouchHelper.LEFT, new RecyclerViewTouchHelper.RecyclerItemTouchHelperListener() {
        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
            if (viewHolder instanceof SearchFriendAdapter.ViewHolder) {
                // get the removed item name to display it in snack bar
                String name = usersList.get(viewHolder.getAdapterPosition()).getName();

                // backup of removed item for undo purpose
                final Friends deletedItem = usersList.get(viewHolder.getAdapterPosition());
                final int deletedIndex = viewHolder.getAdapterPosition();

                Snackbar snackbar = Snackbar
                        .make(findViewById(R.id.layout), "Friend request sent to " + name, Snackbar.LENGTH_LONG);

                // remove the item from recycler view
                usersAdapter.removeItem(viewHolder.getAdapterPosition(), snackbar, deletedIndex, deletedItem);

            }
        }
    });


    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    //new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);
    mRecyclerView.setAdapter(usersAdapter);

}
 
Example 11
Source File: QueryItemTouchHelper.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
public QueryItemTouchHelper() {
    super(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT);
}
 
Example 12
Source File: DownloadFileItemHelperCallback.java    From Audinaut with GNU General Public License v3.0 4 votes vote down vote up
public DownloadFileItemHelperCallback(SubsonicFragment fragment, boolean mainList) {
    super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
    this.fragment = fragment;
    this.mainList = mainList;
}
 
Example 13
Source File: RecyclerViewExampleActivity.java    From realm-android-adapters with Apache License 2.0 4 votes vote down vote up
TouchHelperCallback() {
    super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
}
 
Example 14
Source File: AddFriends.java    From Hify with MIT License 4 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    firestore = FirebaseFirestore.getInstance();
    mAuth = FirebaseAuth.getInstance();

    mRecyclerView = view.findViewById(R.id.recyclerView);
    refreshLayout=view.findViewById(R.id.refreshLayout);

    ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerViewTouchHelper(0, ItemTouchHelper.LEFT, new RecyclerViewTouchHelper.RecyclerItemTouchHelperListener() {
        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
            if (viewHolder instanceof AddFriendAdapter.ViewHolder) {
                // get the removed item name to display it in snack bar
                String name = usersList.get(viewHolder.getAdapterPosition()).getName();

                // backup of removed item for undo purpose
                final Friends deletedItem = usersList.get(viewHolder.getAdapterPosition());
                final int deletedIndex = viewHolder.getAdapterPosition();

                Snackbar snackbar = Snackbar
                        .make(view.findViewById(R.id.layout), "Friend request sent to " + name, Snackbar.LENGTH_LONG);

                // remove the item from recycler view
                usersAdapter.removeItem(viewHolder.getAdapterPosition(), snackbar, deletedIndex, deletedItem);

            }
        }
    });

    usersList = new ArrayList<>();
    usersAdapter = new AddFriendAdapter(usersList, view.getContext(), view.findViewById(R.id.layout));

    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(view.getContext(), DividerItemDecoration.VERTICAL));
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);
    mRecyclerView.setAdapter(usersAdapter);

    refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            getAllUsers();
        }
    });

    getAllUsers();

}
 
Example 15
Source File: StoryRecyclerViewAdapter.java    From materialistic with Apache License 2.0 4 votes vote down vote up
public StoryRecyclerViewAdapter(Context context) {
    super(context);
    mCallback = new ItemTouchHelperCallback(context,
            Preferences.getListSwipePreferences(context)) {
        @Override
        public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            Item item = getItem(viewHolder.getAdapterPosition());
            if (item == null) {
                return 0;
            }
            mSaved = item.isFavorite();
            return checkSwipeDir(0, ItemTouchHelper.LEFT, mCallback.getLeftSwipeAction(), item) |
                    checkSwipeDir(0, ItemTouchHelper.RIGHT, mCallback.getRightSwipeAction(), item);
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            Preferences.SwipeAction action = direction == ItemTouchHelper.LEFT ?
                    getLeftSwipeAction() : getRightSwipeAction();
            Item item = getItem(viewHolder.getAdapterPosition());
            if (item == null) {
                return;
            }
            switch (action) {
                case Save:
                    toggleSave(item);
                    break;
                case Refresh:
                    refresh(item, viewHolder);
                    break;
                case Vote:
                    notifyItemChanged(viewHolder.getAdapterPosition());
                    vote(item, viewHolder);
                    break;
                case Share:
                    notifyItemChanged(viewHolder.getAdapterPosition());
                    AppUtils.share(mContext, item.getDisplayedTitle(), item.getUrl());
                    break;
            }
        }

        private int checkSwipeDir(int swipeDirs, int swipeDir, Preferences.SwipeAction action, Item item) {
            switch (action) {
                case None:
                    break;
                case Vote:
                    if (!item.isVoted() && !item.isPendingVoted()) {
                        swipeDirs |= swipeDir;
                    }
                    break;
                default:
                    swipeDirs |= swipeDir;
                    break;
            }
            return swipeDirs;
        }
    };
    mItemTouchHelper = new ItemTouchHelper(mCallback);
}
 
Example 16
Source File: SimpleItemTouchHelperCallback.java    From SkyTube with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
	int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
	return makeMovementFlags(dragFlags, 0);
}
 
Example 17
Source File: AccessLogActivity.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * コンストラクタ.
 * @param context コンテキスト
 */
SwipeToDeleteCallback(Context context) {
    super(0, (ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT));
    mDeleteIcon = ContextCompat.getDrawable(context, android.R.drawable.ic_menu_delete);
    mBackground = new ColorDrawable(Color.rgb(235, 55, 35));
}
 
Example 18
Source File: MainActivity.java    From FlexibleAdapter with Apache License 2.0 4 votes vote down vote up
@Override
public void onItemSwipe(final int position, int direction) {
    Log.i("onItemSwipe position=%s direction=%s", position, (direction == ItemTouchHelper.LEFT ? "LEFT" : "RIGHT"));

    // FULL_SWIPE: Direct action no Undo Action.
    // Do something based on direction when item has been swiped:
    //   1) optional: update item, set "read" if an email, etc..
    //   2) remove the item from the adapter

    // Create list for single position (only in onItemSwipe)
    List<Integer> positions = Collections.singletonList(position); // This is an immutable list and cannot be sort!
    // Build the message
    IFlexible abstractItem = mAdapter.getItem(position);
    StringBuilder message = new StringBuilder();
    message.append(extractTitleFrom(abstractItem)).append(" ");
    // Experimenting NEW feature
    if (abstractItem.isSelectable())
        mAdapter.setRestoreSelectionOnUndo(false);

    // Perform different actions
    // Here, option 1) is implemented
    if (direction == ItemTouchHelper.LEFT) {
        message.append(getString(R.string.action_archived));

        // Example of UNDO color
        int actionTextColor;
        if (Utils.hasMarshmallow()) {
            actionTextColor = getColor(R.color.material_color_orange_500);
        } else {
            //noinspection deprecation
            actionTextColor = getResources().getColor(R.color.material_color_orange_500);
        }

        mAdapter.setPermanentDelete(false);
        new UndoHelper(mAdapter, this)
                .withPayload(Payload.CHANGE)          // You can provide any custom object
                .withConsecutive(false)               // Keep all previous archived items until time out
                .withAction(UndoHelper.Action.UPDATE) // Specify the action
                .withActionTextColor(actionTextColor) // Change color of the action text
                .start(positions, findViewById(R.id.main_view), R.string.action_archived,
                        R.string.undo, UndoHelper.UNDO_TIMEOUT);

        // Here, option 2) is implemented
    } else if (direction == ItemTouchHelper.RIGHT) {
        // Prepare for next deletion
        message.append(getString(R.string.action_deleted));
        mRefreshHandler.sendEmptyMessage(REFRESH_START);
        mAdapter.setPermanentDelete(false);

        new UndoHelper(mAdapter, this)
                .withPayload(null)     // You can provide any custom object
                .withConsecutive(true) // Commit the previous action
                .start(positions, findViewById(R.id.main_view), message,
                        getString(R.string.undo), UndoHelper.UNDO_TIMEOUT);

        // Handle ActionMode title
        if (mAdapter.getSelectedItemCount() == 0) {
            mActionModeHelper.destroyActionModeIfCan();
        } else {
            mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
        }
    }
}
 
Example 19
Source File: Friends.java    From Hify with MIT License 4 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    firestore = FirebaseFirestore.getInstance();
    mAuth = FirebaseAuth.getInstance();

    mRecyclerView =  getView().findViewById(R.id.recyclerView);
    refreshLayout=getView().findViewById(R.id.refreshLayout);

    usersList = new ArrayList<>();
    usersAdapter = new ViewFriendAdapter(usersList, view.getContext());

    ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerViewTouchHelper(0, ItemTouchHelper.LEFT, new RecyclerViewTouchHelper.RecyclerItemTouchHelperListener() {
        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
            if (viewHolder instanceof ViewFriendAdapter.ViewHolder) {

                usersAdapter.removeItem(viewHolder.getAdapterPosition());

            }
        }
    });

    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(view.getContext(), DividerItemDecoration.VERTICAL));
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);
    mRecyclerView.setAdapter(usersAdapter);

    refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            startListening();
        }
    });

    startListening();

}