What situation can trigger horizontalScrollView's onScrollChanged event? - android

I have a problem with HorizontalScrollView. I have overriden onScrollChanged with only watching x scroll position value. When I am touching the screen and moving my finger, the value in onScrollChanged is correct. When I release my finger each value appears twice in my logcat. It seems that a position value triggered the event twice after my finger left the screen.
First, I created a class that extends from HorizontalScrollView and overriden onScrollChanged
public class MyHorizontalScrollView extends HorizontalScrollView {
#Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
Log.i("12345", String.valueOf(x) + " " + String.valueOf(oldx));
}
}
Summary:
When I am touching the screen and moving, each value appears once.
After my finger left the screen, each value apprears twice.
Please help me solve this problem. I want to know where the second call is from.

The reason behind this is that when MotionEvent.ACTION_UP motion event is fired, onScrollChanged is called again. Add these methods to your scroll view to suppress that event, which will fix your problem.
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return event.getAction() == MotionEvent.ACTION_UP || super.onTouchEvent(event);
}

Related

How to pass a click event to RecyclerView's items?

I have an ordinary RecyclerView, and on top of it a transparent View that implements GestureListener, which basically have the same size of the RecyclerView.
The GestureListener will listen to scroll and fling gestures, and pass this MotionEvent to the RecyclerView underneath it.
I have already made the RecyclerView able to scroll and fling. However, I can't find a way to pass a click event down to the RecyclerView's items as well.
I already know that this is because ACTION_DOWN is consumed in the GestureListener. In fact, GestureListener has a onSingleTap() method for you to override, and this method was called whenever I perform a click.
According to this post, I tried to set an OnTouchListener to my itemView and listen to ACTION_UP events. However, the onTouch() method is never called.
Below is how I do it:
1. Create a callback in the transparent GestureListener
#Override
public boolean onSingleTapUp(MotionEvent e) {
if (scrollDetector == null) return false;
scrollDetector.onSingleTap(e);
return true;
}
Configure the callback in the activity, and pass the MotionEvent to the RecyclerView
#Override
public void onSingleTap(MotionEvent e) {
mRecyclerView.onTouchEvent(e);
}
Set OnTouchListener to the itemView in the adapter:
itemView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
v.performClick();
return true;
}
return false;
}
});
Using debugger, I can see that mRecyclerView.onTouchEvent(e) was called; but the onTouch() of itemView was not called.
So... How should I correctly pass the MotionEvent to the itemView?
You may ask - "Why do you place a GestureListener on top of the RecyclerView?"
This is because I need to change the height of the RecyclerView when the RecyclerView is scrolled. However, if I do this using RecyclerView's addOnScrollListener, the value of dy will fluctuate between negative and positive values, because dy is affected by its height as well. And the fluctuation will also be reflected to the UI.
Therefore I need a scroll detector that does not change its height when scrolled, and just pass the scroll and fling values to RecyclerView by programmatically calling scrollBy() and fling().
You should known that recyclerview's event.If recyclerview can move when you scroll views,it will call onInterceptTouchEvent() and return true to intercept event.So you can't get the ACTION_MOVE event.Maybe you should rewrite the recyclerview's onInterceptTouchEvent() and return false. Then you can get all the event in your itemView's methods.
Stupid me. I should use dispatchTouchEvent(MotionEvent e) instead of onTouchEvent(MotionEvent e).
However, this is not enough.
Simply calling dispatchTouchEvent(e) using the MotionEvent from GestureListener is not working, because that e is an ACTION_UP event.
To simulate a click, you need both ACTION_DOWN and ACTION_UP.
And itemView does not need to set OnTouchListener since you have already simulate
Code:
#Override
public void onSingleTap(MotionEvent e) {
long downTime = SystemClock.uptimeMillis();
long upTime = downTime + 100;
MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN,
e.getX(), e.getY(), 0);
mRecyclerView.dispatchTouchEvent(downEvent);
MotionEvent upEvent = MotionEvent.obtain(upTime, upTime, MotionEvent.ACTION_UP,
e.getX(), e.getY(), 0);
mRecyclerView.dispatchTouchEvent(upEvent);
downEvent.recycle();
upEvent.recycle();
}

