What I need is to detect the right swipe of the item and display some activity. I did prev. investigation but it not seems to obvious to catch correct solution. Please, help me.
My code is following.
public class FragmentTwo extends ListFragment {
public FragmentTwo() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment_two, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView listView = getListView();
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(getContext(),"ACTION_UP", Toast.LENGTH_SHORT).show();
return true;
}
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(getContext(),"ACTION_DOWN", Toast.LENGTH_SHORT).show();
return true;
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
Toast.makeText(getContext(),"ACTION_MOVE", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
new FetchTransportData().execute();
}
private class FetchTransportData extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// ...
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// ...
CustomListAdapter adapter = new CustomListAdapter(getActivity(), unitViews);
setListAdapter(adapter);
}
}
}
/**
* Created by admin on 19-12-2017.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public abstract class SwipeHelper extends ItemTouchHelper.SimpleCallback {
public static final int BUTTON_WIDTH = 200;
private RecyclerView recyclerView;
private List<UnderlayButton> buttons;
private GestureDetector gestureDetector;
private int swipedPos = -1;
private float swipeThreshold = 0.5f;
private Map<Integer, List<UnderlayButton>> buttonsBuffer;
private Queue<Integer> recoverQueue;
public Context context;
private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
for (UnderlayButton button : buttons){
if(button.onClick(e.getX(), e.getY()))
break;
}
return true;
}
};
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent e) {
if (swipedPos < 0) return false;
Point point = new Point((int) e.getRawX(), (int) e.getRawY());
RecyclerView.ViewHolder swipedViewHolder = recyclerView.findViewHolderForAdapterPosition(swipedPos);
if(swipedViewHolder==null) return false;
View swipedItem = swipedViewHolder.itemView;
Rect rect = new Rect();
swipedItem.getGlobalVisibleRect(rect);
if (e.getAction() == MotionEvent.ACTION_DOWN || e.getAction() == MotionEvent.ACTION_UP ||e.getAction() == MotionEvent.ACTION_MOVE) {
if (rect.top < point.y && rect.bottom > point.y)
gestureDetector.onTouchEvent(e);
else {
recoverQueue.add(swipedPos);
swipedPos = -1;
recoverSwipedItem();
}
}
return false;
}
};
public SwipeHelper(Context context, RecyclerView recyclerView) {
super(0, ItemTouchHelper.LEFT);
this.recyclerView = recyclerView;
this.context=context;
this.buttons = new ArrayList<>();
this.gestureDetector = new GestureDetector(context, gestureListener);
this.recyclerView.setOnTouchListener(onTouchListener);
buttonsBuffer = new HashMap<>();
recoverQueue = new LinkedList<Integer>(){
@Override
public boolean add(Integer o) {
if (contains(o))
return false;
else
return super.add(o);
}
};
attachSwipe();
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int pos = viewHolder.getAdapterPosition();
if (swipedPos != pos)
recoverQueue.add(swipedPos);
swipedPos = pos;
if (buttonsBuffer.containsKey(swipedPos))
buttons = buttonsBuffer.get(swipedPos);
else
buttons.clear();
buttonsBuffer.clear();
swipeThreshold = 0.5f ;
recoverSwipedItem();
}
@Override
public float getSwipeEscapeVelocity(float defaultValue) {
return 0.1f * defaultValue;
}
@Override
public float getSwipeVelocityThreshold(float defaultValue) {
return 2.0f * defaultValue;
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
int pos = viewHolder.getAdapterPosition();
float translationX = dX;
View itemView = viewHolder.itemView;
float height = (float) itemView.getBottom() - (float) itemView.getTop();
if (pos < 0){
swipedPos = pos;
return;
}
if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
if(dX < 0) {
List<UnderlayButton> buffer = new ArrayList<>();
if (!buttonsBuffer.containsKey(pos)){
instantiateUnderlayButton(viewHolder, buffer);
buttonsBuffer.put(pos, buffer);
}
else {
buffer = buttonsBuffer.get(pos);
}
translationX = dX * buffer.size() * height / itemView.getWidth();
drawButtons(c, itemView, buffer, pos, translationX);
}
}
super.onChildDraw(c, recyclerView, viewHolder, translationX, dY, actionState, isCurrentlyActive);
}
private synchronized void recoverSwipedItem(){
while (!recoverQueue.isEmpty()){
Iterator iterator=recoverQueue.iterator();
while(iterator.hasNext())
{
int pos = recoverQueue.poll();
if (pos > -1) {
//clearView(recyclerView,recyclerView.findViewHolderForAdapterPosition(pos));
recyclerView.getAdapter().notifyItemChanged(pos);
recyclerView.requestLayout();
//recyclerView.updateViewLayout(recyclerView.getChildAt(pos),recyclerView.getChildAt(pos).getLayoutParams()) ;
//itemTouchUIUtil.clearView(recyclerView,recyclerView.getChildAt(pos));
}
}
recoverQueue.clear();
}
}
private void drawButtons(Canvas c, View itemView, List<UnderlayButton> buffer, int pos, float dX){
//float right = itemView.getRight();
/// float dButtonWidth = (-1) * dX / buffer.size();
for (UnderlayButton button : buffer) {
//float left = right - dButtonWidth;
button.onDraw(c,itemView,dX, pos ,context );
//right = left;
}
}
public void attachSwipe(){
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(this);
itemTouchHelper.attachToRecyclerView(recyclerView);
}
public abstract void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons);
public static class UnderlayButton {
private String text;
private int imageResId;
private int color;
private int pos;
private RectF clickRegion;
private UnderlayButtonClickListener clickListener;
public UnderlayButton(String text, int imageResId, int color, UnderlayButtonClickListener clickListener) {
this.text = text;
this.imageResId = imageResId;
this.color = color;
this.clickListener = clickListener;
}
public boolean onClick(float x, float y){
if (clickRegion != null && clickRegion.contains(x, y)){
clickListener.onClick(pos);
return true;
}
return false;
}
public void onDraw(Canvas c,View itemView, float dX, int pos,Context context){
Bitmap icon;
Paint p = new Paint();
float height = (float) itemView.getBottom() - (float) itemView.getTop();
float width = height / 3;
// Draw background
p.setColor(color);
RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop()+12,(float) itemView.getRight(), (float) itemView.getBottom()-6);
c.drawRect(background,p);
icon = BitmapFactory.decodeResource(context.getResources(), imageResId);
RectF icon_dest = new RectF((float) itemView.getRight() - 2*width ,(float) itemView.getTop() + width,(float) itemView.getRight() - width,(float)itemView.getBottom() - width);
c.drawBitmap(icon,null,icon_dest,p);
clickRegion = background;
this.pos = pos;
}
}
public interface UnderlayButtonClickListener {
void onClick(int pos);
}
}
SwipeHelper swipeHelper=new SwipeHelper(getContext(),recyclerView) {
@Override
public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {
underlayButtons.add(new UnderlayButton("call",
R.drawable.call,
Color.parseColor("#388E3C"), new UnderlayButtonClickListener() {
@Override
public void onClick(final int pos) {
//Utils.Toast_L(getContext(),"Swipe call" +Temp_filter.get(pos).getName());
new AlertDialog.Builder(context)
.setCancelable(true)
.setTitle("Call")
.setMessage("Do you want to make a call to : "+Temp_filter.get(pos).getName())
.setPositiveButton("call", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+Temp_filter.get(pos).getMobile())));
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.create()
.show();
}
}));
}
};
You can make use of SwipeActionAdapter
library.
Having defined your ListFragment
as such:
public class MyListFragment extends ListFragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String[] content = new String[20];
for (int i = 0; i < 20; i++) content[i] = "Row " + (i + 1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<>(
getContext(),
R.layout.row_bg,
R.id.text,
new ArrayList<>(Arrays.asList(content))
);
SwipeActionAdapter mAdapter = new SwipeActionAdapter(stringAdapter);
mAdapter.setListView(getListView());
setListAdapter(mAdapter);
// Set backgrounds for the swipe directions
mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT, R.layout.row_bg_left_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT, R.layout.row_bg_left)
.addBackground(SwipeDirection.DIRECTION_FAR_RIGHT, R.layout.row_bg_right_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT, R.layout.row_bg_right);
// Listen to swipes
mAdapter.setSwipeActionListener(new SwipeActionAdapter.SwipeActionListener() {
@Override
public boolean hasActions(int position, SwipeDirection direction) {
if (direction.isLeft()) return false; // Change this to false to disable left swipes
if (direction.isRight()) return true;
return false;
}
@Override
public boolean shouldDismiss(int position, SwipeDirection direction) {
// Only dismiss an item when swiping normal left
return direction == SwipeDirection.DIRECTION_FAR_RIGHT;
}
@Override
public void onSwipe(int[] positionList, SwipeDirection[] directionList) {
for (int i = 0; i < positionList.length; i++) {
SwipeDirection direction = directionList[i];
int position = positionList[i];
String dir = "";
switch (direction) {
case DIRECTION_FAR_RIGHT:
dir = "Far right";
break;
case DIRECTION_NORMAL_RIGHT:
dir = "Right";
break;
}
Toast.makeText(
getContext(),
dir + " swipe Action triggered on " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
mAdapter.notifyDataSetChanged();
}
}
});
}
}
You'll get following output:
All of the resource xml files can be found in the library resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With