I am working on floating Keyboard using Recycler view which would appear on screen when a button is clicked and dismisses when Right / Left swipe is done. As I am doing so I observed the swiping the keyboard is not smoother (Some times swipe listeners does not detect swipe gestures.) . Then I referred the following links and re-worked,
https://medium.com/@euryperez/android-pearls-detect-swipe-and-touch-over-a-view-203ae2c028dc
https://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures.
I added gesture listeners to the child of Recycler view and used three override methods onClick(), onSwipeRight() and onSwipeLeft(). The position of the onClick is obtained perfectly but sometimes Swipe Listeners are not detected this makes users to feel swiping is not smoother.
All I needed is to make swipe smoother and detecting position of clicking keys without compromising both. Is there any ways to implement these feature if there any way suggest me!. Here is my code. Thanks in Advance !.
public ViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(R.id.textView);
imageView = (ImageView) v.findViewById(R.id.imageView);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativeLayout);
mBodyContainer = (ConstraintLayout) v.findViewById(R.id.body_container);
cllayout = (RelativeLayout) v.findViewById(R.id.cllayout);
setSwipeGestureForParent(cllayout);
}
private void setSwipeGestureForParent(final View view) {
view.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View v, MotionEvent event) {
parentview.stopScroll();
String view = item.text;
if (view.equals("")) {
} else if (v.getId() != R.id.body_container && !view.equals("")) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_selected));
break;
case MotionEvent.ACTION_MOVE:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_selected));
break;
case MotionEvent.ACTION_BUTTON_PRESS:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_selected));
break;
default:
imageView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.bg_keyboard_key_normal));
break;
}
}
return super.onTouch(v, event);
}
@Override
public void onClick() {
super.onClick();
Toast.makeText(mContext, "onclick", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeLeft() {
Toast.makeText(mContext, "Swipeleft", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeRight() {
Toast.makeText(mContext, "SwipeRight", Toast.LENGTH_SHORT).show();
}
});
}
I have prepared 2 general 'copy-and-paste' classes for you so you can very easily use all gestures on the RecyclerView.
1 - GestureDetector - just add it to your project
2 - TouchListener - just add the interface definition to your project. To make the use even more easy, I have used some default implementations of the interface. So, you need to add to your module's build.gradle:
Add the interface:
3 - Now you can attach to any view the TouchListener and implement only those methods you really need. An example is:
That's all! Enjoy.