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.
Related
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.
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 want to use a ListView (and have done this successfully before) containing custom Views.
Basically these custom views are vertical sliders, obviously conflicting with the natural behaviour of the ListView.
I fill it with my custom Views from an Adapter, and these react to the touches on the Items,
but once I move my finger more than a few pixels, it will scroll, and the custom View will not receive any touch-events anymore.
How can I (nicely) prevent the ListView from scrolling, when I touch my own components?
Can I somehow disable ListView Selection, and just forward the touches to the Items, but still use the scrolling behaviour?
Thank you in advance.
To prevent the scrolling of listview you can inplement on touch listener as follows
listView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_MOVE)
{
return true;
}
return false;
}
});
hope it will work and if it us useful to you give vote
You can intercept all touch events using onInterceptTouchEvent() in your root layout (one that contains the ListView, like a FrameLayout) as found here.
What you do there is capture the motion events (return true when a MotionEvent.ACTION_DOWN comes in), capture the following event in onTouchEvent(), decide whether the motion is meant for the list items or the list itself and accordingly dispatch the events.
Don't expect this to work easily. Understanding the flow of motion events and the interaction between onInterceptTouchEvent() and onTouchEvent() is challenging and making it work even more so. But I'm confident that this is a feasible way to solve your problem.
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;
}
}
);
when i tried to use scrollview or webview in a scrollview, the inside scrollview or webview can not scroll. I can scroll with trackball but I can't scroll with touch. Do you have any idea about this issue?
I found something and it works for me. I wanted to share. Here it is,
I have one scrollview and listview. listview inside scrollview. Scrollview-> RelativeLayout-> Listview. That listener belongs to Listview;
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
ScrollView().requestDisallowInterceptTouchEvent(true);
}
return false;
}
Android does not support using ScrollViews or ListViews inside each other. It's possible to do, but it's A Bad Thing (TM), according to Google developers.
Search SO for similar problems with ListViews, and you'll find several discussions + suggestions on how to hack around the limitations of the OS. However, I would strongly suggest you reconsider your layout to try and find some other way to handle your layout.
ScrollView supports only one direct child; it can be either a LinearLayout or a RelativeLayout. If you have more than one children the scroll wouldn't work properly.