Simple RecyclerView Adapter with Drag-Sort and Swipe Delete functionality.
- Swipe UP/DOWN (Horizontal) Swipe RIGHT/LEFT (Vertical) to remove an item.
- LongPress and Drag to sort items in RecyclerView.
LinearLayout | HorizontalLayout | GridLayout |
---|---|---|
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.raghavtilak:SimpleDragSwipeRV:1.0.0'
}
public class UserModel {
private String name;
private String phone;
private String country;
public UserModel(String name, String phone, String country) {
this.name = name;
this.phone = phone;
this.country = country;
}
....
}
public class RVAdapter extends DragSwipeAdapter<UserModel> {
public RVAdapter(@NonNull Context context, List<UserModel> datas, int itemLayoutId, RecyclerView recyclerView, boolean isCanDrag, boolean isCanSwipe, int displayMode, int itemSpacing, OnItemClick onItemClickListener) {
super(context, datas, itemLayoutId, recyclerView, isCanDrag, isCanSwipe, displayMode, itemSpacing, onItemClickListener);
}
@Override
protected void bindView(UserModel item, DragSwipeAdapter.ViewHolder viewHolder) {
if(item!=null){
TextView name=(TextView)viewHolder.getView(R.id.textViewName);
TextView phone=(TextView)viewHolder.getView(R.id.textViewPhone);
TextView country=(TextView)viewHolder.getView(R.id.textViewCountry);
name.setText(item.getName());
phone.setText(item.getPhone());
country.setText(item.getCountry());
}
}
@Override
public void onItemSwiped() {
}
@Override
public void onItemMoved() {
}
}
public class MainActivity extends AppCompatActivity implements DragSwipeAdapter.OnItemClick<UserModel> {
...
@Override
public void onClick(View view, int position, UserModel item) {
...
}
...
}
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
userList=new ArrayList<>();
loadData();
listAdapter = new RVAdapter(this,userList,R.layout.item_layout,
recyclerView,
true,false,RVAdapter.VERTICAL,
16,this);
}
...
public DragSwipeAdapter(@NonNull Context context, List<T> datas, int itemLayoutId,
RecyclerView recyclerView,
boolean isCanDrag, boolean isCanSwipe, int displayMode,
int itemSpacing, OnItemClick onItemClickListener)