raghavtilak / SimpleDragSwipeRV

Simple RecyclerViewAdapter library with Swipe to delete and Drag to Sort functionality. Supports all the three Layouts(Vertical, Horizontal, Grid).
3 stars 3 forks source link
android custom-recyclerview drag-swipe-recyclerview dragsort-recyclerview dragsortview hacktoberfest hacktoberfest-accepted hacktoberfest-android hacktoberfest2023 java recyclerview-adapter swipe-recyclerview

SimpleDragSwipeAdapter

Simple RecyclerView Adapter with Drag-Sort and Swipe Delete functionality.
"Buy Me A Coffee"

Generic badge

Functionality

  1. Swipe UP/DOWN (Horizontal) Swipe RIGHT/LEFT (Vertical) to remove an item.
  2. LongPress and Drag to sort items in RecyclerView.

Demo 📱

LinearLayout HorizontalLayout GridLayout

Usage 🛠️

Dependency

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'
}

Model Class


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;
}
....

}

## RecyclerView Adapter #

public class RVAdapter extends DragSwipeAdapter {

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() {

}

}

## OnItemClickListener #

public class MainActivity extends AppCompatActivity implements DragSwipeAdapter.OnItemClick { ...

@Override public void onClick(View view, int position, UserModel item) { ... }

    ...

}


## Attach Adapter to RecyclerView

... @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);
}
...
# Constructor #
public DragSwipeAdapter(@NonNull Context context, List<T> datas, int itemLayoutId,
                        RecyclerView recyclerView,
                        boolean isCanDrag, boolean isCanSwipe, int displayMode,
                        int itemSpacing, OnItemClick onItemClickListener)