onKeyListener with onScrollListener

Really not sure if I understand this or maybe going about it wrong. I have a gridlayout (cells are textview) wrapped inside a horizontal scroll which is inside a vertical scroll. I am using the Dpad to navigate across the grid. This works well, as I press the right arrow pad the grid cells move left to right as expected and right to left as left arrow pad is pressed. I have added an onKeylistener attached to each textView of the grid, as I scroll across the grid I am changing the color back ground. The problem is that the onKeyListener apparently takes over the control of the grid. The scroll right works for changing the color but the cells no longer move on the grid. Once I get to last visible column focus continues off screen but the cells stay off screen. Is there a way to implement the scroll inside the onkey event so the cells shift and I have control over the properties of the cell? Or is there a totally different way of doing making this work?
The main components are
textViewD.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(keyEvent.getAction()==KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_DPAD_RIGHT) {
gridLayoutE.getChildAt(childIndex[0]).setBackgroundColor(Color.BLACK);
gridLayoutE.getChildAt(childIndex[0] + 1).setBackgroundColor(Color.GREEN);
childIndex[0] = childIndex[0] + 1;
gridLayoutE.requestFocus();
return true;
}
return false;
}
The scrolllistener, I have a class that extends the horizontalscroll in order to have my header table scroll along with my grid. This works.
#Override
public void onScrollChanged (ObservableScrollView scrollView,int x, int y, int oldx, int oldy){
if (scrollView == hsvHeader) {
hsvBody.scrollTo(x, y);
} else if (scrollView == hsvBody) {
hsvHeader.scrollTo(x, y);
}
}
I was able to find a solution to my problem by using dispatchKeyEvent(KeyEvent event). I ended up with something like
public boolean dispatchKeyEvent(KeyEvent event) {
mcols= mcols + 1;
if(event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
gridLayoutE.getFocusedChild().setBackgroundColor(Color.BLACK);
gridLayoutE.findViewById(mcol).setBackgroundColor(Color.GREEN);
}
return super.dispatchKeyEvent(event);
}
This along with my scrolllistener, allowed my to scroll the grid and change each cell as I did.

How to move scrollview with button

I want to create an Android Application that use scrollView, but i don't want to scroll it with drag on the screen, but i want to scroll it with a button. my first question is how to disable all of touch on the scrollView, so it won't scroll when user touch on it?
and the second how to scrool with a button? for the second question, i try to use scrollTo() method. so this is my code on the button
public void UpCLick(View view){
currentY=currentY - 1;
vertScroll.scrollTo(currentX,currentY);
}
public void DownCLick(View view){
currentY=currentY + 1;
vertScroll.scrollTo(currentX,currentY);
}
but it always get stopped. FYI currentX, and currentY is an Integer variable.
extend ScrollView and override onTouchEvent() in ScrollView. don't call super method in it.
#Override
public boolean onTouchEvent(MotionEvent event) {
// super.onTouchEvent(event); // don't do this
return true;
}
and use ScrollView.scrollBy(int,int) method for scrolling.

Intercept touch event SlidingPaneLayout

I have an Activity with a SlidingPaneLayout, and inside there are two fragments: a ListView on the left, and a MapFragment on the right.
How is it possible to intercept the touch event generated so that the user can move the map without close the panel?
The only area that I would like to use to close/open the right panel is the first fourth. On the right of that area I would like to move the map.
Thanks
EDIT2:
Ok, now I figured out how to properly subclass SlidingPaneLayout, now the problem is how to capture correctly the touch event:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN
&& event.getX() > (getWidth() / 6)) {
return false;
}
return super.onTouchEvent(event);
}
With this code I'm not able to slide the map, it remains fixed.
the problem is that I want to intercept the touch ONLY when the right panel is selected (in other words, only when map is displayed).
SlidingPaneLayout have it's own touch listener, so when you reset it by calling setOnTouchListener (which is a method from the super class View) you are loosing all the onTouch behaviour specific to a SlidingPaneLayout.
-------------------------------
Here is a try : make your own SlidingPaneLayout :
the constructor should be this way in order to use your view in an xml layout
public class MySlidingPaneLayout extends SlidingPaneLayout{
public MySlidingPaneLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event){
if (event.getX() < widthPIX / 6) {
return super.onTouchEvent(event);// here it works as a normal SlidingPaneLayout
}
return false; // here it returns false so that another event's listener should be called, in your case the MapFragment listener
}
}
and in your code add MySlidingPaneLayout instead
I finally solved the problem:
simply override this method and control if the SlidingPaneLayout is closed or open (in my case I have a boolean field value "open")
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (!homeActivity.open && event.getX() > (getWidth() / 5)) {
return false;
}
return super.onInterceptTouchEvent(event);
}

