Skip to content

Swipe, drag-and-drop, items clicks, view holder's events delegation and other stuff for Android Recycler View

Notifications You must be signed in to change notification settings

naseemakhtar994/rvtools

 
 

Repository files navigation

RvTools

alt tag

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.

About

Swipe, drag-and-drop, items clicks, view holder's events delegation and other stuff for Android Recycler View

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%