Android HorizontalScrollView disable touch handling but allow child get events - android

I have a case which includes 3 ScrollViews stacked following way:
HorizontalScrollView on top and it contains layout, which has ScrollView, which contains layout containing another HorizontalScrollView.
HorizontalScrollView
--------ScrollView
------------HorizontalScrollView
Without the top HorizontalScrollView touch event's work okay, and now I need to disable the top HorizontalScrollView, but allow the child's get events normally, child HorizontalScrollView will eventually scroll the top HorizontalScrollView.
Thanks.

i am not very clear regarding what you want to do but, this should work,
OnTouchListener skipTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//do nothing
return false;
}
};
yourView.setOnTouchListener(skipTouchListener);
This way yourView will not process the touch events.

Related

How to make ViewPager scrollable from the parent view?

I was implementing the app tutorial which looks like this.
I made a nice and small ViewPager and placed it on the mock-up ImageView. The problem is that the ViewPager is not scrolled when I try to scroll it from elsewhere, say around the view pager indicators. This is so natural because there's no way that the ViewPager listens to touch event outside of itself.
How can I make ViewPager be scrolled when I try to scroll from elsewhere?
I've tried to detect touch events on the parent of the ViewPager but I couldn't figure out how to relate onFling() or onScroll() to ViewPager's scrolling.
If there's any better suggestion of implementing this kind of UI, what would be it?
Is there any tutorial or custom library similar to this?
set a View.OnTouchListener for your outer ViewPager and check inside if you are on proper page which is displaying inner ViewPager. if inner ViewPager isn't on its first or last you might dispatch MotionEvent to second dispatchTouchEvent(MotionEvent me)
outerViewPager.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(outerViewPagerAdapter.isCurrentPageHaveInnerViewPager() &&
! innerViewPagerAdapter.isOnFirstOrLastPage()){
innerViewPagerAdapter.dispatchTouchEvent(event);
return true;
}
return super.onTouch(event); //outer will get touch events
}
});
you might also adjust x/y touch cords in event before dispatching if needed

RecyclerView starting in the middle of ScrollView

I have scrollview. Inside that ScrollView I have some content on top and RecyclerView that starts in the middle of the screen. I want to be able to scroll down until that RecyclerView fill whole ScrollView then i want to be able to scroll in that RecyclerView.
Examples from existing apps: In google plus app on someone's profile, there is some info about user on top but you could scroll down to list of his posts. In Instagram or Facebook is this too.
I tried this:
First i set height of RecyclerView to have the same height as ScrollView.
Then i added OnTouchListener to RecyclerView so it will get scrolls events when ScrollView is scrolled down.
recyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(scrollView.getChildAt(0).getMeasuredHeight() <= scrollView.getScrollY() + scrollView.getHeight()){
v.getParent().requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
It is ok when i want to scroll down but I am unable to scroll back to top to content that isn't inside RecyclerView.
Any ideas how to achieve that? I am also loading more content to RecyclerView as user scrolls down, so i don't know final height of that RecyclerView.

Android make two overlapping views receive and handle the same touch event

I'm using a RelativeLayout to overlap two different views. Both Views fill the entire Screen.
If the user touches the screen, I want both views to receive the TouchEvent and to excecute their own onTouch-Methods. At the moment, only one view receives the TouchEvent
How can I make both views receive the TouchEvent?
Thank you in advance for your help.
EDIT: Here's the code
touchView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
view1.onTouchEvent(event);
view2.onTouchEvent(event);
return false;
}
});
You can create a third invisible View and place it on top of the overlapping part. Then in its TouchEvent perform the actions of those two TouchEvents
For top view assign clickable=false
it will allow the touch to intercept through it
You could try to get to check if the rawX & rawY of the TouchEvent are within the hitRect of the view that didn't get the event. If it did, you can use dispatchTouchEvent(MotionEvent) to send the TouchEvent tot the view that didn't get the event

How to send event from custom linear layout to child view

I have a custom linearlayout in which I have two gridviews.
I want to have multitouch on the complete linearlayout, ie, first touch can be in one gridview and second touch can be in another gridview and the linear layout should be able to get multitouch events for this (ACTION_DOWN and ACTION_POINTER_DOWN). I am able to get this by overriding onInterceptTouchEvent in my custom layout.
Now I also want the gridview to scroll independently if required. Although gridview is a scrollable view, it is not able to do so, because I have intercepted the 'down' event. I see that when I try to scroll two events are generated : 'ACTION_DOWN' and 'ACTION_MOVE'.
Since I need ACTION_DOWN for multitouch use case I want it to be sent to linearlayout as well.
Is there a possibility to send the ACTION_DOWN event to both the linearlayout as well as my child view which is gridView in this case?
Need help.
You don't have to intercept ACTION_DOWN just to monitor that it flowed through your container view. In other words, you can override onInterceptTouchEvent() to monitor the touch events before they are passed to the appropriate child view by the framework, but as long as you don't screw with the return value it won't actually steal subsequent touch events from the grid elements. For example:
public boolean onInterceptTouchEvent (MotionEvent ev)
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
//Touchdown!
}
//Don't modify this and touch interaction will happen normally
return super.onInterceptTouchEvent(ev);
}
This allows you to see the event without taking it away from the child element(s).

ViewPager scrolling issue in Android

I have a ViewPager with dynamic number of images in it. This ViewPager is added as a custom row to a table view. As this table view can have multiple dynamic custom rows, I have to add this table view in a scrollview for scrolling.
Now my question is, when I am scrolling horizontally to View Pager, it's not exactly horizontal scrolling, it's mixed with some vertical scrolling as well. So when a vertical scrolling is detected the events are passed to the scroll view; which makes the ViewPager reset to the initial position.
So how can I pass back the events to ViewPager or avoid scrollview catching vertical scroll events?
Note: I tried disabling scrollview to vertical scrolling but that didn't stop it from capturing the vertical scroll events.
I found the solution for my question and this is how I did it.
I over ride the on touch event for my Viewpager in the following way
myViewPager.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE && myScrollView!=null){
myScrollView.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
Note: myScrollView is parent of myViewPager, as said in my question.

Categories

Resources