ViewPager scrolling issue in Android - 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.

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

Android touch event propagation

It's my understanding that Android event propagation goes from parent to child, that is to say, it starts with the outermost element and inwards from there. My question is, why is it that when I try to scroll vertically a listview that is inside a viewpager that is wrapped on a scrollview, the listview moves, and not the viewpager.
Okay, let me rephrase that:
I'm trying to create a menu that appears when the user pulls down the view pager, let me make that even clearer:
Scrollview
My custom Menu
ViewPager (with three fragments, all of them have a lisview)
ListView
I understand that what I'm trying to do is a bit odd, but bear with me just for this time. :)
What can I do to "disable" momentarily the list views scrolling.
Thanks
It seems you have to override the scrolling event. A good webpage is Disable scrolling in Android ListView .
Mainly look at dispatchTouchEvent. Snippet of it:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
Personally I wish it is simpler than this like disabling scroll method.

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: ListView with Swipe inside ViewPager

I have an activity with a ViewPager that covers the whole activity. Inside this ViewPager is a ListView, that only covers the bottom part of the fragment. The ListView recognizes swipe events (you can swipe Items left and right) but when you want to swipe a list item the ViewPager switches the page, so it doesn't work.
Is there an easy way to deal with this problem, so that the ViewPager doesn't receive the touch events that are intended for the list.
Thanks
My first idea is requestDisallowInterceptTouchEvent() may help you.
public boolean onTouch(View v, MotionEvent e) {
if(e.getAction() == MotionEvent.ACTION_DOWN){
listItem.getParent().requestDisallowInterceptTouchEvent(true);
}
}

Android HorizontalScrollView disable touch handling but allow child get events

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.

Categories

Resources