RvTools

Additional

Language
Java
Version
N/A
Created
Feb 13, 2017
Updated
Oct 11, 2017
Owner
Olexii Muraviov (olmur)
Contributor
Olexii Muraviov (olmur)
1
Activity
Badge
Generate
Download
Source code
APK file

Announcement

RvTools

Add lib to your project

compile ('com.olmur.rvtools:rvtools:0.0.5') {
        exclude group: 'com.android.support'
}

Extend RvtRecyclerAdapter class

public class MyAdapter extends RvtListAdapter<MyEntity, MyViewHolder> {
  ...
}

Create ViewHolder that extends RvtViewHolder

public class MyViewHolder extends RvtRecyclerAdapter.RvtViewHolder<MyEntity> {

        private TextView titleTv;

        ViewHolder(@NonNull View view) {
            super(view);
            titleTv = (TextView) view.findViewById(R.id.title_tv);
        }

        @Override
        public void bindViewHolder(MyEntity element) {
            titleTv.setText(element.getTitle());
        }
}

Create Gestures Listeners

public class MainActivity extends AppCompatActivity implements OnSwipeLeftAction, OnSwipeRightAction, OnOrderChangedListener {

    @Override
    public void onSwipeLeftAction(int position) {
        adapter.deletItem(position);
    }
    
    @Override
    public void onSwipeRightAction(int position) {
        adapter.changeItem(position);
    }
    
    @Override
    public void onOrderChanged() {
        Snackbar.make(root, "Order has been changed", Snackbar.LENGTH_SHORT).show();
    }
}

Create Swipe Context Menu Drawer:

You can use RvtSwipeContextMenu

RvtSwipeContextMenu rvtSwipeContextMenu = new RvtSwipeContextMenu.Builder(context)
                        .withIconsRes(R.drawable.ic_edit, R.drawable.ic_delete)
                        .withIconsSizeDp(32)
                        .withIconsMarginFromListEdgesDp(16)
                        .withIconsColorInt(Color.WHITE)
                        .withBackgroundsColorsInt(Color.MAGENTA, Color.RED)
                        .build();

Or create your own swipe context menu by implementing SwipeContextMenuDrawer interface

To add different behaviour on item select/release override these methods in your ViewHolder

 public class MyViewHolder extends RvtViewHolder<MyEntity> {
         ... 
        @Override
        public void onSelected() {
            super.onSelected();
            itemView.setBackgroundColor(Color.GRAY);
            titleTv.setTextColor(Color.WHITE);
        }

        @Override
        public void onReleased() {
            super.onReleased();
            itemView.setBackgroundColor(Color.WHITE);
            titleTv.setTextColor(Color.GRAY);
        }
        
        // Provide selected vh height for elevation increase/decrease when vh is being selected/released.
        // Works only API 21 and above only.
        @Override
        public int selectedVhHeightDp() {
            return RvtUtils.Resources.dpToPixels(itemView.getContext(), 8);
        }
 }

Create RvtRecyclerView and provide it with empty view

RvtRecyclerView recyclerView = (RvtRecyclerView) findViewById(R.id.rvt_recycler_view);
recyclerView.setEmptyView(emptyView);

Create RvTools for your RecyclerView

  RvTools rvtools = new RvTools
                         .Builder(recyclerView)
                         .withSwipeRightAction(this)
                         .withSwipeLeftAction(this)
                         .withMoveAction(this, RvTools.DOWN, RvTools.UP)
                         .withSwipeContextMenuDrawer(rvtSwipeContextMenu)
                         .build();

Than you can bind and unbind rvtools from your recycler view

   rvtools.bind(recyclerView);
   
   rvtools.unbind();

Check out for more examples in sample app.