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);
}
}
Related
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
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.
I have a ViewPager that is connected to FragmentStatePagerAdapter.
Now each fragment from that FragmentStatePagerAdapter that comes to ViewPager is made up of custom views.
Problem is, if I start swiping on those custom views, ViewPager won't react to swipe, but I have to carefully position my finger between those custom views and then swipe so it catches the swiping motion.
Anyone knows what is the problem? I tried setting those custom views to android:clickable="true" and android:clickable="false" but it didn't work.
in the constructors of your custom views, use requestDisallowInterceptTouchEvent(true);
Also, if your custom view contains a textview, then make sure you replace android:singleLine="true" with android:maxLines="1" otherwise it breaks the viewpager horizontal scrolling ! (Gotta love Google's lovely bugs!)
add this line to your custom view
#Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
In my app I use two external libraries (Sliding menu and Horizontal Listview). The first one implements a sliding menu effect like the Facebook app and it works fine, I can trigger the movement by swiping or clicking a button. The second one implements an horizontal listview which I can scroll swiping right/left. Using both of them I can see the listview but the scroll movement doesn't work. How can I solve it? I guess the menu is "stealing" the swipe action.
If you need some code just ask.
EDIT: Actually it works! I just have to put the finger on the horizontal view and swipe a little bit up/down and then I can swipe left/right. Which could be tha cause of this behaviour?
There is very simple solution! Just set OnTouchListener for your HorizontalListView, disable slinding menu on ACTION_DOWN and reenable it on ACTION_UP.
getActivity().findViewById(R.id.my_horizontal_list_view).setOnTouchListener(
new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
((SlidingFragmentActivity) getActivity()).getSlidingMenu().setSlidingEnabled(false);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
((SlidingFragmentActivity) getActivity()).getSlidingMenu().setSlidingEnabled(true);
}
return false;
}
}
);
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.