Anand Jain
Anand Jain

Reputation: 2385

How to swipe both side left and right in recyclerview in android

I want to swipe recyclerview both side left and right like below image:- enter image description here

But I can't do I see many library but I find only one side swipe . I want both side swipe how I do that help me to do do this.

Thanks in advance

Upvotes: 4

Views: 10213

Answers (3)

Avinash Kumar
Avinash Kumar

Reputation: 318

It maybe late, but could help.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_childs, container, false);
    recyclerView = v.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
    recyclerView.addItemDecoration(new VerticalItemDecoration());
    recyclerView.setAdapter(new ChildCareAdapter());
    touchHelper.attachToRecyclerView(recyclerView); // Attaching with RecyclerView
    return v;
}

ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (i == ItemTouchHelper.LEFT) {
            Toast.makeText(getContext(),"Swipe left",Toast.LENGTH_SHORT).show();
        } else if (i == ItemTouchHelper.RIGHT) {
            Toast.makeText(getContext(),"Swipe right",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            float alpha = 1 - (Math.abs(dX) / recyclerView.getWidth());
            viewHolder.itemView.setAlpha(alpha);
        }
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
});

Upvotes: 0

Suhayl SH
Suhayl SH

Reputation: 1223

You need something like this:

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();

if (direction == ItemTouchHelper.LEFT){
  Log.i("ItemSwipe", "LEFT");
} 
else if (direction == ItemTouchHelper.RIGHT){
  Log.i("ItemSwipe", "RIGHT");
 }
}

Upvotes: -2

Nikhil Sharma
Nikhil Sharma

Reputation: 603

You can try below given one lib you just have to some modification

https://github.com/daimajia/AndroidSwipeLayout

Thanks!

Upvotes: 5

Related Questions