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;
}
}
);
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 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);
}
}
Scope
There is a viewpager of two fragments. One of those fragments has a layout witch listens to onTouch changes at X-axis.
Problem
Layout doesn't get almost all Action.Move events when touching and sliding along X-axis.
It seems that viewpager has a onInterceptTouchEvent which returns true.
Question
Is it real to override viewpager's behavior to make it and my layout work together? So the perfect situation is layout intercepts all onTouch events on it and viewpager manages the rest of onTouch events. Thanks!
You are right, I believe every scrolling container intercepts touch events, but you can prevent it. You can put a touch listener on your layout:
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
pager.requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
pager.requestDisallowInterceptTouchEvent(false);
break;
}
}
Similar situation (but not using a ViewPager), putting this in the view that needed the touch event worked for me. Add checks for MotionEvents other than ACTION_MOVE if applicable to your use case.
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
this.getParent().requestDisallowInterceptTouchEvent(true);
return true;
} else {
return super.onTouchEvent(event);
}
}
neutrino was right!
getParent().requestDisallowInterceptTouchEvent(true);
once the viewpager access the touchEvent Intercept,the child view in it can got the event.
enter image description here
I use a FrameLayout in viewpager to got the DrawerLayout Effect(I need it not match the height of screen,so I can't use drawerlayout or navigation drawer).
It really helps!
I had a similar problem.
In my case I was setting a OnTouchListener on ViewPager but it wasn't receiving touch events when the children that received the touch had onClick set.
What I did was extend the ViewPager class and call my click listener inside the method onInterceptTouchEvent(boolean) and it worked fine. Just be careful not to intercept wrong events.
I'm using a GridView for a game board. Recently some users have had problems with the board scrolling vertically (on Samsung Galaxy / Vibrant phones running 2.2) -- This bug does not occur on my Nexus One.
One user produced some screenshots of the problem.
How could I lock the GridView in place? Is there a way to disable scrolling?
Try to add or override setOnTouchListener for GridView,
then in onTouch method you can use code like this to
make gridView not scrollable
Java
gridView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return event.getAction() == MotionEvent.ACTION_MOVE;
}
});
Kotlin
gridView.setOnTouchListener { v, event ->
event.action == MotionEvent.ACTION_MOVE
}
You can try setEnabled(false), although it might have other side effects. GridView is really not meant to be used the way you are using it. You should create your own custom view or layout. You could also use a TableLayout.