Is there any way to disable getView() method in ListView Adapter when I use MotionEvent.ACTION_MOVE in OnTouchListener??
I need to do because i try move my conteiner(inside is my ListView) by finger. I use for this method setLayoutParams:
private class MyListener implements View.OnTouchListener{
#Override
public boolean onTouch(View v, MotionEvent event) {
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
RelativeLayout.LayoutParams paramsUp = (RelativeLayout.LayoutParams) myBar.getLayoutParams();
paramsUp.topMargin = 0;
myBar.setLayoutParams(paramsUp);
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams paramsMove = (RelativeLayout.LayoutParams) myBar.getLayoutParams();
paramsMove.topMargin = Y - yDelta;
myBar.setLayoutParams(paramsMove);
break;
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) myBar.getLayoutParams();
yDelta = Y - lParams.leftMargin;
break;
}
//myBar.invalidate();
return true;
}
}
When I moving container, everything happens very slowly. This is because the container changes its size and all objects are loaded again.
And my second question:
How to disbale scroll when I move ListView item? For example: move on left or move on right.
Thx
Related
I thought this is a pretty relevant and common question, but I couldnt find an answer.
At the moment I have this method:
public boolean onTouchEvent (MotionEvent evt){
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
do stuff ...
}
}
So if the user taps on the screen (wherever) the code is executed. Now I want the distinction between the right side of the display and the left side (left side means --> go back).
You can do this many ways. Here is one of them:
Attach onTouch listener to the view, which stretches to its edges. (For example your RelativeLayout which holds rest of views)
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float halfOfAScreen = mainLayout.getMaxWidth() / 2;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float fingerPosition = event.getX();
if(fingerPosition < halfOfAScreen) {
onBackPressed();
}
return true;
default:
return false;
}
}
};
Refer to this post on how to get touch position.
It seems in your case you will use
int x = (int)event.getX();
int y = (int)event.getY();
and work within the bounds of your layout that you want the app to react to.
I want to move any normal view in my app along with swipe.
I am able to detect swipe using OnTouchListener in onTouch method. But am not able to move the view along with the swipe.
I want to move the view along with the swipe.
I think so that something i need to implement inside onTouch method to move the view or item along with swipe, but what i don't know.
Any help will be highly appreciated.
You just need to use view.setTranslationX(diffX) for X coordinate and view.setTranslationY(diffY) for Y coordinate while its MotionEvent.ACTION_MOVE under onTouch() method.
view.setOnTouchListener(new View.OnTouchListener() {
float downX, downY, nowX, nowY, diffX, diffY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getRawX();
downY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
nowX = event.getRawX();
nowY = event.getRawY();
diffX = nowX - downX;
diffY = nowY - downY;
v.setTranslationX(diffX);
v.setTranslationY(diffY);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
It is not related to changing order in RecyclerView question and I didn't find satisfying example.
Let's say we want to put apple to one of the boxes. In our application we have ImageView on the top which represents apple, and boxes below. These boxes are just RecyclerView with GridLayoutManager.
First we set onTouchListener on apple:
appleImageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
});
Now we set OnDragListener on our container which contains RecyclerView (or can we set on RecyclerView?):
containerLayout.setOnDragListener(new OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
final View draggedView = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// HERE IS INTERESTING PART
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
});
The question is:
How can we get RecyclerView item on which drop action was happened?
I thought maybe we can get coordinates where we dropped appleImageView by calling event.getX(); event.getY(); but I don't how to get RecyclerView item with screen coordinates.
I am pretty new to Android and I am making an app with a Vertical Linear Layout at the bottom of the screen with a list of imageViews going across the screen. Each of these are draggable onto the main part of the screen, however, if I drag one of the imageviews on the far right of the layout, it will drop the view far to the left of the actual drop point. What is strange is that if I try dragging the first imageview in the linearlayout (the one on the left side) it will land right under where I drop it.
My onDrag and onTouch Listeners...
class MyDragListener implements OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
float X = event.getX();
float Y = event.getY();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
container = (RelativeLayout) v;
container.setLayoutParams(findViewById(R.id.drawingbackground).getLayoutParams());
container.addView(view);
view.setX(X-(view.getWidth()/2));
view.setY(Y-(view.getHeight()/2));
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
and the OnTouch...
final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return true;
} if(motionEvent.getAction()== MotionEvent.ACTION_UP){
view.performClick();
}
return false;
}
}
Any assistance would be greatly appreciated!
Change your xml.Use Relative Layout Instead of LinearLayout and Fix their height and width.It would work pretty fine.
I have a listview and I set an onClickListener and an onTouchListener. The click listener for when they click on an item, I had just this and everything thing worked fine. But, when I added the touch listener to watch for left and right swipes, it won't let me scroll anymore.
listview.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
historicX = event.getX();
historicY = event.getY();
break;
case MotionEvent.ACTION_UP:
if(event.getX() - historicX < -DELTA){
slidingLeft(event.getX(),event.getY());
return true;
}
else if(event.getX() - historicX > DELTA){
slidingRight(event.getX(), event.getY());
return true;
}
break;
default: return true;
}
return false;
}
});
How can I have to able to scroll through the listview again. Is there another listener I could add to do this, or could I modify the onTouchListener to watch for scrolls too?
You should use GestureDetector .
You can use example from here, it is very clear.
https://stackoverflow.com/a/12938787/1374065