Let parent View assume MotionEvents if child returns false

I have a application that need event handling on a unusual way.
For my question, let me first explain a simple case that the current event handling system of Android don't fits for me.
Supposing that I have a FrameLayout (that I'll call ViewSwiper since now) that all Views added on it are MATCH_PARENT X MATCH_PARENT (that I'll call PageView), it's handles events by translating the actual View and replace it based on the direction of moving.
This component I already have done and work properly ( Using Animation to swipe views ).
But the problem is on that PageView I add on top of it, in case of ImageViews that return false on it's onTouchEvent, the ViewSwiper will handle the events and let another PageView enter the screen, but if I add a ScrollView on that, all the events will be consumed by the Scroll and the ViewSwiper will not have chance to replace the PageView.
So I figured out that returning false onTouchEvent of the ScrollView the parent can assume it's events, I wrote this sub-class of ScrollView to test it:
public class ScrollViewVertical extends ScrollView {
public ScrollViewVertical(Context context) {
super(context);
setOverScrollMode(OVER_SCROLL_ALWAYS);
setVerticalScrollBarEnabled(false);
}
public boolean onTouchEvent(MotionEvent evt) {
super.onTouchEvent(evt);
return false;
}
}
But returning false make any further events to get dispatched to the parent, but I need these events for VERTICAL scrolling, so I have the idea to return falses only if the user are moving HORIZONTAL, that's what my code looks like:
public class ScrollViewVertical extends ScrollView {
private MovementTracker moveTracker;
public ScrollViewVertical(Context context) {
super(context);
setOverScrollMode(OVER_SCROLL_ALWAYS);
setVerticalScrollBarEnabled(false);
moveTracker = new MovementTracker();
}
public boolean onTouchEvent(MotionEvent evt) {
if (moveTracker.track(evt))
if (moveTracker.getDirection() == Direction.HORIZONTAL)
return false;
return super.onTouchEvent(evt);
}
}
PS: MovementTracker will returns true on track() after some events and tell on which direction the user is moving.
But in that case, the ScrollView keep receiving events since it's returns true on the first events.
Any ideas on how can I handle the events on ViewSwiper when it's child returns false (even if some trues are returned).
PS: I can give more info about this if needed, and accept different solutions also.
Based on answers I tried the following:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
onTouchEvent(ev);
return intercept;
}
public boolean onTouchEvent(MotionEvent evt) {
boolean x = super.onTouchEvent(evt);
if (moveTracker.track(evt)) {
intercept = moveTracker.getDirection() != Direction.VERTICAL;
if (!intercept)
getParent().requestDisallowInterceptTouchEvent(false);
}
return x;
}
Still nothing.
try this in onTouchEvent() of the scrollview
//if (evt.getAction() == MotionEvent.ACTION_MOVE) {
if (moveTracker.track(evt)){
if (moveTracker.getDirection() == Direction.VERTICAL){
//Or the direction you want the scrollview keep moving
getParent().requestDisallowInterceptTouchEvent(true);
}
}
return true;
Update
Please try the following to the custom Scrollview
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return false;
}
And nothing else
This way i assume the MotionEvent will perform on both views. And since they don't conflict (One is vertical the other one is Horizontal) this could work
Based on the answer from weakwire, I came to the following solution:
On ViewSwiper
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(!super.dispatchTouchEvent(ev))
onTouchEvent(ev);
return true;
}
And on ScrollHorizontal I return false on dispatchTouchEvent when I don't need then anymore.

Categories

Resources